Exit in For loop - Windows Command Processor (CMD.EXE)

后端 未结 3 1535
半阙折子戏
半阙折子戏 2020-12-19 09:50

I am trying to find way to break / exit from FOR loop, if there are any error occured. Below is content of batch file.

@echo on

set myfile=D:\\sample.txt

F         


        
3条回答
  •  Happy的楠姐
    2020-12-19 10:00

    Joey's answer is great. I have used it with success. I discovered that you don't have to exit the script though. You can use goto :SomeLabel, where :SomeLabel is a label outside of the loop.

    FOR /F "tokens=1,2 delims=," %%i in (%myfile%) do (
      if defined exit goto :ParseError
      call :process "%%i"
    )
    
    @echo SUCCESS: %myfile%
    goto :RestOfScript
    
    :ParseError
    @echo FAILURE: cannot parse %myfile%
    @echo Using defaults...
    
    :RestOfScript
    ...
    
    

提交回复
热议问题