Solved looping all PDF files with folder

February 14, 2020 at 02:35:59
Specs: Windows 10

I am trying to loop on each file in a specific directory and do something on them. The problem is that the code is looping on only the first file but not on all. Below is what I have written it contains 2 for loops and I am guessing it could be an issues as I am getting the folder path and file name. Any suggestions will be highly helpful.

for %%F in (C:\test\test\*.pdf) do (
"gswin64c.exe" -dBATCH -dNOPAUSE -q -sDEVICE=bbox %1 2> c:\test\CropBox.txt
for /f "tokens=1* delims=:" %%a in ('find "%%BoundingBox: "^<c:\test\CropBox.txt') do set MyVar=%%b (
echo Myvar: %MyVar%
Set Folder=%%~dpA
Set Name=%%~nxA

echo.Folder is: %Folder%
echo.Name is: %Name%
"gswin64c.exe" -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=%Folder%\out\%Name% -c "[/CropBox [%MyVar%] /PAGES pdfmark" -f %1


message edited by Panky245

See More: looping all PDF files with folder

February 14, 2020 at 04:48:34
I often find myself either adding or removing the /F switch, maybe that is what is needed here as well.
The path could also be an issue, in that case just navigate to it ( CD /D C:\path ) and only specify the filenames in the FOR loop.

Report •

February 14, 2020 at 21:37:27
✔ Best Answer
This is best I could do with what you submitted.
@echo off & setlocal
md c:\test\test\out 2>nul
for %%F in (c:\test\test\*.pdf) do (
gswin32c.exe -dBATCH -dNOPAUSE -q -sDEVICE=bbox %%F 2> c:\test\CropBox.txt
call :aa "%%F"

for /f "tokens=1* delims=:" %%a in ('find "%%BoundingBox: "^<c:\test\CropBox.txt') do (
set MyVar=%%b
goto :xx
echo Myvar: %MyVar% Folder: %~dp1 name: %~nx1
rem everything after the -c is complete mystery to me, as my working gswin32 does not
rem have that option. Also, the unmatched brackets usually indicate
rem a coding fault.
gswin64c.exe -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=c:\test\test\out\%~nx1 -c "[/CropBox [%MyVar%] /PAGES pdfmark" -f %~1
goto :eof

message edited by nbrane

Report •

February 19, 2020 at 02:36:32
Thanks nbrane for rescue. With litle bit of tweaking on your script I was able to use the solution. Much appreciated.


Report •
Related Solutions

Ask Question