Search code examples
windowspdfbatch-filebatch-rename

Windows batch operation for adding pdf page dimensions to filename?


I need to include page size information of many single-paged pdf's into their filenames. E.g. "150x250mm.pdf". I found no renamer apps able to do it. I suspect this could be done using a batch file and pdfinfo.exe (from xpdf suite), but I have no idea how to make use of it.. Could you give me some hints?


Solution

  • Yes, you can convert from postscript points to MM. In this case, the script is in the top level folder containing the PDF's to be renamed. It does go into subfolders. If you don't want or need that, remove the /s from the dir command on the 5th line. Change the paths as needed.

    @echo off
    setlocal enabledelayedexpansion
    
    set "pdfi=U:\Scripts\Utilities\xpdf\pdfinfo.exe"
    for /f "delims=" %%a in ('dir /b /s *.pdf') do (
      for /f "tokens=3,5 delims= " %%b in (
        '%pdfi% -meta "%%a"^|find /i "Page size:"') do (
          set pts=%%b %%c
            for %%d in (!pts!) do (
              call :Eval %%d*.352777778 mm
              set "mm1=!mm1!x!mm!"
            )
            ren "%%~dpfnxa" "!mm1:~1!.pdf"
            set mm1=
      )
    )
    exit /b
    
    :Eval <in> <out>
    setlocal
    if exist eval.vbs del eval.vbs
    >eval.vbs echo wsh.echo formatnumber(eval("%1"),0)
    for /f "delims=" %%a in ( 
      'cscript //nologo eval.vbs' 
    ) do endlocal & set %~2=%%a
    del eval.vbs