1

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:

  • Ghostscript is installed on the win10 computer and settings in the environment (PATH) are working well.
  • The PDF file names are structured as follows: "RAWRenderingDoc-976866-84.pdf".
Kico
  • 13
  • 4
  • `set "outputfile=%outputdir%\!characters!.pdf"` and `gswin64c -q -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile="!outputfile!" `, You need `!var!` to access the new value of any variable changed within the code block (parenthesised sequence of lines). This is the [delayed expansion trap](https://stackoverflow.com/a/30284028/2128947) – Magoo May 17 '23 at 06:43
  • I have adjusted the code, and now it creates a file with the name !outputfile!, without extension. – Kico May 17 '23 at 07:00
  • Seems odd. I have no exposure to ghostscript. If you `echo gswin64c …` does the `.pdf` appear in the output filename? Perhaps you need to add an extra parameter to tell `ghostscript` to create a `.pdf` filename. – Magoo May 17 '23 at 07:28
  • Ghostscript doesn't need any extra parameter, it will create the output filename exactly as specified by the OutputFile. Try ECHO'ing the output filename and see what it actually contains. But the first thing I would say is "don't use '\' as a separator". This is readily confused by escaping, use '/' in the definition of outputfile instead. Ghostscript understands either, but the '/' avoids any escape problems. – KenS May 17 '23 at 07:30

2 Answers2

0

You are using delayed expansion (the !var! syntax) but you did not enable the use of delayed epansion. Default delayed expansion is disabled. You have to enable this with the command: setlocal enabledelayedexpansion and use it inside the for-loop as mentioned in the comments by Magoo.

OJBakker
  • 602
  • 1
  • 5
  • 6
  • I'm now one step further. Now it creates a new PDF with the correct name (order number). However, it remains empty (0kb). It seems that the PDFs are not merged in the loop. – Kico May 17 '23 at 09:14
  • Ghostsscript doesn't, strictly, merge PDF files, it produces a new PDF file which should look like the originals. You appear to be calling Ghostscript once per input file, that's not going to work. Each time you do that you start Ghostscript up, process one file and then close it. So obviously the output will only contain the one input file. You need to supply **all** the input files to Ghostscript in one go. So something like "gswin64c -sDEVICE=pdfwrite -o out.pdf input1.pdf input2.pdf...last.pdf". No you can't use wildcards. – KenS May 17 '23 at 18:52
0

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

Modern cleaner one line method

@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"
Community
  • 1
  • 1
K J
  • 8,045
  • 3
  • 14
  • 36
  • Thanks KJ When I run the *.bat, it creates the correct file name and also the grouping, but I still get the blank PDFs. What am I doing wrong? – Kico May 19 '23 at 08:06
  • CMD displays: ` Generating new collectives from lists Error: /undefinedfilename in (C:\\Users\\804\\Desktop\\Ticket) Operand stack: Execution stack: %interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push Dictionary stack: --dict:770/1123(ro)(G)-- --dict:0/20(G)-- --dict:75/200(L)-- Last OS error: No such file or directory GPL Ghostscript 10.01.1: Unrecoverable error, exit code 1 ` – Kico May 19 '23 at 08:38