Maanu
Maanu

Reputation: 5203

Exit from For loop

I want to exit from the for loop after getting the first string. How can I do that? Should I use for loop or something else?

for /F "delims=" %%c in ('dumpbin "%%i" /headers ^| findstr "(x86)"') do set Result3=%%c

Upvotes: 0

Views: 124

Answers (1)

dbenham
dbenham

Reputation: 130819

You can use GOTO to exit a FOR loop prematurely. Both FOR and FOR /F are terminated immediately when GOTO is executed.

for /F "delims=" %%c in ('dumpbin "%%i" /headers ^| findstr "(x86)"') do set Result3=%%c&goto :break
:break

The FOR /L will sort of exit immediately upon GOTO in that the contents of the DO clause will no longer be executed. But the FOR /L will silently continue to count until it reaches its end condition.

If your intent is to immediately leave not only the FOR loop but also exit out of a subroutine or batch script, then it is more efficient to use EXIT /B instead of GOTO :LABEL.

Upvotes: 3

Related Questions