在Windows批处理文件中设置错误级别 [英] Set errorlevel in Windows batch file
问题描述
我正在编写一个批处理脚本,该脚本将遍历文本文件的每一行(每行包含文件名),检查该文件是否存在,然后运行该文件并将其移动.
I am writing a batch script that will loop through each line of a text file, (each line containing a filename) check if the file exists and then runs the file and moves it.
这是我的批处理脚本:
REM Loop through each line of input.txt
FOR /F "tokens=1-3 delims=, " %%i IN (./ready/input.txt) DO (
ECHO.
ECHO.
ECHO.
ECHO Check %%i exists, set error flag if it doesnt
if not exist .\ready\%%i set errorlevel=2
echo return code is %errorlevel%
ECHO Run %%i if it exists
if errorlevel 0 call .\ready\%%i
ECHO Move %%i to archive if no error occured
if errorlevel 0 copy .\ready\%%i .\archive\%mydate%_%mytime%_%%j_%%k_%%i
ECHO Copy line of text to the new output.txt file if an error occured
if %errorlevel% NEQ 0 >>output.txt %%i, %%j, %%k
)
以下是输出:
我不明白为什么如果错误级别"不能按预期方式工作...如果文件不存在(如本例中不存在的文件),则不应尝试运行文件,也不应复制文件,它应该回显2而不是0
I do not understand why the "if errorlevel" is not working as expected... if the file does not exist (as in this example where it does not exist) it should NOT try to run the file, it should NOT copy the file, and it should echo a 2 not a 0
编辑1 :我正在阅读另一个我不确定是否与该问题有关
Edit 1: I was reading another SO Post regarding "delayed environment variable expansion" I am not sure if this issue is related
推荐答案
@ECHO OFF
SETLOCAL
DEL output.txt 2>nul
REM Loop through each line of input.txt
FOR /F "tokens=1-3 delims=, " %%i IN (.\ready\input.txt) DO (
ECHO.
ECHO.
ECHO.
ECHO Check %%i exists, set error flag if it doesnt
if exist .\ready\%%i (set "errorflag=") ELSE (set errorflag=2)
CALL echo return code is %%errorflag%%
ECHO Run %%i if it exists
if NOT DEFINED errorflag (
call .\ready\%%i
ECHO Move %%i to archive if no error occured
if errorlevel 1 (SET errorflag=3) ELSE (ECHO copy .\ready\%%i .\archive\%mydate%_%mytime%_%%j_%%k_%%i)
)
ECHO Copy line of text to the new output.txt file if an error occured
if DEFINED errorflag >>output.txt ECHO %%i, %%j, %%k
)
GOTO :EOF
这是一个重写的过程.
注意:output.txt
会在开始时删除,否则>>
会附加到任何现有文件中. 2>nul
如果删除失败(例如文件不存在),则禁止显示错误消息
Note: output.txt
is deleted at the start, else the >>
would append to any existing file. 2>nul
suppresses error messages if the delete fails (eg. file not exist)
在块语句(a parenthesised series of statements)
中,将解析ENTIRE块并执行 THEN .在执行该块之前,该块中的所有%var%
都会在变量被填充时被该变量的值替换.
Within a block statement (a parenthesised series of statements)
, the ENTIRE block is parsed and THEN executed. Any %var%
within the block will be replaced by that variable's value AT THE TIME THE BLOCK IS PARSED - before the block is executed.
因此,IF (something) else (somethingelse)
将在遇到IF
时使用%variables%
的值执行.
Hence, IF (something) else (somethingelse)
will be executed using the values of %variables%
at the time the IF
is encountered.
克服此问题的两种常见方法是:1)使用setlocal enabledelayedexpansion
并使用!var!
代替%var%
来访问var
的变更值,或2)调用子例程以使用更改值.
Two common ways to overcome this are 1) to use setlocal enabledelayedexpansion
and use !var!
in place of %var%
to access the chnaged value of var
or 2) to call a subroutine to perform further processing using the changed values.
因此请注意使用CALL ECHO %%var%%
来显示更改后的var
值.显示CALL ECHO %%errorlevel%%
,但遗憾的是会显示RESETS错误级别.
Note therefore the use of CALL ECHO %%var%%
which displays the changed value of var
. CALL ECHO %%errorlevel%%
displays, but sadly then RESETS errorlevel.
IF DEFINED var
为真.
ERRORLEVEL
是一个特殊的变量名称.它是由系统设置的,但是如果由用户设置,则用户分配的值将覆盖系统值.
ERRORLEVEL
is a special varable name. It is set by the system, but if set by the user, the user-assigned value overrides the system value.
IF ERRORLEVEL n
为TRUE.因此,IF ERRORLEVEL 0
始终为真.
IF ERRORLEVEL n
is TRUE if errorlevel
is n OR GREATER THAN n. IF ERRORLEVEL 0
is therefore always true.
语法SET "var=value"
(值可能为空)用于确保分配的值中不包含行尾的任何空格.
The syntax SET "var=value"
(where value may be empty) is used to ensure that any stray spaces at the end of a line are NOT included in the value assigned.
所需的命令仅出于测试目的而ECHO
.确认命令正确无误后,将ECHO COPY
更改为COPY
即可实际复制文件.
The required commands are merely ECHO
ed for testing purposes. After you've verified that the commands are correct, change ECHO COPY
to COPY
to actually copy the files.
我使用了以下input.txt
:
seterr1.bat, J1, K1
seterr5.bat,J2,K2
seterr0.bat,J3 K3
seterr5.bat, J4, K4
notexist.bat, J5, K5
包含现有文件seterr*.bat
@ECHO OFF
EXIT /b 1
(最后一行中的1
确定返回的errorlevel
)
(where the 1
in the last line determines the errorlevel
returned)
并收到结果输出:
Check seterr1.bat exists, set error flag if it doesnt
return code is
Run seterr1.bat if it exists
Move seterr1.bat to archive if no error occured
Copy line of text to the new output.txt file if an error occured
Check seterr5.bat exists, set error flag if it doesnt
return code is
Run seterr5.bat if it exists
Move seterr5.bat to archive if no error occured
Copy line of text to the new output.txt file if an error occured
Check seterr0.bat exists, set error flag if it doesnt
return code is
Run seterr0.bat if it exists
Move seterr0.bat to archive if no error occured
copy .\ready\seterr0.bat .\archive\__J3_K3_seterr0.bat
Copy line of text to the new output.txt file if an error occured
Check seterr5.bat exists, set error flag if it doesnt
return code is
Run seterr5.bat if it exists
Move seterr5.bat to archive if no error occured
Copy line of text to the new output.txt file if an error occured
Check notexist.bat exists, set error flag if it doesnt
return code is 2
Run notexist.bat if it exists
Copy line of text to the new output.txt file if an error occured
请注意,就像我前面提到的那样,仅复制了ECHO
.
Note that the COPY is merely ECHO
ed as I mentioned earlier.
和output.txt
and output.txt
seterr1.bat, J1, K1
seterr5.bat, J2, K2
seterr5.bat, J4, K4
notexist.bat, J5, K5
这篇关于在Windows批处理文件中设置错误级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!