使用windows bat文件获取部分文件名并使用GhostScript合并多个PDF

问题描述 投票:0回答:2

我需要将多个具有相同订单号(显示在 PDF 文件名中)的 PDF 分组到一个新的 PDF 中。新分组的 PDF 应该有一个新名称=只有订单号。最好的解决方案是使用 Windows bat 文件和 GhostScript。

我写了一个脚本,但是它不起作用,我也想不通为什么。 bat 文件“grouping.bat”与 PDF 位于同一文件夹中。

@echo off
setlocal
rem Set the output directory
set "outputdir=%~dp0"
rem Get the current directory where the batch file is located
set "inputfolder=%~dp0"
rem Loop through the PDF files in the input folder
for %%F in ("%inputfolder%\*.pdf") do (
    rem Extract the file name without extension
    set "filename=%%~nF"
    rem Get characters from position 16 to 22 of the file name
    set "characters=!filename:~15,7!"
    rem Set the output file path
    set "outputfile=%outputdir%\%characters%.pdf"
    gswin64c -q -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile="%outputfile%" "%%F""
    pause
)

echo PDF files combined successfully.

endlocal

不知怎么的,它没有得到文件名的一部分。 你们能帮我找出为什么代码不起作用吗?

附:

  • win10电脑安装Ghostscript,设置在 环境(PATH)运行良好。
  • PDF 文件名的结构如下:“RAWRenderingDoc-976866-84.pdf”。
windows batch-file pdf ghostscript
2个回答
0
投票

您正在使用延迟扩展(!var! 语法)但您没有启用延迟扩展。默认延迟扩展被禁用。您必须使用以下命令启用它:

setlocal enabledelayedexpansion
并在 Magoo 评论中提到的 for 循环中使用它。


0
投票

我无法再进一步......这是新代码:

@echo on
setlocal enabledelayedexpansion
rem Set the output directory 
set "outputdir=%~dp0"
rem Get the current directory where the batch file is located
set "inputfolder=%~dp0" 
rem Loop through the PDF files in the input folder
for %%F in ("%inputfolder%\*.pdf") do (
   rem Extract the file name without extension
   set "filename=%%~nF"
   rem Get characters from position 16 to 22 of the file name
   set "characters=!filename:~16,6!"
   rem Set the output file path
   set "outputfile=%outputdir%!characters!.pdf"
   gswin64c -q -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile="!outputfile!"
)
echo PDF files combined successfully
endlocal

命令窗口正在打开并在 GS> 提示符处停止。这是命令窗口的副本:

C:\Users\804\Desktop\Ticket Master\Data>( 
rem Extract the file name without extension 
set "filename=RAWRenderingDoc-976825-47"  
rem Get characters from position 16 to 22 of the file name  
set "characters=!filename:~16,6!" 
rem Set the output file path 
set "outputfile=C:\Users\804\Desktop\Ticket Master\Data\!characters!.pdf"
gswin64c -q -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile="!outputfile!" ) 
GS> 

结果: 所以它只是打开一个 0kb 的 PDF 文件,看起来它没有在循环中填充任何 PDF。

有什么想法吗?

© www.soinside.com 2019 - 2024. All rights reserved.