0

This is a follow up to a question that has already been answered.

If a user inputs "c:\folder" and there happens to be several folders in that directory how do I make it search all subdirectories in the requested root directory.

Sample Code below currently searches one folder and displays results even if there are multiple folders.

@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
Nick
  • 49
  • 5
  • Did you have a look at for /r ? – Marged Aug 07 '15 at 16:38
  • Yes, I still get a page count of 0 even though there are multiple folders in the root I am selecting which all contain PDF's. This is really stumping me. – Nick Aug 07 '15 at 16:42

2 Answers2

2

Try replace

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
    )
)

With

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 isn't important because it only shows the last one.

Happy Face
  • 1,061
  • 7
  • 18
0

So I solved this on my own (sort of), See my code below. Copying the files to a temporary directory and then counting them from there. This was a much easier solution for me.

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
Nick
  • 49
  • 5