Batch file 使用内容重命名PDF文件的批处理脚本

Batch file 使用内容重命名PDF文件的批处理脚本,batch-file,pdf,Batch File,Pdf,我有一个批处理脚本用于提取PDF信息,然后重命名PDF 该脚本对于1个PDF文件运行良好,但我需要在包含大量PDF文件的文件夹中直接使用它 那么怎么做呢 脚本需要针对每个PDF文件逐一运行,直到结束。 一旦重命名了PDF文件,下一个文件将被移动到另一个文件夹中,因此保留在该文件夹中的PDF文件需要相同的东西。当文件夹为空时,脚本退出 @echo off setlocal enabledelayedexpansion set PDF="Renommer_Manuellement.pdf" se

我有一个批处理脚本用于提取PDF信息,然后重命名PDF

该脚本对于1个PDF文件运行良好,但我需要在包含大量PDF文件的文件夹中直接使用它

那么怎么做呢

脚本需要针对每个PDF文件逐一运行,直到结束。 一旦重命名了PDF文件,下一个文件将被移动到另一个文件夹中,因此保留在该文件夹中的PDF文件需要相同的东西。当文件夹为空时,脚本退出

@echo off

setlocal enabledelayedexpansion

set PDF="Renommer_Manuellement.pdf"
set text="Renommer_Manuellement.txt"
set DSUBDIX=%USERPROFILE%\Google Drive\CLASSEURS

ren *.pdf %PDF%
pdftotext -raw %PDF%

for /f "delims=- tokens=2" %%a in ('find "Number=" %text%') do set numeroa=%%a

for /f "delims== tokens=2" %%a in ('find "NAME=" %text%') do set nature=%%a


ren "%PDF%" "OCC-%numeroa:~0,5%#!nature!.pdf"
move "%PDF%" "OCC-%numeroa:~0,5%#!nature!.pdf" "XYZ FOLDER"

exit

可能此注释的批处理文件代码用于将当前目录中的所有PDF文件移动到子目录
XYZ文件夹
,并根据每个PDF文件的内容确定新文件名

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Don't know what this environment variable is for. It is not used by this code.
set "DSUBDIX=%USERPROFILE%\Google Drive\CLASSEURS"

md "XYZ FOLDER" 2>nul

for /F "delims=" %%I in ('dir *.pdf /A-D /B 2^>nul') do call :RenamePDF "%%~fI"

rem Restore initial command environment and exit batch file processing.
endlocal
goto :EOF


:RenamePDF
set "FilePDF=%~1"
set "FileTXT=%~dpn1.txt"

pdftotext.exe -raw "%FilePDF%"

for /F "delims=- tokens=2" %%J in ('%SystemRoot%\System32\find.exe "Number=" "%FileTXT%"') do set "numeroa=%%J"
for /F "delims== tokens=2" %%J in ('%SystemRoot%\System32\find.exe "NAME=" "%FileTXT%"') do set "nature=%%J"

rem The text version of the PDF file is no longer needed.
del "%FileTXT%"

rem Move the PDF file to XYZ FOLDER and rename the file while moving it.
move "%FilePDF%" "XYZ FOLDER\OCC-%numeroa:~0,5%#%nature%.pdf"

rem The file movement could fail in case of a PDF file with new
rem name exists already in target folder. In this case rename
rem the PDF file in its current folder.
if errorlevel 1 ren "%FilePDF%" "OCC-%numeroa:~0,5%#%nature%.pdf"

rem Exit the subroutine RenamePDF and continue with FOR loop in main code.
goto :EOF
我不清楚为什么每个*.pdf文件都应该用新文件名移动到子目录中。
在我看来,这并不是真的必要。命令REN就足够了

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • 呼叫/?
  • del/?
  • dir/?
  • echo/?
  • endlocal/?
  • 查找/?
  • 获取/?
  • goto/?
  • md/?
  • 移动/?
  • rem/?
  • ren/?
  • 设置/?
  • setlocal/?
另请阅读Microsoft关于的文章,了解
2>nul
的解释。当Windows命令解释器在执行
FOR的命令之前处理此命令行时,重定向操作符必须在上用插入符号^转义,以便命令行被解释为文字字符,该命令行以单独的方式执行嵌入的dir命令行命令进程在后台启动

另请参见

这将对您有所帮助