如何突破内部循环中嵌套的循环批处理脚本 [英] How to break inner loop in nested loop batch script
问题描述
我的目标是通过行来比较两个文件行并捕获变化。对于我使用两个嵌套循环。我坚持制动某种条件的内部循环。
MY goal is to compare two files line by line and capture the changes. For that i am using two nested loops. I am stuck with braking the inner loop on some condition.
我使用的标签打破它内环之外,但不工作。它去标签并终止外环也。
I am using label outside the inner loop for break it, but not working. It goes to label and terminate outer loop also.
@ echo off
SETLOCAL EnableDelayedExpansion
for /F "skip=8 tokens=* delims=." %%a in (sample.txt) do (for /F "skip=8 tokens=* delims=." %%b in (test.txt) do (if %%a==%%b (goto :next) else ( echo %%a)
)
: Next
echo out of inner loop
)
任何人都可以帮助...?
Anyone can help....?
推荐答案
一个GOTO:标签总是打破了所有的循环
A goto :label always breaks all loops.
但是,你可以把你的内循环的分离作用,那么它可以工作。
But you can put your inner loop in a separated function, then it could work.
@echo off
SETLOCAL EnableDelayedExpansion
for /F "skip=8 tokens=* delims=." %%a in (sample.txt) do (
call :myInnerLoop "%%a"
)
echo out of inner loop
)
goto :eof
:myInnerLoop
for /F "skip=8 tokens=* delims=." %%b in (test.txt) do (
if "%~1"=="%%b" (
goto :next
) else (
echo %%a
)
:next
goto :eof
一此言一出,FOR / L循环断不工作不如预期,for循环总是算到最后,但如果你打破它,内code的停止执行,但它可能是真的很慢。
One remark, breaking of FOR /L loops does not work as expected, the for-loop always count to the end, but if you break it, the execution of the inner code is stopped, but it could be really slow.
@echo ON
FOR /L %%n IN (1,1,1000000) DO (
echo %%n - count
goto :break
)
:break
编辑:
概念验证
@echo off
SETLOCAL EnableDelayedExpansion
for %%a in (a b c) DO (
echo Outer loop %%a
call :inner %%a
)
goto :eof
:inner
for %%b in (U V W X Y Z) DO (
if %%b==X (
echo break
goto :break
)
echo Inner loop Outer=%1 Inner=%%b
)
:break
goto :eof
输出
Outer loop a
Inner loop Outer=a Inner=U
Inner loop Outer=a Inner=V
Inner loop Outer=a Inner=W
break
Outer loop b
Inner loop Outer=b Inner=U
Inner loop Outer=b Inner=V
Inner loop Outer=b Inner=W
break
Outer loop c
Inner loop Outer=c Inner=U
Inner loop Outer=c Inner=V
Inner loop Outer=c Inner=W
break
这篇关于如何突破内部循环中嵌套的循环批处理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!