2014-10-04 177 views
0

我有以下批處理腳本批處理腳本只有第一個if語句被執行

@echo off 
cd %USERPROFILE%\desktop 

IF EXIST output.txt (
    FOR /F "tokens=*" %%G IN (output.txt) DO (
    echo %%G 
    IF "%%G" == "BUILD FAILED" (
     echo failed 
     pause 
     exit (1) 
    ) 
    IF "%%G" == "BUILD SUCCESS" (
     echo success 
     pause 
     exit (0) 
    ) 
) 
) 
pause 

它從具有一行文本文件中讀取,完成構建成功或建立失效。我確定的是,如果BUILD SUCCESS if語句首先被列出並且BUILD SUCCESS在文件中,它將按預期退出。但是,如果BUILD SUCCESS位於文件中,並且BUILD SUCCESS if語句是第二位(如代碼示例中所示),它從不捕獲它並在文件末尾暫停。我的語法突出顯示是用圓括號做奇怪的事情,所以我認爲這是我如何分組的東西,但我無法弄清楚。

回答

1

刪除exit命令中的括號。

+0

韋爾普,這很簡單。 – gfree 2014-10-04 15:30:18

1

這個腳本完全適合我。我希望它給你,你需要解決您的問題的思路:

@ECHO off 
ECHO Start of script 
IF NOT DEFINED T set T=Undefined Title 
TITLE %T% 
::cd %USERPROFILE%\desktop 
IF NOT EXIST output.txt (
    ECHO Generating sample file... 
    ECHO BUILD SUCCESS>output.txt 
    ECHO BUILD FAILED>>output.txt 
) 

IF EXIST output.txt (
    FOR /F "tokens=*" %%G IN (output.txt) DO (
    echo %%G 
    IF "%%G" == "BUILD FAILED" (
     echo failed 
     GOTO :ERR 
    ) 
    IF "%%G" == "BUILD SUCCESS" (
     echo success 
    ) 
) 
) 
GOTO :END 
:ERR 
ECHO Failed. Will close in 10 seconds... 
FOR /l %%a in (10,-1,1) do (TITLE %T% -- closing in %%as&ping -n 2 -w 1000 127.1>NUL) 
EXIT 1 
:END 
ECHO End of script. Will close in 10 seconds... 
FOR /l %%a in (10,-1,1) do (TITLE %T% -- closing in %%as&ping -n 2 -w 1000 127.1>NUL) 
EXIT 0 
1

這裏有一個替代文字:

@echo off 
find "BUILD SUCCESS" < "%USERPROFILE%\desktop\output.txt" >nul && (echo success & pause & exit 0) 
find "BUILD FAILED" < "%USERPROFILE%\desktop\output.txt" >nul && (echo failed & pause & exit 1)