How to set errorlevel ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

How to set errorlevel ?

#1 Post by Ed Dyreen » 12 Nov 2011 10:21

'
currently I use this overflow technique to set the errorlevel, I wonder whether it can be shortened:

Code: Select all

>nul 2>&1 ( if !$error! neq 0 ( set /a $? = 0 ) else set /a $? = 9999999999 )
It can only switch the errorlevel between 1 and 0, I wonder if it's possible to change that, like u can do at the end of a function :?

Code: Select all

exit /b 1001
I use it for chaining macro's:

Code: Select all

>nul 2>&1 ( if !$error! neq 0 ( set /a $? = 0 ) else set /a $? = 9999999999 ) &&echo.yes ||echo.no !errorlevel!

aGerman
Expert
Posts: 4736
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to set errorlevel ?

#2 Post by aGerman » 12 Nov 2011 10:27

Try

Code: Select all

cmd /c exit 5
echo %errorlevel%


Regards
aGerman

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to set errorlevel ?

#3 Post by Ed Dyreen » 12 Nov 2011 10:28

'
isn't that like very slow ?, it's definetly short, I think I'll take it :)

aGerman
Expert
Posts: 4736
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to set errorlevel ?

#4 Post by aGerman » 12 Nov 2011 10:36

It takes some time to load another instance of cmd.exe. How fast it is depends on your machine.

Regards
aGerman

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: How to set errorlevel ?

#5 Post by Ed Dyreen » 12 Nov 2011 10:40

'
How beautifull, thanks A :wink: , where is my creativity ?

Code: Select all

C:\PROFSYS\ADMIN>cmd /c exit 1 ||echo.not
not
C:\PROFSYS\ADMIN>cmd /c exit 0 &&echo.odd ||echo.not
odd

Post Reply