for /f "tokens=* delims=" %%i in (list.txt) do (
set "reported="
ping -n 3 %%i >nul
call echo %%i responded with errorlevel %%errorlevel%%
if errorlevel 2 set "reported=y"&echo errorlevel 2 actions&cd .
if errorlevel 1 set "reported=y"&echo errorlevel 1 actions&cd .
if not defined reported echo errorlevel 0 actions
)
The string "ERRORLEVEL"
will never be equal to the string "0"
. You are looking for the value of errorlevel
, which would normally be %errorlevel%
BUT since you have used a code block
(a parenthesised sequence of statements) %errorlevel%
, like any variable would be replaced by its value at the time the code block is encountered, not as it changes when executed. See Stephan's DELAYEDEXPANSION link: https://stackoverflow.com/a/30284028/2128947
You can use if errorlevel?
as shown, but you must interrogate errorlevel
in reverse-order as if errorlevel n
is true for errorlevel
being n or greater than n.
The code shown will set errorlevel
to 0
(using cd .
) after whatever actions you choose to execute so any change to errorlevel
made by those actions is overridden and will not cause any of the other errorlevel
actions to be executed. If none of the actions is executed, reported
will remain undefined, so the fact that it's undefined tells us that the original errorlevel
was 0
.