0

I'm having a problem with the next code. I want to process the files in the directory "pdfs_hospital" and remove the password that the .pdf files have. I use the utility pdftk, it works well in a batch file for only one .pdf but I'm having problems when I have to treat more than one .pdf located in a directory. The error is that when I'm in the for loop %%A takes correctly the name of the .pdf file to treat but in the next echo I see that the path of that .pdf is "C:\Program Files (x86)\PDFtk Server\bin\name.pdf" when it has to say "C:\Users\Guillem Escuder\Desktop\pdfs_hospital\name.pdf" so the pdftk.exe program would work fine.

This is the program:

REM @echo off
REM Current directory:
set curr_dir=%cd%
set PATH=C:\Users\Guillem Escuder\Desktop\pdfs_hospital\

REM We change to pdftk.exe directory:
chdir /d "C:\Program Files (x86)\PDFtk Server\bin"
echo %cd%

for /F "delims=," %%A in ('dir /b "%PATH%"') do (
    echo %%~fA
    REM Executing the program:
    start pdftk.exe "%%~fA" input_pw "matrix" output "C:\Users\Guillem Escuder\Desktop\pdfs_hospital_procesados\%%A"
)

chdir /D %curr_dir%

And this is the exit:

C:\Users\Guillem Escuder\Desktop>PdfRemovePass2.bat

C:\Users\Guillem Escuder\Desktop>REM @echo off

C:\Users\Guillem Escuder\Desktop>REM Directorio actual:

C:\Users\Guillem Escuder\Desktop>set curr_dir=C:\Users\Guillem Escuder\Desktop

C:\Users\Guillem Escuder\Desktop>set PATH=C:\Users\Guillem Escuder\Desktop\pdfs_hospital\

C:\Users\Guillem Escuder\Desktop>chdir /d "C:\Program Files (x86)\PDFtk Server\bin"

C:\Program Files (x86)\PDFtk Server\bin>REM Cambiamos a directorio del pdftk:

C:\Program Files (x86)\PDFtk Server\bin>echo C:\Program Files (x86)\PDFtk Server\bin
C:\Program Files (x86)\PDFtk Server\bin

C:\Program Files (x86)\PDFtk Server\bin>for /F "delims=," %A in ('dir /b "C:\Users\Guillem Escuder\Desktop\pdfs_hospital\"') do (
echo %~fA
 REM Ejecutamos el programa:
 start pdftk.exe "%~fA" input_pw "matrix" output "C:\Users\Guillem Escuder\Desktop\pdfs_hospital_procesados\%A"
)

C:\Program Files (x86)\PDFtk Server\bin>(
echo C:\Program Files (x86)\PDFtk Server\bin\Apache Quick Reference Card.pdf
 REM Ejecutamos el programa:
 start pdftk.exe "C:\Program Files (x86)\PDFtk Server\bin\Apache Quick Reference Card.pdf" input_pw "matrix" output "C:\Users\Guillem Escuder\Desktop\pd
fs_hospital_procesados\Apache Quick Reference Card.pdf"
)
C:\Program Files (x86)\PDFtk Server\bin\Apache Quick Reference Card.pdf

C:\Program Files (x86)\PDFtk Server\bin>(
echo C:\Program Files (x86)\PDFtk Server\bin\exam08-1.pdf
 REM Ejecutamos el programa:
 start pdftk.exe "C:\Program Files (x86)\PDFtk Server\bin\exam08-1.pdf" input_pw "matrix" output "C:\Users\Guillem Escuder\Desktop\pdfs_hospital_procesa
dos\exam08-1.pdf"
)
C:\Program Files (x86)\PDFtk Server\bin\exam08-1.pdf

C:\Program Files (x86)\PDFtk Server\bin>chdir /D C:\Users\Guillem Escuder\Desktop

C:\Users\Guillem Escuder\Desktop>

2 Answers 2

1

Change...

for /F "delims=," %%A in ('dir /b "%PATH%"') do (

To...

for /F "delims=," %%A in ('dir /b "%PATH%\*.pdf"') do (

Or, simply...

for %%f in (%PDF_UNLOCK_PATH%\*.pdf) do (::processing here)
Sign up to request clarification or add additional context in comments.

4 Comments

ok, I did it but the error still happens. I don't know if it is that I change directory before the loop. I'm working from the Desktop. There I have the directory "pdfs_hospital" with all the .pdf and the output directory "pdfs_hospital_procesados" when running pdftk.exe. The program pdftk.exe is in C:\Program Files (x86)\PDFtk Server\bin
One thing: I wouldn't change the PATH environment variable, personally. I would call it something else, like PDF_UNLOCK_PATH, rather than affecting the global PATH variable. Or just write the path in the FOR loop. FOR %%f IN (C:\path\to\pdfFiles\*.pdf) DO ( ::process them here ) I think you can dispense with the /F. I would personally just use.. FOR %%f IN (%PDF_UNLOCK_PATH\*.pdf) DO (::stuff)
I changed that PATH variable, I agree with you, it's one mistake. I need to use "delims=," tag because there are maybe one pdf with blanc spaces inside the folder and it forces me to use /f. I don't know why doing an echo %%~fA to show the path of the variable that the loop is processing it shows the current path where I'm in the computer adding at the end the file. I don't know another way to execute the .exe with not changing directory to the path of the program.
I found the solution, thanks for taking your time on me on trying to solve it.
0

SOLVED!

I just put the PDF_UNLOCK_PATH followed by \%%A in the command line of the program and it worked! I don't know if it is a good solution but it works.

This is the code:

set curr_dir=%cd%
set PDF_UNLOCK_PATH=C:\Users\Guillem Escuder\Desktop\pdfs_hospital

for /f "delims=," %%A in ('dir /b "%PDF_UNLOCK_PATH%\*.pdf"') do (
    echo %%A
    chdir /d "C:\Program Files (x86)\PDFtk Server\bin"
    start pdftk.exe "%PDF_UNLOCK_PATH%\%%A" input_pw "matrix" output "C:\Users\Guillem Escuder\Desktop\pdfs_hospital_procesados\%%A"
    chdir /D %curr_dir%
)

chdir /D %curr_dir%

1 Comment

Yep. That's exactly what I said above. Glad you got it working! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.