#1 07 May 2021 14:12

Registered: 07 May 2019
Posts: 14

errorlevel not working, possible reset?

I can't seem to get errorlevel to work correctly with this.  I think I know why but I'm still unsure of how to get it to work.

The part that's failing is the errorlevel.  When I check errorlevel, it's always 0, even when the "where" command fails and should be setting it to 1.  This causes the else condition to fail.  I suspect it's because the for loop ends and errorlevel is reset to 0.

@echo off

for /f "tokens=* usebackq" %%F in (`where /F notepad2.exe`) do (set "_notepad=%%F")
if %errorlevel% equ 0 (goto foundnotepad) else (goto nonotepad)

echo Found %_notepad%
goto end

echo ERROR: Unable to locate %%F
goto abort

echo Aborting!!
goto end


I'm saving the file as test2.cmd under c:\temp and here are the results:

INFO: Could not find files for the given pattern(s).

C:\temp>echo %errorlevel%

... On a side note, is there a way to suppress the "INFO: Could not find files for the given pattern(s)."?


Board footer

Powered by FluxBB