Why does this batch file never break out of the loop?
For /L %%f In (1,1,1000000) Do @If Not Exist %%f Goto :EOF
Shouldn't the Goto :EOF
break out of the loop?
I guess I should've asked more explicitly... how can I break out of the loop?
batch-file Echo @Echo off @echo off prevents the prompt and contents of the batch file from being displayed, so that only the output is visible. The @ makes the output of the echo off command hidden as well.
You can use goto :eof to terminate a subroutine (reaching end-of-file terminates the routine and returns to the statement after the call ) OR you can use exit or exit /b [n] as explained in the documentation (se exit /? from the prompt.
break command (C and C++) break ; In a looping statement, the break command ends the loop and moves control to the next command outside the loop. Within nested statements, the break command ends only the smallest enclosing do , for , switch , or while commands.
Ctrl+C. One of the most universal methods of aborting a batch file, command, or another program while it's running is to press and hold Ctrl + C . This keyboard shortcut sends a SIGINT signal, which cancels or terminates the currently-running program and returns you to the command line.
You could simply use echo on
and you will see that goto :eof
or even exit /b
doesn't work as expected.
The code inside of the loop isn't executed anymore, but the loop is expanded for all numbers to the end.
That's why it's so slow.
The only way to exit a FOR /L loop seems to be the variant of exit
like the exsample of Wimmel, but this isn't very fast nor useful to access any results from the loop.
This shows 10 expansions, but none of them will be executed
echo on for /l %%n in (1,1,10) do ( goto :eof echo %%n )
Based on Tim's second edit and this page you could do this:
@echo off if "%1"=="loop" ( for /l %%f in (1,1,1000000) do ( echo %%f if exist %%f exit ) goto :eof ) cmd /v:on /q /d /c "%0 loop" echo done
This page suggests a way to use a goto inside a loop, it seems it does work, but it takes some time in a large loop. So internally it finishes the loop before the goto is executed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With