批处理脚本"继续"在循环?

时间:2014-10-06 08:40:38

标签: windows loops batch-file for-loop continue

do()不适用于我上网的代码,所以我正在使用do:processline。 问题是“继续”;想要进入下一次迭代时不起作用。这个问题是它正在执行:每次迭代的eof而不是post loop ...如何避免这种情况?感谢

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%

2 个答案:

答案 0 :(得分:3)

continue;不是内部或外部命令。

EOF是CMD中的内部标签,不需要包含在其中。

测试一下:

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

此代码也应该以相同的方式工作,但使用延迟扩展时!个字符会成为毒药字符。

@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

答案 1 :(得分:0)

我用这个继续循环(注意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 ) ):

08264.51782528
08 = last 2 digits of year (will be within  last 60 years so if the 2 digits are above current (eg. 18, then assume they're in the 20th century)
264 = number of days
51782528 = decimal representation of how far through the day (0 = midnight, 0.5 = noon, 0.999988 = 1 second to midnight the following day)