【发布时间】:2021-02-20 15:21:10
【问题描述】:
我正在尝试合并大量具有相似数字的 pdf 文件
例如“NR 01234567_1.pdf” “NR 01234567_2.pdf” ETC 在包含数千个类似文件页面的文件夹中,每个文件名需要合并 例如“NR 01234567.pdf”
我已经使用下面的脚本成功地处理了所有文件名中没有空格的文件(感谢这个论坛上一些非常有帮助的人),但它不适用于名称中的空格。
谁能帮我解决这个问题?
@echo off
setlocal EnableDelayedExpansion
rem Initialize (delete) "lastFile" and "fileList" variables
set "lastFile="
set "fileList="
rem Next line get the output of a "dir /B" command, that show file names *only*
rem "for /F" command execute the dir, get the output and divide each line in two "tokens" ("%%a" and "%%b")
rem with the first part before the "_" in "%%a" and the *rest* (including further "_") in "%%b"
for /F "tokens=1* delims=_" %%a in ('dir /B *.*') do (
rem If the base file name changed...
if "%%a" neq "!lastFile!" (
rem Process previous file list;
rem this "if" is just to avoid process the empty list the first time
if defined fileList (
pdftk !fileList! output !lastFile!.pdf
)
rem Reinitialize the new list
set "lastFile=%%a"
set "fileList=%%a_%%b"
) else (
rem Append this file to current list
set "fileList=!fileList! %%a_%%b"
)
)
rem Process the last list
pdftk !fileList! output !lastFile!.pdf
【问题讨论】:
-
作为最佳实践,在批处理文件中使用文件名或文件路径时应始终使用“引号”。出于同样的原因,您将它们与 IF 比较和 SET 命令一起使用。
-
Jeroen Grommen,我注意到您已经返回登录到此站点,但没有回复您官方提供的任何答案。请参阅当有人回答我的问题时我该怎么办?了解在这种情况下您可以选择哪些选项,因为没有接受答案的问题不会被本网站视为已回答。
标签: batch-file pdf merge pdftk