Guillem Escuder
Guillem Escuder

Reputation: 27

For loop: file in directory treatment (batch)

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>

Upvotes: 0

Views: 344

Answers (2)

Guillem Escuder
Guillem Escuder

Reputation: 27

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%

Upvotes: 0

ManoDestra
ManoDestra

Reputation: 6473

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)

Upvotes: 1

Related Questions