Search code examples
batch-filepdftk

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


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

Solution

  • 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