我有一个小脚本会检查文件夹中的新文件,它会打印所有PDF文件。每当打印失败时,%errorlevel%
变量将达到1.我正在尝试使用带有goto和标签的if语句来使其工作,但我无法弄清楚发生了什么。
脚本是:
:printfile
echo printing %1
set fname=%1
set fullFname="print\STW_Print\STW_Printer_TEST\%fname:~1%
echo ERLVL: %errorlevel%
"C:\Program Files (x86)\SumatraPDF\SumatraPDF.exe" -silent -print-to "\\s8011\OPS_2055_IS" %
echo ERLVL: %errorlevel%
set logFilename=%DATE:~4,2%-%DATE:~7,2%-%DATE:~10,4%
if %errorlevel% neq 0 goto error
echo Moving %fname% to printed
move %fullFname% STW_printed
echo %time% %1 Printed on STWTEST and Moved>> Printlogs\TEST-%logFilename%-log.txt
goto eof
:error
echo Moving %fname% to failed
move %fullFname% STW_failed
echo %time% %1 Failed on STWTEST and Moved>> Printlogs\TEST-%logFilename%-log.txt
GOTO eof
:eof
调用脚本的行:
for /f "tokens=*" %%i in ('dir print\STW_Print\STW_Printer_TEST\*.pdf /b') do (call :printfile "%%i")
我的输出:
printing "BADTEST (2) - Copy.pdf"
ERLVL: 0
ERLVL: 1
Moving "BADTEST (2) - Copy.pdf" to printed
1 file(s) moved.
printing "BADTEST (2).pdf"
ERLVL: 0
ERLVL: 1
Moving "BADTEST (2).pdf" to printed
1 file(s) moved.
printing "BADTEST (3) - Copy.pdf"
ERLVL: 0
ERLVL: 1
Moving "BADTEST (3) - Copy.pdf" to printed
1 file(s) moved.
我无法弄清楚为什么它不会失败。任何帮助将不胜感激!
答案 0 :(得分:1)
这是因为你在执行SET命令后检查了errorlevel ...它清除了errorlevel。将'set logfilename ...'行移动得更远。此外,使用errolevel时你可以做到
if errorlevel 1 goto :error