2016-07-01 78 views
1

我需要爲文件中的每一行運行一個程序。我一次觸發四個實例。四次後,等待一段時間,並觸發另一次。等待時間後批處理腳本中斷。使用goto時在批處理文件中打破for循環

請建議如果我做錯了。

for /F "tokens=*" %%J in (%JobListFile%) do (
    SET JobName=%%J 
    echo job name !JobName! 

    if "!JobName!" equ "" (
     echo joblist not found... rerun the script.. 
     REM exit /b 
    ) else (
     :waittofinish 
     for /F "tokens=*" %%a in ('tasklist /fi "IMAGENAME eq dsexport.exe" ^| grep -c "dsexport.exe"') do (SET /A CurrInstances=%%a) 
     echo current instance is !CurrInstances! 
     echo parallelism set to !NoOfParallelInstances! 
     if "!CurrInstances!" gtr "!NoOfParallelInstances!" (
      echo going to wait 
      sleep 5 
      goto waittofinish 
      echo failed to go wait label... 
     ) else (
      echo Exporting job: !JobName! ...............Starting 
      start /b cmd /C "C:/IBM/9.1/InformationServer/Clients/Classic/dsexport.exe /D=%vDomain% /U=%vuserID% /P=%vpassword% /H=%vServer% %vDSProject% /NODEPENDENTS /JOB=!JobName! %tmppath%\!JobName!.dsx" 
      echo. 
      echo. 
     ) 
    ) 

) 

echo script completed... 

exit /b 
+1

不要在塊中使用'goto'或標籤(塊之間都是'('和')'),除非你故意要離開塊。 – Stephan

+1

你可以把':waittofinish'開頭的整個塊(所有代碼放在'if'!JobName!「equ''''的'else'子句中)放到子例程中並使用'call';這樣你可以從'for/F %% J'循環的上下文中隱藏'goto' ... – aschipfl

+0

謝謝Aschipfl。有效。你救了我......但是有什麼遺失?最後一行是執行兩次?我的意思是在文件的最後一行,程序執行兩次..任何想法呢?子程序第二次觸發程序。如何預防它? – srinath

回答

1

goto :Label場所括號(...)一個代碼塊的塊上下文;對於for ... do (...)循環和if ... (...) else (...)條件也是如此。

爲了克服這一點,你可以把與goto:Label到子程序的代碼部分,因爲這個隱藏調用代碼部分的塊的上下文從goto,像這樣:

for /F "usebackq tokens=*" %%J in ("%JobListFile%") do (
    SET "JobName=%%J" 
    echo job name !JobName! 

    if "!JobName!" equ "" (
     echo joblist not found... rerun the script.. 
     REM exit /b 
    ) else (
     rem /* The `goto` and `:Label` code fragment has been transferred to a subroutine, 
     rem which receives the current value of variable `JobName` as an argument: */ 
     call :waittofinish "!JobName!" 
    ) 

) 

echo script completed... 

exit /b 


:waittofinish JobName 
rem // This subroutine contains the `goto` and `:Label` code fragment so that it does no longer appear inside of a block `(...)`: 
for /F "tokens=*" %%a in ('tasklist /fi "IMAGENAME eq dsexport.exe" ^| grep -c "dsexport.exe"') do (SET /A CurrInstances=%%a) 
echo current instance is !CurrInstances! 
echo parallelism set to !NoOfParallelInstances! 
if "!CurrInstances!" gtr "!NoOfParallelInstances!" (
    echo going to wait 
    sleep 5 
    goto :waittofinish 
    echo failed to go wait label... 
) else (
    echo Exporting job: %~1 ...............Starting 
    rem // Not sure if some arguments should be enclosed in `""` in the next line (but I do not know `dsexport.exe`): 
    start "" /b cmd /C "C:/IBM/9.1/InformationServer/Clients/Classic/dsexport.exe /D=%vDomain% /U=%vuserID% /P=%vpassword% /H=%vServer% %vDSProject% /NODEPENDENTS /JOB=%~1 %tmppath%\%~1.dsx" 
    echo. 
    echo. 
) 
exit /b 

注:我沒不檢查你的腳本的邏輯,因爲我不知道grep.exedsexport.exe