批处理:合并具有相同 "filename-part" 和 pdftk 的 pdf

Batch: merge pdfs with same "filename-part" and pdftk

我想将一些 PDF 与 pdftk 合并。 要将 pdf 与 pdftk 合并,您必须使用

"pdftk file1.pdf file2.pdf output file_final.pdf"

但我必须合并很多文件名这样的 pdf

   BCCM995_KM_1.pdf
   BCCM995_KM_2.pdf
   BCCM995_KM_3.pdf
   QREM657_KM_1.pdf
   QREM657_KM_2.pdf
   QREM657_KM_3.pdf  
   QREM657_KM_4.pdf

批处理脚本应该解析文件名并合并所有以相同文件名开头的文件,例如 BCCM995_* 或 QREM657_*。

“_”是分隔符。

我有一个带有此功能的 bash 脚本,但我需要一个批处理脚本。

#!/bin/bash

for file in *_KM_1
  do
    number=$( echo $file | cut -d'_' -f1 )
    files=$(ls | grep "$number")
    echo "Found Number: $number"
    pdftk $files output output/$number.pdf
    echo "Wrote merged file to: /output/$number.pdf"
done

谁能帮我或推荐一个网站来学习批处理?

谢谢

虽然我以前不会回答至少没有提供一小段批处理文件代码的问题,但我在这种情况下做一个例外...

@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 *_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

有关任何命令的更多详细信息,请键入 help 后跟命令名称,或键入带有 /? 参数的命令;例如:for /?。特别是,您应该在此论坛上查找 "Delayed Expansion" 问题...