【问题标题】:Batch: merge pdfs with same "filename-part" and pdftk批处理:合并具有相同“文件名部分”和 pdftk 的 pdf
【发布时间】:2016-03-10 11:12:40
【问题描述】:

我想将一些 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

有人可以帮我或推荐一个网站来学习批处理吗?

谢谢

【问题讨论】:

  • 你可以使用pdftk *.pdf cat ouput.pdf

标签: batch-file pdf merge pdftk


【解决方案1】:

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

@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 /?。特别是,您应该在此论坛上查找“延迟扩展”问题...

【讨论】:

  • 哇,非常感谢!它工作得很好,有超过 10 个 pdf 的“数据集”有一点问题,但我想我可以解决这个问题。做得好!祝你有美好的一天
猜你喜欢
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 2017-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多