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-06-25 06:49
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » 请问DOS下使用for命令的时候如何让其退出循环 Please tell me how to make the for command exit the loop under DOS View 1,619 Replies 5
Original Poster Posted 2009-10-14 10:15 ·  中国 天津 南开区 联通
初级用户
爱的使者
Credits 195
Posts 32
Joined 2003-07-20 00:00
22-year member
UID 7213
Gender Male
From Amway
Status Offline
In batch scripting, to make the loop stop as soon as the first matching condition is found, you can use the `goto` statement. Here's how you can modify the code:

```batch
@echo off
for %%i in (z,y,x,w,v,u,t,s,r,q,p,o,n,m,l,k,j,i,h,g,f,e,d,c) do (
if exist %%i:\ghost\winxp.gho (
set drv=%%i
goto :breakLoop REM Jump to the label to exit the loop
)
)
:breakLoop
```

The key change is adding the `goto :breakLoop` inside the `if` block to jump to the label `:breakLoop` once the first match is found, which effectively exits the loop.
http://sikro.spaces.live.com/
Floor 2 Posted 2009-10-14 10:39 ·  中国 广东 广州 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
Write a label externally and then use goto ~ to jump out. This is okay in general. Oh, it's under DOS... I haven't tried it. Are you talking about pure DOS? But you wrote "%%i", is it under batch processing or under DOS...

Got it, if it's under wincmd, it can be like this, using


cmd /c "@echo off &for /l %a in (1,1,5) do (if %a==2 (echo %a &exit))"


[ Last edited by 523066680 on 2009-10-14 at 12:08 ]
Floor 3 Posted 2009-10-14 12:11 ·  中国 天津 南开区 联通
初级用户
爱的使者
Credits 195
Posts 32
Joined 2003-07-20 00:00
22-year member
UID 7213
Gender Male
From Amway
Status Offline
This script runs under pure DOS. I tried it and it works with %%i in DOS 7.1.
http://sikro.spaces.live.com/
Floor 4 Posted 2009-10-14 14:24 ·  中国 广东 广州 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
That is in the script, not in...
Haven't used DOS 7.1 x_x
Floor 5 Posted 2009-10-20 09:20 ·  IANA 局域网IP(Private-Use)
新手上路
Credits 10
Posts 9
Joined 2006-03-21 22:40
20-year member
UID 52551
Gender Male
Status Offline
Learn from the experts' experiences
Floor 6 Posted 2010-12-18 10:02 ·  中国 上海 浦东新区 电信
初级用户
Credits 23
Posts 12
Joined 2006-02-12 22:28
20-year member
UID 50257
Status Offline
for %%i in (z,y,x,w,v,u,t,s,r,q,p,o,n,m,l,k,j,i,h,g,f,e,d,c) do if exist %%i:\ghost\winxp.gho set drv=%%i && goto end

:end
echo pass
Forum Jump: