简体   繁体   English

Win bat文件:如何将FIND的结果转换为新变量?

[英]Win bat file: How to get the result of FIND into a new variable?

The pdftk tool "dump_data" function can be used to deliver meta information about a pdf, including the number of pages. pdftk工具“ dump_data”功能可用于传递有关pdf的元信息,包括页面数。 The following command... 以下命令...

pdftk test.pdf dump_data | find "NumberOfPages"

...outputs the full data dump line, for example: ...输出完整的数据转储行,例如:

"Number of pages: 32"

How can I get the count value (32 in the above case) into a new variable for further processing in the bat file? 如何将计数值(上述情况下为32)放入新变量以在bat文件中进行进一步处理?

If the format of the line is fixed and matches the one you've shown, you could try something like this: 如果该行的格式是固定的,并且与您显示的格式匹配,则可以尝试执行以下操作:

@ECHO OFF
>testfile ECHO Number of pages: 32
FOR /F "delims=: tokens=2" %%A IN ('TYPE testfile ^| FIND "Number of pages"') DO SET /A pagenum=%%A
ECHO %pagenum%

Outputs: 输出:

32

Naturally, >testfile ECHO ... line is just for testing purposes, and the TYPE testfile part of the FOR loop should be replaced by your pdftk test.pdf dump_data . 自然, >testfile ECHO ...行仅用于测试目的,并且FOR循环的TYPE testfile测试文件部分应替换为pdftk test.pdf dump_data

Try this: 尝试这个:

FOR /F "usebackq delims=" %%v IN (`pdftk test.pdf dump_data ^| find "Number of pages"`) DO (
    FOR /F "delims=: tokens=1,2" %%i IN ("%%v") DO set NBPAGES=%%j
)

Note that you have to use two % in front of every variables in the above example if you are using it in a batch file. 请注意,如果在批处理文件中使用上述示例中的每个变量,则必须在每个变量之前使用两个% If you are running it directly from the console, use only one % . 如果直接从控制台运行它,则仅使用%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM