这是对已经回答的问题的跟进。
如果用户输入" c:\ folder"并且在该目录中恰好有几个文件夹如何使其搜索请求的根目录中的所有子目录。
下面的示例代码目前搜索一个文件夹并显示结果,即使有多个文件夹。
@echo off
set total=0
set /p direct=What directory do you want to count?
for %%a in (%direct%\*.pdf) do (
title %%a
for /f "tokens=2 delims=: " %%b in ('pdftk "%%a" dump_data ^| find "NumberOfPages"') do (
set /a total+=%%b
)
)
echo TOTAL PAGE COUNT IS %total%
pause
答案 0 :(得分:2)
尝试替换
for %%a in (%direct%\*.pdf) do (
title %%a
for /f "tokens=2 delims=: " %%b in ('pdftk "%%a" dump_data ^| find "NumberOfPages"') do (
set /a total+=%%b
)
)
使用
forfiles /p %direct% /c "cmd /c if @isdir == FALSE (for %%a in (@path\*.pdf) do (for /f "tokens=2 delims=: " %%b in ('pdftk "%%a" dump_data ^| find "NumberOfPages"') do (set /a total+=%%b)))"
title %%a
并不重要,因为它只显示最后一个。
答案 1 :(得分:0)
所以我自己解决了这个问题,请参阅下面的代码。将文件复制到临时目录,然后从那里计算它们。这对我来说是一个更容易的解决方案。
set total=0
set /p direct=What directory do you want to count?
del "C:\Program Files\PDF COUNTER\TEMP\*.pdf"
pushd %direct%
for /r %%a in (*.pdf) do (
copy "%%a" "C:\Program Files\PDF COUNTER\TEMP\%%~nxa"
)
popd
for %%a in ("C:\Program Files\PDF COUNTER\TEMP\*.pdf") do (
title %%a
for /f "tokens=2 delims=: " %%b in ('pdftk "%%a" dump_data ^| find "NumberOfPages"') do (
set /a total+=%%b
)
)
echo TOTAL PAGE COUNT IS %total%
pause