batch script "continue" in loop?

14,598

Solution 1

continue; is not an internal or external command.

EOF is an internal label in CMD and doesn't need to be included.

Test this:

SET NGCSV=UserList3Col.csv
SET NGCSVT1=UserList3Col.csv.temp1
SET NGCSVT2=UserList3Col.csv.temp2
SET NGFINAL=UserListFinal.csv
del %NGFINAL%
set /a c=0
for /f "tokens=1" %%i in (groupList.txt) do call :processline %%i
goto :continue

:processline 
set /a c=c+1
echo %c%
SET GROUP=%*
setlocal EnableDelayedExpansion
net group /domain "GG-%GROUP%" > %NGCSV%
REM * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
REM Now strip out the crap
REM ...make a temporary copy
COPY %NGCSV% %NGCSVT2%
REM ...strip off the crap using alternating temp files
findstr /B /L /V /C:"The request" %NGCSVT2% > %NGCSVT1%
findstr /B /L /V /C:"Group name" %NGCSVT1% > %NGCSVT2%
findstr /B /L /V /C:"Comment" %NGCSVT2% > %NGCSVT1%
findstr /B /L /V /C:"Members" %NGCSVT1% > %NGCSVT2%
findstr /B /L /V /C:"-----" %NGCSVT2% > %NGCSVT1%
findstr /B /L /V /C:"The command" %NGCSVT1% > %NGCSVT2%
REM ...make the last temporary copy the final copy and clean up
COPY %NGCSVT2% %NGCSV%
DEL %NGCSVT1%
DEL %NGCSVT2%
REM * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
REM ...Column 1
for /F "tokens=1" %%A in (%NGCSV%) do @echo %GROUP%,%%A >> %NGFINAL%
REM ...Column 2
for /F "tokens=2" %%A in (%NGCSV%) do @echo %GROUP%,%%A >> %NGFINAL%
REM ...Column 3
for /F "tokens=3" %%A in (%NGCSV%) do @echo %GROUP%,%%A >> %NGFINAL%
del %NGCSV%
goto :EOF
:continue
echo this is reached after the entire file is parsed
pause

This code should work in the same way too with the exception that ! characters become a poison character when delayed expansion is used.

@echo off
setlocal enabledelayedexpansion
SET NGCSV=UserList3Col.csv
SET NGCSVT1=UserList3Col.csv.temp1
SET NGCSVT2=UserList3Col.csv.temp2
SET NGFINAL=UserListFinal.csv
del %NGFINAL% 2>nul
del %NGCSV%   2>nul
set /a c=0
for /f "tokens=1" %%i in (groupList.txt) do (
set /a c=c+1
echo !c!
net group /domain "GG-%%i" |findstr /B /L /V /i /C:"The request" /C:"Group name" /C:"Comment" /C:"Members" /C:"-----" /C:"The command" > %NGCSV%
REM ...Column 1
for /F "tokens=1" %%A in (%NGCSV%) do >>%NGFINAL% echo %%i,%%A
REM ...Column 2
for /F "tokens=2" %%A in (%NGCSV%) do >>%NGFINAL% echo %%i,%%A
REM ...Column 3
for /F "tokens=3" %%A in (%NGCSV%) do >>%NGFINAL% echo %%i,%%A
del %NGCSV%
)
pause

Solution 2

I use this one to continue the loop (note :next label MUST have some code, I use echo smth > nul):

for /f "eol= tokens=*" %%a in (settings.ini) do ( 
  set line=%%a    
 
  if "%line%"=="!line:[Server]=!" (
    echo ***[Server] section found
    goto :next
  ) 
  
  :next
  echo continue this loop > nul
)
Share:
14,598
Dird
Author by

Dird

Updated on June 22, 2022

Comments

  • Dird
    Dird almost 2 years

    do ( ) wasn't working for the code I got online so instead I'm using do :processline. The problem is "continue;" doesn't work when wanting to go to the next iteration. The problem with this is it's executing :eof for each iteration rather than post loop...how to avoid this? Thanks

    SET NGCSV=UserList3Col.csv
    SET NGCSVT1=UserList3Col.csv.temp1
    SET NGCSVT2=UserList3Col.csv.temp2
    SET NGFINAL=UserListFinal.csv
    del %NGFINAL%
    set /a c=0
    for /f "tokens=1" %%i in (groupList.txt) do call :processline %%i
    goto :eof
    
    :processline 
    SET GROUP=%*
    setlocal EnableDelayedExpansion
    net group /domain "GG-%GROUP%" > %NGCSV%
    REM * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    REM Now strip out the crap
    REM ...make a temporary copy
    COPY %NGCSV% %NGCSVT2%
    REM ...strip off the crap using alternating temp files
    findstr /B /L /V /C:"The request" %NGCSVT2% > %NGCSVT1%
    findstr /B /L /V /C:"Group name" %NGCSVT1% > %NGCSVT2%
    findstr /B /L /V /C:"Comment" %NGCSVT2% > %NGCSVT1%
    findstr /B /L /V /C:"Members" %NGCSVT1% > %NGCSVT2%
    findstr /B /L /V /C:"-----" %NGCSVT2% > %NGCSVT1%
    findstr /B /L /V /C:"The command" %NGCSVT1% > %NGCSVT2%
    REM ...make the last temporary copy the final copy and clean up
    COPY %NGCSVT2% %NGCSV%
    DEL %NGCSVT1%
    DEL %NGCSVT2%
    REM * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    REM ...Column 1
    for /F "tokens=1" %%A in (%NGCSV%) do @echo %GROUP%,%%A >> %NGFINAL%
    REM ...Column 2
    for /F "tokens=2" %%A in (%NGCSV%) do @echo %GROUP%,%%A >> %NGFINAL%
    REM ...Column 3
    for /F "tokens=3" %%A in (%NGCSV%) do @echo %GROUP%,%%A >> %NGFINAL%
    del %NGCSV%
    continue; rem Doesn't work
    
    :eof
    set /a c=c+1
    echo %c%
    
  • Dird
    Dird over 9 years
    Thanks that works although I'd rather not have the end of the workflow in the middle. Is there no continue equivalent to jump to the next iteration then?
  • foxidrive
    foxidrive over 9 years
    It does iterate through the grouplist.txt file - what might confuse you it is that the counter only executes once, as it wasn't clear in your code what it was supposed to be doing. See the edited answer above, plus an alternate code which should work the same way.
  • Dird
    Dird over 9 years
    I know it iterated. By "continue" I mean, before the loop would still call :eof each iteration. I was wondering if there was a way to keep the :eof section at the bottom and before that have a command saying "don't continue reading down the file, begin the next iteration of the loop instead" like the "continue;" command does in most languages. I like that 1 line findstr though, will try to incorporate
  • foxidrive
    foxidrive over 9 years
    My edited version of your first script shows you how to do what you asked.
  • Dird
    Dird over 9 years
    Oh I see. So I was choosing a bad name with :eof or it would work the same for any :proc_name not defined in the file?
  • foxidrive
    foxidrive over 9 years
    :eof is not recommended as a label name but any other label name could be used. People generally recommend to avoid using names of commands as label names too, as it can be confusing.
  • AntonK
    AntonK almost 4 years
    probably this works in your case, but generally the approach seems to be wrong - goto disrupts the execution flow
  • Larry
    Larry over 2 years
    FYI should be echo continue this loop > nul as mentioned in your description not in your code. note the single 'l' in 'nul'. 2 'l's in 'null' just creates a file instead. I couldn't contribute the edit :(
  • Dr.eel
    Dr.eel over 2 years
    @Larry got it! 10x. Fixed
  • jeb
    jeb over 2 years
    The goto :next doesn't implement a continue, it's a break