中国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-08-12 08:16
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » 请问DOS下使用for命令的时候如何让其退出循环 Please tell me how to make the for command exit the loop under DOS
Printable Version  1,780 / 5
Floor1 sikro Posted 2009-10-14 10:15
初级用户 Posts 32 Credits 195 From Amway
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.
Floor2 523066680 Posted 2009-10-14 10:39
银牌会员 Posts 1,133 Credits 2,362
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



[ Last edited by 523066680 on 2009-10-14 at 12:08 ]
Floor3 sikro Posted 2009-10-14 12:11
初级用户 Posts 32 Credits 195 From Amway
This script runs under pure DOS. I tried it and it works with %%i in DOS 7.1.
Floor4 523066680 Posted 2009-10-14 14:24
银牌会员 Posts 1,133 Credits 2,362
That is in the script, not in...
Haven't used DOS 7.1 x_x
Floor5 cj000 Posted 2009-10-20 09:20
新手上路 Posts 9 Credits 10
Learn from the experts' experiences
Floor6 shengping Posted 2010-12-18 10:02
初级用户 Posts 12 Credits 23
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
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023