将pdf与PDFTK和相同的起始文件名合并

问题描述 投票:1回答:1

我正在尝试使用pdftk在目录中搜索相似的文件名并将其组合。

[我发现了Aacini编写的精彩脚本,当以NAME_KM_1,NAME_KM_2,NAME_KM_3等格式使用时,效果很好。它将所有带有KM的文件合并为pdf。

在我的情况下,我的文件名都以类似Q1111_CONFIRM,Q1111_ORDER,Q2222_CONFIRM,Q2222_ORDER的名称开头。我将如何修改此订单以结合符合Qnumber的订单/确认?我尝试了几种不同的方法,但均未成功。

@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 f 
urther "_") in "%%b"

for /F "tokens=1* delims=_" %%a in ('dir /B *_KM_*.*') 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
batch-file
1个回答
0
投票
@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
© www.soinside.com 2019 - 2024. All rights reserved.