如何用findstr的结果设置变量 [英] How to set variable with the result of findstr

查看:866
本文介绍了如何用findstr的结果设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个批处理文件,搜索pdf文件,并找到它们有多少页,并循环所有页面。

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

我写了以下内容。我可以找到文件,我甚至可以找到页面计数与一个工具名为pdftk。结果如下。

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

如何设置一个值 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
)

)


推荐答案

你已经有90%。使用FOR / F命令处理命令的结果。在命令提示符下输入 HELP FOR

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

这篇关于如何用findstr的结果设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆