如何打破嵌套循环批处理脚本中的内部循环
我的目标是逐行比较两个文件并捕获更改。为此,我使用两个嵌套循环。在某些情况下,我不得不制动内循环。
我在内循环之外使用标签来打破它,但不起作用。它还会标记并终止外循环。
@ 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
)
任何人都可以帮忙......?
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....?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
goto :label 总是会打破所有循环。
但是你可以将内部循环放在一个单独的函数中,然后它就可以工作了。
需要注意的是,打破 FOR /L 循环并不能按预期工作,for 循环总是计数到末尾,但如果打破它,内部代码的执行就会停止,但速度可能会非常慢。
编辑:
概念验证
输出
A goto :label always breaks all loops.
But you can put your inner loop in a separated function, then it could work.
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.
EDIT:
Proof of concept
Output
即使 goto 标签位于 for 循环中,它也会跳出循环上下文。比如
这会打印出来
一个
%d%f
Even if the goto label is in the for loop, it also goes out the loop context. Such as
This will print out
A
%d %f
你不必重新发明轮子(如果你有选择的话)。这只是一种方式。下载 Windows 版 diffutils 然后你就可以这样做
you do not have to reinvent the wheel (if you have a choice). This is just one way. Download diffutils for windows and then you can just do