I need to group multiple PDFs with the same order number (which is shown in the PDF filenames) into a new PDF. The new grouped PDF should have a new name= only the order number. The best solution is using a Windows bat file and GhostScript.
I have written a script, but it doesn't work, and I can't figure out why. The bat file "grouping.bat" is in the same folder as the PDFs.
@echo off
setlocal
rem Set the output directory
set "outputdir=%~dp0"
rem Get the current directory where the batch file is located
set "inputfolder=%~dp0"
rem Loop through the PDF files in the input folder
for %%F in ("%inputfolder%\*.pdf") do (
rem Extract the file name without extension
set "filename=%%~nF"
rem Get characters from position 16 to 22 of the file name
set "characters=!filename:~15,7!"
rem Set the output file path
set "outputfile=%outputdir%\%characters%.pdf"
gswin64c -q -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile="%outputfile%" "%%F""
pause
)
echo PDF files combined successfully.
endlocal
Somehow it doesn't get the part of the file name. Can you guys help me find out why the code is not working?
ps:
As a Programming issue in the Days of DOS this Windows OS limitation is just as much a problem as then.
It was common in 1980s DOS to cycle bat files by recursion to bypass such problems, so this is perhaps dirty by todays standards but old school modernised so feel free to update my attempt. IT WORKS given the OP parameters, but is spaghetti hoops so see lower
Do not use this at home.bat simple modification to show poorly how it may have been done avoiding expansion problems by total recall (where arguments could have negated the delay expansion issues)
:@echo off
if %1. equ ReCurse. goto recurse
rem Get the current directory where the batch file is located
set "inputfolder=%~dp0"
rem Set the output directory
set "outputdir=%~dp0"
goto main
:recurse
echo Looping through the PDF files in the input folder...
set "_#1=%~2"
set "_#2=%_#1:~15,7%"
echo "%~2.pdf" >>"%inputfolder%\%_#2%.txt"
goto eof
:main
del "%outputdir%-*.txt
echo Collating lists
for %%F in ("%inputfolder%RAW*.pdf") do call %~0 ReCurse "%%~nF"
echo Merging lists
for %%F in ("%inputfolder%-*.txt") do "gswin64c.exe" -q -sDEVICE=pdfwrite -o "%%~dpnF.pdf" "@%%F"
echo PDF files combined successfully.
pause
:eof
@echo off & Title "Newly printed PDF groups"
REM Combining is Never "Merging 100%" which needs a considerably more complex editor to renumber some internal PDF objects
setlocal EnableDelayedExpansion
set "GSexe=C:\Apps\PDF\GS\gs10011w64\bin\gswin64c.exe"
REM Get the input set in this case where the batch file is located (beware %~dp0 has trailing \)
set "inSet=%~dp0RAW*.pdf"
REM Set the output directory as can be different (beware trailing \)
set "outputdir=%~dp0"
REM Get characters from position 16 to 22 of the file name without extension
REM txt files will in this case be seven chars from above as -######.lst !!!
REM clear any previous run or potential temporary rogue file
if exist "%temp%\-*.lst" del "%temp%\-*.lst"
echo Looping through the PDF files in the input folder...
for %%F in ("%inSet%") do (
set "filename=%%~nF"
set "characters=!filename:~15,7!"
echo "%%F" >>"%temp%\!characters!.lst"
)
echo Generating new collectives from lists
for %%F in ("%temp%\-*.lst") do ("%GSexe%" -q -sDEVICE=pdfwrite -o "%outputdir%%%~nF.pdf" "@%%F")
echo PDF files combined successfully.
pause
del "%temp%\-*.lst"