如何修复批处理代码中的 if-else 语句以成功运行?

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

我有一个批处理脚本,可以合并两个单独文件夹中的两个 pdf,将它们放入临时文件夹中供我审核,然后将它们移动到公司范围的文件夹目录中。我想编辑此脚本以转储文件夹 1 中的 pdf,无论文件夹 2 中是否有 pdf。这是我的原始代码,效果完美。我知道它可能看起来很奇怪,但它是故意的(就像目标变量与 gisfolder 变量相同。我根本不想更改它。我只想添加一个“if else”语句。

@echo off
setlocal enabledelayedexpansion

set "pdftk=C:\Program Files (x86)\PDFtk\bin\pdftk.exe"

set "sourcemap=C:\GIS\Maps\Ranch Map Staging Folders\PCA\Central"
set "sourcetables=C:\GIS\Maps\Ranch Map Staging Folders\PCA\Photos"
set "destination=C:\GIS\Maps\Ranch Map Staging Folders\PCA\Central Completed"

set pdffile=
set pdfattach=
for /f "delims=" %%i in ('dir /b /a-d /o:n "%sourcemap%\*.pdf"') do (
    set "pdffile=%sourcemap%\%%i"
    set "pdfattach=%sourcetables%\PestManagement_TableSeries_!pdffile:~50!"
    set "outputpdf=%%~ni"
    "C:\Program Files (x86)\PDFtk\bin\pdftk.exe" "!pdffile!" "!pdfattach!" cat output "%destination%\!pdffile:~50,-4! - Field Scout Survey Map %date:~-4,4%%date:~-10,2%%date:~-7,2%.pdf"
    echo "Successfully completed !pdffile:~50,-4!"
)

set "gisfolder=C:\GIS\Maps\Ranch Map Staging Folders\PCA\Central Completed"
set "spfolder=\\sunpacific.local\lerdodata\Sun Pacific Public\6 - Business Units\Sun Pacific Farming Cooperative\B - General\10 - Maps\Ranch Maps\Other Maps\PCA\Maps\Central"
set "archivefolder=\\sunpacific.local\lerdodata\Sun Pacific Public\6 - Business Units\Sun Pacific Farming Cooperative\B - General\10 - Maps\Other Maps\PCA\Old Maps\Central"

move "%spfolder%\*.pdf" "%archivefolder%"

move "%gisfolder%\*.pdf" "%spfolder%"

cmd /k

这是我正在努力解决的代码。我知道这很简单,但我不是批处理编程的本地人,每次运行它时,我都不会收到任何错误,命令窗口会弹出并在我可以执行任何操作之前关闭。两个代码块之间的唯一变化是“if else”语句。感谢您提供的任何帮助。

@echo off
setlocal enabledelayedexpansion

set "pdftk=C:\Program Files (x86)\PDFtk\bin\pdftk.exe"

set "sourcemap=C:\GIS\Maps\Ranch Map Staging Folders\PCA\Central"
set "sourcetables=C:\GIS\Maps\Ranch Map Staging Folders\PCA\Photos"
set "destination=C:\GIS\Maps\Ranch Map Staging Folders\PCA\Central Completed"

set pdffile=
set pdfattach=
for /f "delims=" %%i in ('dir /b /a-d /o:n "%sourcemap%\*.pdf"') do (
    set "pdffile=%sourcemap%\%%i"
    set "pdfattach=%sourcetables%\PestManagement_TableSeries_!pdffile:~50!"
    set "outputpdf=%%~ni"
    if exist "!pdfattach!" (
        "C:\Program Files (x86)\PDFtk\bin\pdftk.exe" "!pdffile!" "!pdfattach!" cat output "%destination%\!pdffile:~50,-4! - Field Scout Survey Map %date:~-4,4%%date:~-10,2%%date:~-7,2%.pdf"
    )else (
        move "!pdffile!" "%destination%\!pdffile:~50,-4! - Field Scout Survey Map %date:~-4,4%%date:~-10,2%%date:~-7,2%.pdf"
        echo "Successfully completed !pdffile:~50,-4!"
        )

set "gisfolder=C:\GIS\Maps\Ranch Map Staging Folders\PCA\Central Completed"
set "spfolder=\\sunpacific.local\lerdodata\Sun Pacific Public\6 - Business Units\Sun Pacific Farming Cooperative\B - General\10 - Maps\Ranch Maps\Other Maps\PCA\Maps\Central"
set "archivefolder=\\sunpacific.local\lerdodata\Sun Pacific Public\6 - Business Units\Sun Pacific Farming Cooperative\B - General\10 - Maps\Other Maps\PCA\Old Maps\Central"

move "%spfolder%\*.pdf" "%archivefolder%"

move "%gisfolder%\*.pdf" "%spfolder%"

cmd /k
batch-file pdf merge
1个回答
0
投票

我会将

"C:\Program Files (x86)\…
更改为
"C:\Program Files (x86^)\…
,因为我怀疑未转义的
)
正在终止
if
子句。

我还希望在

)
else
之间添加一个空格。
)else
看起来可行,但很丑。

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