You are not logged in.

#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

#2 10 Sep 2021 19:52

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: errorlevel not working, possible reset?

IF ERRORLEVEL 0 will return TRUE whether the errorlevel is 0, 1 or 5 or 64
IF ERRORLEVEL 1 will return TRUE whether the errorlevel is 1 or 5 or 64
https://ss64.com/nt/errorlevel.html

Offline

Board footer

Powered by