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-04 19:44
中国DOS联盟论坛 » DOS学习入门 & 精彩文章 (教学室) » What’s wrong with this batch file View 870 Replies 2
Original Poster Posted 2003-08-23 00:00 ·  中国 四川 成都 联通
初级用户
Credits 113
Posts 3
Joined 2003-08-23 00:00
22-year member
UID 9074
Gender Male
Status Offline
This is a *.bat file I wrote by following the example in a book, but it gives an error when I run it.
Please help me take a look



@echo off
choice /c:dme defrag,mem,end
if errorlevel 3 goto defrag
if errorlevel 2 goto mem
if errotlevel 1 goto end

:defrag
c:\windows\defrag.exe
goto end
:mem
mem
goto end
:end
echo good bye

After running it, choosing d or m is fine, but choosing e gives the result for choosing d

Floor 2 Posted 2003-09-06 00:00 ·  中国 山东 烟台 福山区 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re xuan5272095:

It's obviously an input error:
if errotlevel 3 goto end -> if errorlevel 3 goto end
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 3 Posted 2003-09-06 00:00 ·  中国 湖北 武汉 联通
银牌会员
★★★
Credits 1,681
Posts 512
Joined 2003-08-02 00:00
22-year member
UID 7953
Gender Male
Status Offline
Make a choice: choice /c:dme defrag,mem,end (no problem)
Choose end (errorlevel 1)
if errorlevel 3 goto defrag -- errorlevel is less than 3, skip
if errorlevel 2 goto mem -- errorlevel is less than 2, skip
if errotlevel 1 goto end -- invalid statement, skip and show an error message
:defrag -- label, skip
c:\windows\defrag.exe -- execute
goto end -- go to the end

That is why choosing e gives the result for choosing d

Add one line: goto error

if errorlevel 3 goto defrag
if errorlevel 2 goto mem
if errotlevel 1 goto end
goto error

Then make an error entry

:error
ECHO Choice.com not found
goto end

This can avoid errors caused by not finding choice.com (and if the BAT is written wrong, it will also show an error message)

Somehow somewhere I've got to choose.
No matter if it is win or lose.
Forum Jump: