for循环:目录处理中的文件(批处理)

时间:2016-03-14 16:26:55

标签: batch-file for-loop

我遇到下一个代码的问题。我想处理目录“pdfs_hospital”中的文件并删除.pdf文件所具有的密码。我使用实用程序pdftk,它只在一个.pdf的批处理文件中运行良好,但是当我必须处理位于目录中的多个.pdf时,我遇到了问题。错误是当我在for循环中时%% A正确地获取要处理的.pdf文件的名称但在下一个回显中我看到该.pdf的路径是“C:\ Program Files(x86) \ PDFtk Server \ bin \ name.pdf“当它必须说”C:\ Users \ Guillem Escuder \ Desktop \ pdfs_hospital \ name.pdf“时,pdftk.exe程序才能正常工作。

这是该计划:

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%

这是退出:

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 个答案:

答案 0 :(得分:1)

更改...

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

要...

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

或者,简单地说......

for %%f in (%PDF_UNLOCK_PATH%\*.pdf) do (::processing here)

答案 1 :(得分:0)

解决!

我只是把PDF_UNLOCK_PATH后跟\ %% A放在程序的命令行中就可以了!我不知道这是否是一个好的解决方案,但它有效。

这是代码:

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%