Turn off nested loops in a batch file

After going through this problem twice, I thought I would post it here to find out if anyone knew how to get around it.

I cannot break out of nested loops using goto , because it seems that when it exits the inner loop, the brackets then do not match, because it never reached the inner close.

I narrowed it down to a very simple example

 for %%a in (1,2,3) do ( for %%b in (4,5,6) do ( echo Breaking goto :BREAK ) :BREAK ) 

This results in an error.

 ) was unexpected at this time. 

I thought maybe adding extra brackets might solve the problem, but that will not help if I don’t know what I will break, if it is a conditional break, this is the same problem.

Is there any easy alternative to exiting the inner loop back to the outer, even if it's a conditional break using if and else 's?

+8
batch-file
source share
5 answers

Break by placing the inner contour in the label.

 for %%a in (1, 2, 3) DO ( call :innerloop ) :innerloop for %%b in (4, 5, 6) DO ( if %%b==<something> ( echo break goto :break ) ) :break 
+10
source share

You can also omit the remaining iterations after breaking with the control variable bound to FOR:

 @echo off for %%a in (1,2,3) do ( echo Top-level loop: %%a set break= for %%b in (4,5,6) do if not defined break ( echo Nested loop: %%a-%%b if %%b == 4 ( echo Breaking set break=yes ) ) ) 

This method stores the code of the embedded body in its original location and makes it easy to set gaps at more than one point within the body.

Antonio

+5
source share

Antonio's offer is not bad. I used it in my example to parse some CSV or HTML files from a list of files. (nested loop). The first look analyzes the list of files, the second analyzes the contents of each file. Separators either; or /.

 @dir /O /B j:\temp\*.txt > j:\temp\filelist.doc @for /F "tokens=1" %%a in (j:\temp\filelist.doc) DO ( @echo j:\temp\%%a @set ef= @for /F "delims=;/ tokens=1,2,3,4,5,6,7*" %%b in (j:\temp\%%a) DO @if not defined ef ( @if %%e == CNAME ( @echo Do something here @set ef=yes ) ) ) 
0
source share

DEFINED can act evil for several reasons . I do not suggest using it unless you know exactly how it works and all possible use cases.

It is recommended that you avoid using separator characters (spaces, commas, etc.) in the variable name, for example IF DEFINED _variable will often fail if the variable name contains a separator character.

A safer answer that also doesn't use EXIT or GOTO :

 @echo off SETLOCAL ENABLEDELAYEDEXPANSION Set time_to_exit=false FOR /L %%O IN (1,1,3) DO ( echo [ Outer ] loop iteration %%O FOR /L %%I IN (1,1,3) DO ( IF NOT "!time_to_exit!"=="true" ( echo ]Inner[ loop iteration %%I IF "%%O"=="2" Set time_to_exit=true ) ) ) 

exit:

 [ Outer ] loop iteration 1 ]Inner[ loop iteration 1 ]Inner[ loop iteration 2 ]Inner[ loop iteration 3 [ Outer ] loop iteration 2 ]Inner[ loop iteration 1 [ Outer ] loop iteration 3 
0
source share

Could you provide additional information about your requirement? in the meantime, you can try this if it meets your requirement. keep me posted if you need more.

 @echo off for %%a in (1,2,3) do ( for %%b in (4,5,6) do ( echo Breaking goto Break ) ) :Break 
-one
source share

All Articles