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-01 16:51
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Please help me see what's wrong with this script, thanks View 964 Replies 2
Original Poster Posted 2006-06-04 08:00 ·  中国 江苏 苏州 教育网
初级用户
Credits 20
Posts 4
Joined 2006-06-03 10:58
20-year member
UID 56478
Status Offline
I've just started learning batch files.
Why does this simple script jump to First every time?
Friends who understand it, please help explain it, thanks.
Also, could you introduce how choice return values work?


@echo off
:start
choice /C:FSTO /M "First,Second,Third,Out"
if errorlevel 1 goto First
if errorlevel 2 goto Second
if errorlevel 3 goto Third
if errorlevel 4 goto end
:First
echo This is the first line.
goto start
:Second
echo This is the Second line.
goto start
:Third
echo This is the Third line.
goto start
:end
echo bye!
Floor 2 Posted 2006-06-04 10:49 ·  中国 四川 成都 联通
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
ErrorLevel x means a case greater than or equal to x! So you have to write it in reverse order!

if errorlevel 4 goto end
if errorlevel 3 goto Third
if errorlevel 2 goto Second
if errorlevel 1 goto First

This is the mistake beginners are most likely to make when writing ErrorLevel ^_^

Check the help yourself for how to use chioce!

[ Last edited by electronixtar on 2006-6-4 at 10:51 ]

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 3 Posted 2006-06-04 10:50 ·  中国 广东 广州 海珠区 电信
荣誉版主
★★★
Credits 718
Posts 313
Joined 2005-09-26 00:00
20-year member
UID 42844
Gender Male
Status Offline
RE ziver :
A: Using this forum's search function instead of asking for help will save you a lot of waiting time, and you'll also be able to see more examples and understand more.
B: Error codes should be tested starting from the highest value and going downward.
if errorlevel 4 goto end
if errorlevel 3 goto Third
if errorlevel 2 goto Second
if errorlevel 1 goto First

C: If you don't use error codes for testing, you can test based on the character input by the user. See link {1}
D: Related links:
{1}http://www.cn-dos.net/forum/viewthread.php?tid=15763&fpage=1&highlight=choice
{2}http://www.cn-dos.net/forum/viewthread.php?tid=6287&fpage=1&highlight=choice
{3}http://www.cn-dos.net/forum/viewthread.php?tid=19414&fpage=1&highlight=choice
{4}http://www.cn-dos.net/forum/viewthread.php?tid=5675&fpage=1&highlight=choice

***************
Please change the thread title as soon as possible. Otherwise moderator willsort is going to dock your credits! Haha
Suggested title: A question about the choice command and errorlevel
***************

[ Last edited by 220110 on 2006-6-4 at 15:24 ]
Forum Jump: