China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

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!

中国DOS联盟论坛
The time now is 2026-08-27 09:02
中国DOS联盟论坛 » 其它操作系统综合讨论区 » Can SET change the value of errorlevel? View 808 Replies 3
Original Poster Posted 2007-10-25 19:51 ·  中国 山东 济南 教育网
初级用户
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!
Floor 2 Posted 2007-10-26 16:01 ·  中国 河北 保定 联通
铂金会员
★★★★
网络独行侠
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.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 3 Posted 2007-10-26 16:08 ·  中国 河北 保定 联通
铂金会员
★★★★
网络独行侠
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.

F:\WORK\DOS>if errorlevel 0 echo found.
found.

F:\WORK\DOS>if %errorlevel%==1 echo found.

F:\WORK\DOS>set errorlevel=

F:\WORK\DOS>if %errorlevel%==1 echo found.
found.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 4 Posted 2007-10-26 18:41 ·  中国 山东 济南 教育网
初级用户
Credits 38
Posts 15
Joined 2007-10-13 12:20
18-year member
UID 99609
Gender Male
Status Offline
Thanks to both of you for your answers, I understand now.
Forum Jump: