The requirement is: a file contains multiple line, each of which is a windows cmd. Execute one by one and provide error resolving after each command execution.
My code looks like below:
set RESPOUND=z
for /F "tokens=*" %%A in (%FILE%) do (
:REPEAT
call %%A
if NOT %ERRORLEVEL% == 0 (
:INVALID
set /p RESPOUND="Execution on %%A Failed, (R)etry, (S)kip, (A)bort:"
if %RESPOUND%=="r" GOTO REPEAT
if %RESPOUND%=="s" GOTO SKIP
if %RESPOUND%=="a" GOTO ABORT
echo %RESPOUND%
GOTO INVALID
)
:SKIP
echo test
)
:ABORT
This it turns out that unlike other coding languages, the variables are instantly be filled with preset value, so here ERRORLEVEL
is filled with 0
when the entire for
is read, making the condition always lose effect; Similarly, RESPOUND
is already set to z
, so that even I alter the previous condition and break into, the inner part does not work because all %RESPOUND%
has already be replaced by z
before then.
Additionally, if the echo test
line is removed, the batch will report unexpected ) in syntax
error, which also confusing me.
So how to change the code to make it work?