2012-08-13 65 views
0

斷裂考慮以下批處理文件:CALL在爲

@echo off 

set ORACLESERVER=ORAOP09 
set ORACLEDB=OraDb11g_home1 

set ORACLESERVICES.1 = OracleVssWriter%ORACLESERVER% 
set ORACLESERVICES.2 = OracleDBConsole%ORACLESERVER% 
set ORACLESERVICES.3 = Oracle%ORACLEDB%TNSListener 
set ORACLESERVICES.4 = OracleService%ORACLESERVER% 

AT > NUL 
IF ERRORLEVEL 1 (
    echo FATAL: THIS SCRIPT MUST BE RUN AS ADMINISTRATOR! 
    pause 
    exit 
) 

echo Stopping services... 
FOR /F "tokens=2* delims=.=" %%A IN ('SET ORACLESERVICES.') DO sc stop %%B > NUL 
FOR /F "tokens=2* delims=.=" %%A IN ('SET ORACLESERVICES.') DO call:check %%B 

:check 
    sc query %1 | find /I "State" | find /I "STOPPED" 
    IF ERRORLEVEL 1 call:check %1 

echo All services stopped successfully! 
pause 
exit 

在第二FOR語句,當ERRORLEVEL = 0時,文件運行的其餘部分,而不是去到下一個FOR項目。我如何按預期做好這項工作?

謝謝!

+0

你需要'轉到:eof'在您的子程序(它可以作爲一個'return'語句) – wmz 2012-08-13 12:06:39

回答

3

您忘記批處理文件沒有控制結構。您必須通過從批處理文件末尾刪除或通過exit /b明確終止每個子例程。批處理文件的主體也是如此(以避免犁入第一個子例程的主體)。

此外,你必須在你的check子程序無限期遞歸,很快就會耗盡cmd的嵌套能力。使用普通的goto

所以:

.................... 
FOR /F "tokens=2* delims=.=" %%A IN ('SET ORACLESERVICES.') DO call:check %%B 

echo All services stopped successfully! 
pause 
exit 
rem If you change your mind and remove the exit above, you'll need this: 
goto :EOF 

:check 
    sc query %1 | find /I "State" | find /I "STOPPED" 
    IF ERRORLEVEL 1 goto check 
    goto :EOF 
    rem Technically you don't need the above line when this subroutine 
    rem is at the end of the batch file, but it's a good idea to put it 
    rem in anyway for clarity and to simplify future modifications