如果存在在批处理文件中不再起作用

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

我有一个批次的问题。它会扫描文件夹中的新pdf文件,打印,移动和删除它们。它工作正常,直到突然停止没有错误。如果我手动将其键入cmd,它会调用“文件路径不存在”,但这是正确的路径。我没有任何线索,也许有些人可以帮忙或者有同样的问题。

预先感谢您的帮助。

@echo off

:pdfprint

echo Checkin Druck - bitte offen lassen

IF EXIST *.pdf for %%p in ("C:\Users\Public\Documents\Lexware\bueroeasy\Daten\eRechnung\signed\*.pdf") do ( start /b "Print" "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" /n /t "%%p"

ping 127.0.0.1 -n 10

%windir%\system32\taskkill.exe /F /IM AcroRd32.exe

ping 127.0.0.1 -n 5

xcopy "C:\Users\Public\Documents\Lexware\bueroeasy\Daten\eRechnung\signed\*.pdf" "C:\Users\Textilpflege\Desktop\backup\"
ping 127.0.0.1 -n 2
move "%%p" "C:\Users\Textilpflege\Documents\Belegtransfer\9860-11206\Rechnungsausgang\"
IF EXIST *.pdf for %%p in ("C:\Users\Public\Documents\Lexware\bueroeasy\Daten\eRechnung\signed\*.pdf") do DEL *.pdf

)
IF EXIST *.pdf for %%p in ("C:\Users\Public\Documents\Lexware\bueroeasy\Daten\Poststelle\*.pdf") do ( start /b "Print" "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" /n /t "%%p"

ping 127.0.0.1 -n 10

%windir%\system32\taskkill.exe /F /IM AcroRd32.exe

ping 127.0.0.1 -n 5
xcopy "C:\Users\Public\Documents\Lexware\bueroeasy\Daten\Poststelle\*.pdf" "C:\Users\Textilpflege\Desktop\backup\"
ping 127.0.0.1 -n 2
move "%%p" "C:\Users\Textilpflege\Documents\Belegtransfer\9860-11206\Rechnungsausgang\"
IF EXIST *.pdf for %%p in ("C:\Users\Public\Documents\Lexware\bueroeasy\Daten\Poststelle\*.pdf") do DEL *.pdf

)

goto :pdfprint
windows batch-file if-statement cmd exists
1个回答
1
投票

让我通过一些更改来修改脚本。首先,我不确定为什么要检查文件是否存在于本地,然后在另一个目录上执行删除操作。另外,让我们为ping扫出timeout

@echo off
:pdfprint
echo Checkin Druck - bitte offen lassen

for %%a in ("C:\Users\Public\Documents\Lexware\bueroeasy\Daten\eRechnung\signed\*.pdf") do (
    start /b "Print" "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" /n /t "%%a"
    timeout 10
    %windir%\system32\taskkill.exe /F /IM AcroRd32.exe
    timeout 5
    xcopy "C:\Users\Public\Documents\Lexware\bueroeasy\Daten\eRechnung\signed\*.pdf" "C:\Users\Textilpflege\Desktop\backup\"
    timeout 2
    move "%%a" "C:\Users\Textilpflege\Documents\Belegtransfer\9860-11206\Rechnungsausgang\"
    del "C:\Users\Public\Documents\Lexware\bueroeasy\Daten\eRechnung\signed\*.pdf"
 )
for %%d in ("C:\Users\Public\Documents\Lexware\bueroeasy\Daten\Poststelle\*.pdf") do (
    start /b "Print" "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" /n /t "%%d"
    timeout 5
    %windir%\system32\taskkill.exe /F /IM AcroRd32.exe
    timeout 5
    xcopy "C:\Users\Public\Documents\Lexware\bueroeasy\Daten\Poststelle\*.pdf" "C:\Users\Textilpflege\Desktop\backup\"
    timeout 2
    move "%%d" "C:\Users\Textilpflege\Documents\Belegtransfer\9860-11206\Rechnungsausgang\"
    del "C:\Users\Public\Documents\Lexware\bueroeasy\Daten\Poststelle\*.pdf"
 )
goto :pdfprint

问题!

  1. 对于不需要的循环来说太多了。你做for %%p in (path\to\*.pdf) del *.pdf如此有效地说,对于每个pdf,删除所有pdf的。只需做一个没有不需要的for循环的del path\*.pdf
  2. 你将相同的标记值%%p分配给2 for循环,相反,我添加了%%a%%d
  3. if exists语句是无用的,因为您检查本地是否存在* .pdf,但在另一个文件夹中删除。
© www.soinside.com 2019 - 2024. All rights reserved.