Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!
Credits 38 Posts 15 Joined 2007-10-13 12:20 18-year member UID 99609 Gender Male
Status Offline
1: The single SET command can view the system environment variables. errorlevel is also a system variable, so why can't I see it?
2: Can set errorlevel=n change the value of errorlevel? For example:
set errorlevel=5
echo %errorlevel%
The result shows 5
But if errorlevel 5 echo haha
doesn't display anything at all. Why?
I'm confused, and hope the experts won't hesitate to enlighten me. Thanks!
Credits 6,962 Posts 2,753 Joined 2003-04-16 00:00 23-year member UID 1565 Gender Male From 河北保定
Status Offline
if %errorlevel%==5 echo ok
errorlevel is an implicit dynamic environment variable, just like %date% and %cd%. Also, its value should be set automatically by the system. Although you can manually set its value with the SET command, that only means you defined an environment variable named errorlevel yourself.
Credits 6,962 Posts 2,753 Joined 2003-04-16 00:00 23-year member UID 1565 Gender Male From 河北保定
Status Offline
After you manually set an environment variable named errorlevel, the system variable with the same name stops working. That means that when you access it with %errorlevel%, you only get the value you defined yourself, while at that point the system's errorlevel can only be accessed with if errorlevel.
F:\WORK\DOS>set errorlevel=
F:\WORK\DOS>echo %errorlevel%
1
F:\WORK\DOS>set errorlevel=a
F:\WORK\DOS>echo %errorlevel%
a
F:\WORK\DOS>echo %errorlevel% | find /i "a"
a
F:\WORK\DOS>if errorlevel 1 echo not found.
F:\WORK\DOS>if errorlevel 0 echo found.
found.
F:\WORK\DOS>if %errorlevel%==0 echo found.
F:\WORK\DOS>echo %errorlevel% | find /i "b"
F:\WORK\DOS>if errorlevel 1 echo not found.
not found.