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-04 10:04
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How do I break out of the inner for loop and return to the outer for loop? View 1,315 Replies 7
Original Poster Posted 2007-04-27 01:05 ·  中国 北京 联通
初级用户
Credits 50
Posts 18
Joined 2007-04-20 05:57
19-year member
UID 85810
Gender Female
Status Offline
There are two for loops. I want to break out of the inner one and return to the outer for loop. What should I do?
Does DOS have anything like break or continue?
The specific code:
@echo off
for /f "delims=" %%i in (1.txt) do call :ss %%i
:ss
set a=%1
setlocal ENABLEDELAYEDEXPANSION
for /f "delims=" %%j in (2.txt) do (
set b=%%j
if !a! NEQ !b! echo %%j>new.txt

Add a statement here to exit the inner for loop

)


Many thanks!
Floor 2 Posted 2007-04-27 01:18 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
for /f "delims=" %%i in (1.txt) do (
for /f "delims=" %%j in (2.txt) do (
if "%%i" == "%%j" echo %%j>new.txt&&goto :eof
)
)
This seems to work, but for this kind of search, using findstr /G:1.txt 2.txt>>new.txt is a bit more convenient
Floor 3 Posted 2007-04-29 06:25 ·  中国 北京 联通
初级用户
Credits 50
Posts 18
Joined 2007-04-20 05:57
19-year member
UID 85810
Gender Female
Status Offline
Thanks
Floor 4 Posted 2007-04-29 09:14 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
Originally posted by everest79 at 2007-4-26 12:18:
for /f "delims=" %%i in (1.txt) do (
for /f "delims=" %%j in (2.txt) do (
if "%%i" == "%%j" echo %%j>new.txt&&goto :eof
)
)
This seems to work ...

But wouldn't this exit the batch file when %%i equals %%j?
Floor 5 Posted 2007-04-29 09:22 ·  中国 广东 广州 天河区 电信
高级用户
★★★
潜水修练批处理
Credits 788
Posts 366
Joined 2006-12-31 02:43
19-year member
UID 75048
Gender Male
Status Offline
When using a for loop, call another for with call, then use goto :eof inside that for, for example:

@echo off
for /l %%i in (1,1,5) do call :doit %%i
pause
eixt
:doit
for /l %%a in (6,1,10) do (
if "%%a"=="9" goto :eof
echo %1 %%a
)
goto :eof
Floor 6 Posted 2007-04-29 09:33 ·  中国 广东 广州 天河区 电信
高级用户
★★★
潜水修练批处理
Credits 788
Posts 366
Joined 2006-12-31 02:43
19-year member
UID 75048
Gender Male
Status Offline
Posting one more simple example; the rest can be expanded from the above

@echo off
for /l %%a in (6,1,10) do (
if "%%a"=="9" goto :end
echo %1 %%a
)
:end
pause
exit
Floor 7 Posted 2007-05-01 22:55 ·  中国 北京 联通
初级用户
Credits 50
Posts 18
Joined 2007-04-20 05:57
19-year member
UID 85810
Gender Female
Status Offline
Thanks
Floor 8 Posted 2007-05-02 11:32 ·  中国 湖北 武汉 电信
中级用户
★★
scriptlover
Credits 328
Posts 131
Joined 2007-03-25 22:17
19-year member
UID 82910
Gender Male
Status Offline
Why is the line echo %1 %%a written like that?? Wouldn't removing %1 make it shorter? I want to know what the advantage of writing it this way is. Please answer, brother dikex.
scriptlover
Be with script forever!!
Forum Jump: