如果用户输入有子目录的目录,搜索根目录下的所有子目录

If user inputs directory that has subdirectories search all subdirectories in root

这是对已回答问题的跟进。

如果用户输入 "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

尝试替换

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 不重要,因为它只显示最后一个。

所以我自己(有点)解决了这个问题,请参阅下面的代码。将文件复制到一个临时目录,然后从那里计算它们。这对我来说是一个更简单的解决方案。

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