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-07-01 22:33
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Can a subroutine I write myself return an errorlevel? View 746 Replies 3
Original Poster Posted 2007-04-25 04:42 ·  中国 北京 电信
初级用户
Credits 22
Posts 8
Joined 2007-04-21 04:11
19-year member
UID 85925
Gender Male
Status Offline
I tried it, and directly using set errorlevel doesn't work

echo %errorlevel% // outputs 0
call :sub
echo %errorlevel% // outputs 1
if errorlevel 1 echo errorlevel=1 // the test here doesn't work
if errorlevel 0 echo errorlevel=0 // errorlevel is still 0, :(

pause
exit /b

:sub
set /a errorlevel=1
goto :EOF

[ Last edited by bright2k on 2007-4-24 at 03:44 PM ]
Floor 2 Posted 2007-04-25 04:47 ·  中国 广东 广州 白云区 电信
高级用户
★★★
潜水修练批处理
Credits 788
Posts 366
Joined 2006-12-31 02:43
19-year member
UID 75048
Gender Male
Status Offline
errorlevel is a special environment variable, so writing it like this won't work. Wouldn't it be fine to use some other variable?

call :sub
if "%error%" equ "1" echo error=1
pause
exit /b

:sub
set /a error=1
goto :EOF
Floor 3 Posted 2007-04-25 08:15 ·  中国 北京 电信
初级用户
Credits 22
Posts 8
Joined 2007-04-21 04:11
19-year member
UID 85925
Gender Male
Status Offline
I found that you can solve it by using exit /b 1 in the subroutine, no need for goto :EOF
:sub
exit /b 1


At least there's no problem under XP
Floor 4 Posted 2007-04-25 09:53 ·  中国 广东 广州 联通
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Hehe, the OP is using it wrong.

So the test statement doesn't work...

Also, :errorlevel is an environment variable...

The OP can completely modify it according to his own needs...

For example...

Here's an example for you

@echo off

echo %errorlevel%
call :abc
echo %errorlevel%
goto :END
:abc

set errorlevel=1234

goto :EOF

:END

pause
知,不觉多。不知,乃求知
Forum Jump: