How to set variable with the result of findstr

本小妞迷上赌 提交于 2020-01-06 08:16:14

问题


I am trying to write a batch file which searches for pdf files and finds how many pages they have and loop all pages.

I wrote the following. I can find the files, I can even find the pagecounts with a tool named pdftk. The results is as below.

C:\Users\test\Documents\fishes\Fish_1.pdf
NumberOfPages: 5

How can I set a variable which has the value of 5?

@ECHO off
for /R %%i IN (*.pdf) DO (

ECHO %%i
"C:\Program Files (x86)\PDF Labs\PDFtk Server\bin\pdftk.exe" %%i dump_data | findstr NumberOfPages

set pagecount = findstr NumberOfPages ???

FOR /L %%j IN (1,1,%pagecount%) DO (
    ECHO "page " + %%j
)

)

回答1:


You were already 90% there. Use the FOR /F command to process the results of a command. Type HELP FOR from the command prompt for more info.

for /f "tokens=2" %%A in (
  '"C:\Program Files (x86)\PDF Labs\PDFtk Server\bin\pdftk.exe" %%i dump_data ^| findstr NumberOfPages'
) do set numberOfPages=%%A


来源:https://stackoverflow.com/questions/14186474/how-to-set-variable-with-the-result-of-findstr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!