#1 07 May 2021 14:12

kteague
Member
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)

:foundnotepad
echo Found %_notepad%
goto end

:nonotepad
echo ERROR: Unable to locate %%F
goto abort

:abort
echo Aborting!!
goto end

:end
echo.

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

C:\temp>test2
INFO: Could not find files for the given pattern(s).
Found


C:\temp>echo %errorlevel%
0


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

Offline

Board footer

Powered by FluxBB