如何打破嵌套循环批处理脚本中的内部循环

发布于 2024-10-12 11:48:20 字数 383 浏览 3 评论 0原文

我的目标是逐行比较两个文件并捕获更改。为此,我使用两个嵌套循环。在某些情况下,我不得不制动内循环。

我在内循环之外使用标签来打破它,但不起作用。它还会标记并终止外循环。

@ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巾帼英雄 2024-10-19 11:48:20

goto :label 总是会打破所有循环。

但是你可以将内部循环放在一个单独的函数中,然后它就可以工作了。

@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 循环总是计数到末尾,但如果打破它,内部代码的执行就会停止,但速度可能会非常慢。

@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

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

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

EDIT:

Proof of concept

@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

Output

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
只是我以为 2024-10-19 11:48:20

即使 goto 标签位于 for 循环中,它也会跳出循环上下文。比如

@echo off
for %%d in (A B) do (
    echo %%d
    for %%f in ( 1 2 ) do (
        goto loop
        :loop
        echo %%d %%f
    )
)

这会打印出来
一个
%d%f

Even if the goto label is in the for loop, it also goes out the loop context. Such as

@echo off
for %%d in (A B) do (
    echo %%d
    for %%f in ( 1 2 ) do (
        goto loop
        :loop
        echo %%d %%f
    )
)

This will print out
A
%d %f

长梦不多时 2024-10-19 11:48:20

你不必重新发明轮子(如果你有选择的话)。这只是一种方式。下载 Windows 版 diffutils 然后你就可以这样做

diff sample.txt test.txt

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

diff sample.txt test.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文