批处理脚本以使用内容重命名PDF文件 [英] Batch script to rename PDF files using the content

查看:54
本文介绍了批处理脚本以使用内容重命名PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a batch script used to extract PDF information and rename the PDF then.

该脚本可以很好地处理1个PDF文件,但我需要直接在包含大量PDF文件的文件夹中使用它.

The script is working fine for 1 PDF file but I need to use it directly in a folder with a lot of PDF files.

那怎么办?

需要为每个PDF文件逐个运行脚本,直到最后.一旦将PDF文件重命名,则该文件将移动到另一个文件夹中,因此保留在该文件夹中的PDF文件也需要相同的内容.当文件夹为空时,脚本退出.

The script needs to run for each PDF file, one by one until the end. Once the PDF file is renamed next the file is moved in another folder, so the PDF file remaining in the folder need the same thing. And when the folder is empty the script exits.

@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 FOLDER 中,并根据内容确定新文件名每个PDF文件.

Perhaps this commented batch file code works for moving all PDF files in current directory to subdirectory XYZ FOLDER with new file name determined from contents of each PDF file.

@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 命令就足够了.

It is unclear for me why each *.pdf file should be moved into a subdirectory with new file name.
This is not really necessary in my point of view. The command REN could be enough.

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

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • 呼叫/?
  • del/?
  • dir/?
  • echo/?
  • endlocal/?
  • 找到/?
  • 用于/?
  • 转到/?
  • md/?
  • 移动/?
  • rem/?
  • ren/?
  • 设置/?
  • setlocal/?
  • call /?
  • del /?
  • dir /?
  • echo /?
  • endlocal /?
  • find /?
  • for /?
  • goto /?
  • md /?
  • move /?
  • rem /?
  • ren /?
  • set /?
  • setlocal /?

另请阅读有关使用命令重定向操作符的Microsoft文章. 2> nul 的说明.Windows命令解释器处理此命令时,重定向操作符> 必须在 FOR 命令行上使用脱字符号 ^ 进行转义,才能将其解释为原义字符.执行命令 FOR 之前的一行,该命令在后台启动的单独命令过程中执行嵌入式 dir 命令行.

Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line in a separate command process started in background.

另请参见 GOTO:EOF返回哪里?

这篇关于批处理脚本以使用内容重命名PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆