获取Windows批处理文件的文件名部分,并使用GhostScript合并多个PDF文件。

huangapple go评论63阅读模式
英文:

Get part of the Filename with windows bat file and merge multiple PDFs with GhostScript

问题

I need to group multiple PDFs with the same order number (which is shown in the PDF filenames) into a new PDF. The new grouped PDF should have a new name= only the order number. The best solution is using a Windows bat file and GhostScript.

我需要将具有相同订单号的多个PDF文件(订单号在PDF文件名中显示)分组到一个新的PDF中。新的分组PDF应该以订单号命名。最佳解决方案是使用Windows批处理文件和GhostScript。

I have written a script, but it doesn't work, and I can't figure out why. The bat file "grouping.bat" is in the same folder as the PDFs.

我编写了一个脚本,但它不起作用,我无法弄清楚原因。批处理文件“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

Somehow it doesn't get the part of the file name. Can you guys help me find out why the code is not working?

不知何故它没有获取文件名的一部分。你们能帮我找出代码为何不起作用吗?

ps:

  • Ghostscript is installed on the win10 computer and settings in the environment (PATH) are working well.
  • The PDF file names are structured as follows: "RAWRenderingDoc-976866-84.pdf".

附注:

  • Ghostscript已安装在Win10计算机上,并且环境中的设置(PATH)正常工作。
  • PDF文件名的结构如下:“RAWRenderingDoc-976866-84.pdf”。
英文:

I need to group multiple PDFs with the same order number (which is shown in the PDF filenames) into a new PDF. The new grouped PDF should have a new name= only the order number. The best solution is using a Windows bat file and GhostScript.

I have written a script, but it doesn't work, and I can't figure out why.
The bat file "grouping.bat" is in the same folder as the PDFs.

@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

Somehow it doesn't get the part of the file name.
Can you guys help me find out why the code is not working?

ps:

  • Ghostscript is installed on the win10 computer and settings in the
    environment (PATH) are working well.
  • The PDF file names are structured as follows: "RAWRenderingDoc-976866-84.pdf".

答案1

得分: 0

你正在使用延迟扩展(!var! 语法),但你没有启用延迟扩展的使用。默认延迟扩展已禁用。你需要使用以下命令启用它:setlocal enabledelayedexpansion 并在 for 循环中使用,如 Magoo 在注释中提到的。

英文:

You are using delayed expansion (the !var! syntax) but you did not enable the use of delayed epansion. Default delayed expansion is disabled. You have to enable this with the command: setlocal enabledelayedexpansion and use it inside the for-loop as mentioned in the comments by Magoo.

答案2

得分: 0

以下是您要求的翻译好的内容:

在DOS时代的编程问题中,与现在一样,Windows操作系统的限制是一个问题。

在1980年代的DOS中,通过递归来循环执行批处理文件以绕过这些问题是常见的,因此按今天的标准可能有点不太规范,但已经被老派现代化了,所以可以随意更新我的尝试。在给定OP参数的情况下,它可以工作,但代码结构较为混乱,下面有更简洁的版本。

请注意,以下是现代化后的一行方法:

@echo off & Title "新生成的PDF组"
REM 合并永远不是"100%合并",这需要一个更复杂的编辑器来重新编号一些内部PDF对象
setlocal EnableDelayedExpansion
set "GSexe=C:\Apps\PDF\GS\gs10011w64\bin\gswin64c.exe"

    REM 获取输入集,本例中是批处理文件所在的位置(注意%~dp0后面有\)
set "inSet=%~dp0RAW*.pdf"

    REM 设置输出目录,可以不同(注意末尾的\)
set "outputdir=%~dp0"

    REM 从文件名中获取位置16到22的字符,不包括扩展名
    REM 在这种情况下,txt文件将是上述七个字符,如-######.lst!!!
    REM 清除任何以前的运行或潜在的临时恶意文件
if exist "%temp%\-*.lst" del "%temp%\-*.lst"
echo 正在循环遍历输入文件夹中的PDF文件...
  for %%F in ("%inSet%") do (
    set "filename=%%~nF"
    set "characters=!filename:~15,7!"
    echo "%%F" >>"%temp%\!characters!.lst"
)
echo 从列表生成新的集合
  for %%F in ("%temp%\-*.lst") do ("%GSexe%" -q -sDEVICE=pdfwrite -o "%outputdir%%%~nF.pdf" "@%%F")
echo PDF文件成功合并。
  pause
del "%temp%\-*.lst"
英文:

As a Programming issue in the Days of DOS this Windows OS limitation is just as much a problem as then.

It was common in 1980s DOS to cycle bat files by recursion to bypass such problems, so this is perhaps dirty by todays standards but old school modernised so feel free to update my attempt. IT WORKS given the OP parameters, but is spaghetti hoops so see lower

Do not use this at home.bat simple modification to show poorly how it may have been done avoiding expansion problems by total recall (where arguments could have negated the delay expansion issues)

:@echo off
if %1. equ ReCurse. goto recurse
rem Get the current directory where the batch file is located
set "inputfolder=%~dp0"
rem Set the output directory
set "outputdir=%~dp0"
goto main

:recurse
echo Looping through the PDF files in the input folder...
set "_#1=%~2"
set "_#2=%_#1:~15,7%"
echo "%~2.pdf" >>"%inputfolder%\%_#2%.txt"
goto eof

:main
del "%outputdir%-*.txt
echo Collating lists
for %%F in ("%inputfolder%RAW*.pdf") do call %~0 ReCurse "%%~nF"
echo Merging lists
for %%F in ("%inputfolder%-*.txt") do "gswin64c.exe" -q -sDEVICE=pdfwrite -o "%%~dpnF.pdf" "@%%F"
echo PDF files combined successfully.
pause

:eof

Modern cleaner one line method

@echo off & Title "Newly printed PDF groups"
REM Combining is Never "Merging 100%" which needs a considerably more complex editor to renumber some internal PDF objects
setlocal EnableDelayedExpansion
set "GSexe=C:\Apps\PDF\GS\gs10011w64\bin\gswin64c.exe"

    REM Get the input set in this case where the batch file is located (beware %~dp0 has trailing \)
set "inSet=%~dp0RAW*.pdf"

    REM Set the output directory as can be different (beware trailing \)
set "outputdir=%~dp0"

    REM Get characters from position 16 to 22 of the file name without extension
    REM txt files will in this case be seven chars from above as -######.lst !!!
    REM clear any previous run or potential temporary rogue file
if exist "%temp%\-*.lst" del "%temp%\-*.lst"
echo Looping through the PDF files in the input folder...
  for %%F in ("%inSet%") do (
    set "filename=%%~nF"
    set "characters=!filename:~15,7!"
    echo "%%F" >>"%temp%\!characters!.lst"
)
echo Generating new collectives from lists
  for %%F in ("%temp%\-*.lst") do ("%GSexe%" -q -sDEVICE=pdfwrite -o "%outputdir%%%~nF.pdf" "@%%F")
echo PDF files combined successfully.
  pause
del "%temp%\-*.lst"

huangapple
  • 本文由 发表于 2023年5月17日 14:37:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定