使用 BAT 文件合并具有通用文件名的 PDF

使用 BAT 文件合并具有通用文件名的 PDF

我正在使用 BAT 文件尝试自动合并具有相似文件名的 PDF。我对此还不太熟悉,我想我可能设置了错误的文件位置?我收到:环境变量“x”未定义。我从另一篇文章中了解到这一点,非常感谢任何帮助:

echo off
setlocal enabledelayedexpansion

rem source root folder where to crawl pdfs.
set F:\Sabine_To_Galveston_Project\ZZ_File_Transform\154482.0

rem destination folder
set F:\Sabine_To_Galveston_Project\ZZ_File_Transform\154482.0\Merged

    for /f "delims=" %%a in ('dir /b /s /ad /o:n "%source%"') do (
        set _pdffiles=

        for /f "delims=" %%i in ('dir /b /a-d /o:n "%%a\*.pdf"') do (
            set _pdffiles=!_pdffiles! "%%i"
            set "_outputpdf=%%~ni"
        )

        echo pdftk.exe !_pdffiles! cat output "%destination%\!_outputpdf:~0,6!.pdf"
    )

答案1

你试一试:

@echo off && setlocal enabledelayedexpansion

rem :: set ful path\file.exe
set "_pdftk=\The_\Full_\Path_\To_\File_\pdftk.exe"

rem :: goto your folder:
cd /d "F:\Sabine_To_Galveston_Project\ZZ_File_Transform\154482.0"

rem  :: set traget file directory
set "_desti=F:\Sabine_To_Galveston_Project\ZZ_File_Transform\154482.0\Merged"

rem :: run in your folder  
for /f tokens^=* %%i in ('echo\"!cd!"')do set "_n=!_desti!\%%~nxi" && ^
    call "!_pdftk!" "%%~dpnxi\*.pdf" cat output "!_n:~0,-6!.pdf"

rem :: run in all subfolder 
for /r /d %%i in (*)do if /i not "%%~nxi"=="Merged" 2>nul (
    set "_n=!_desti!\%%~nxi" && call "!_pdftk!" "%%~dpnxi\*.pdf" cat output "!_n:~0,-6!.pdf"
   )
   
endlocal & goto=:EOF

答案2

您实际上并没有设置两个变量,请使用这个:

@echo off
setlocal enabledelayedexpansion

rem source root folder where to crawl pdfs.
set "source=F:\Sabine_To_Galveston_Project\ZZ_File_Transform\154482.0"

rem destination folder
set "destination=F:\Sabine_To_Galveston_Project\ZZ_File_Transform\154482.0\Merged"

for /f "delims=" %%a in ('dir /b /s /ad /o:n "%source%"') do (
set _pdffiles=
for /f "delims=" %%i in ('dir /b /a-d /o:n "%%a\*.pdf"') do (
    set _pdffiles=!_pdffiles! "%%i"
    set "_outputpdf=%%~ni"
)
echo pdftk.exe !_pdffiles! cat output "%destination%\!_outputpdf:~0,6!.pdf"
)

相关内容