这是我的批处理脚本:
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 )
这是输出:
我不明白为什么“if errorlevel”不能像预期的那样工作…如果该文件不存在(如本例中不存在),则不应该尝试运行该文件,它不应该复制该文件,它应该回应2不是0
编辑1:我正在读另一个SO Post关于“延迟的环境变量扩展”我不知道这个问题是否相关
@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,%%k ) GOTO :EOF
这是一个重写的程序.
注意:output.txt在开始时被删除,否则>>将附加到任何现有文件. 2> nul如果删除失败(如文件不存在),则会抑制错误消息
在块语句(括号内的一系列语句)中,ENTIRE块被解析,然后执行.块内的任何%var%将被该变量的值替换为块被分配的时间 – 块执行前.
因此,IF(something)else(somethingelse)将在遇到IF时使用%variables%的值执行.
克服这个的两个常见方法是1)使用setlocal enabledelayedexpansion并使用!var!代替%var%来访问var的chnaged值或2)调用子例程以使用更改的值执行进一步处理.
因此,请注意使用CALL ECHO %% var %%显示更改的var值. CALL ECHO %% errorlevel %%显示,但遗憾的是RESETS错误级别.
IF DEFINED如果var为CURRENTLY定义,则var为true.
ERRORLEVEL是一个特殊的可变名称.它由系统设置,但如果由用户设置,则用户分配的值将覆盖系统值.
IF ERRORLEVEL n如果errorlevel为n或大于n,则为TRUE.因此,如果ERRORLEVEL 0始终为真.
语法SET“var = value”(其中值可能为空)用于确保行末尾的任何杂散空间不包括在分配的值中.
所需的命令仅用于ECHOed用于测试目的.验证命令正确后,将ECHO COPY更改为COPY以实际复制文件.
我使用了以下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确定返回的错误级别)
并收到结果输出:
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
请注意,如前所述,COPY仅仅是ECHOed.
和output.txt
seterr1.bat,K2 seterr5.bat,K5