中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-25 05:22
47,813 topics / 349,918 posts / today 1 new / 48,274 members
DOS批处理 & 脚本技术(批处理室) » Can a subroutine I write myself return an errorlevel?
Printable Version  881 / 3
Floor1 bright2k Posted 2007-04-25 04:42
初级用户 Posts 8 Credits 22
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 ]
Floor2 dikex Posted 2007-04-25 04:47
高级用户 Posts 366 Credits 788
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
Floor3 bright2k Posted 2007-04-25 08:15
初级用户 Posts 8 Credits 22
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
Floor4 flyinspace Posted 2007-04-25 09:53
银牌会员 Posts 517 Credits 1,206
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
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023