简体   繁体   English

批处理脚本“继续”循环?

[英]batch script “continue” in loop?

do ( ) wasn't working for the code I got online so instead I'm using do :processline. do ( ) 不适用于我在线获取的代码,因此我使用 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?问题在于它为每次迭代执行 :eof 而不是 post loop ......如何避免这种情况? 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%

continue; is not an internal or external command.不是内部或外部命令。

EOF is an internal label in CMD and doesn't need to be included. EOF是 CMD 中的内部标签,不需要包含。

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

I use this one to continue the loop (note :next label MUST have some code, I use echo smth > nul ):我用这个来继续循环(注意:next标签必须有一些代码,我使用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 > null
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM