我有一个批处理脚本,用来提取PDF信息,然后重命名PDF。
这个脚本对1PDF文件很好,但是我需要在文件夹中直接使用它,其中有很多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发布于 2018-01-14 12:37:42
这个批次文件代码可能用于将当前目录中的所有PDF文件移动到子目录XYZ FOLDER,并根据每个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就足够了。
要了解所使用的命令及其工作方式,请打开命令提示符窗口,在那里执行以下命令,并非常仔细地读取为每个命令显示的所有帮助页。
call /?del /?dir /?echo /?endlocal /?find /?for /?goto /?md /?move /?rem /?ren /?set /?setlocal /?还可以阅读微软关于使用命令重定向运算符的文章,了解2>nul的解释。当>命令解释器在执行命令FOR之前处理此命令行时,必须在FOR命令行上使用插入字符^对重定向运算符进行转义,以便将其解释为文字字符,该命令行使用后台启动的单独命令进程执行嵌入式dir命令行。
https://stackoverflow.com/questions/48249134
复制相似问题