批處理檢測執行結果並返回成功或失敗

 

result.txt 文件中寫入了執行結果(文本),如果文件文本包含failed,則認爲執行失敗;否則認爲執行成功。

@echo off
REM ********Result Check********
REM First Edit 2019

set TargetPath=E:\jenkins\workspace\Smoke

set target_stresult=%TargetPath%\result.txt

set target_st_result=%TargetPath%\ST_result.txt

if exist %target_st_result% del %target_st_result%

findstr "failed" %target_stresult% >%target_st_result%


if errorlevel 1 goto succ
if errorlevel 0 goto fail

:fail
exit 1
goto end

:succ
cd %TargetPath%
goto end

:end

findstr  查找包含 “failed” 的行

 

errorlevel:

0複製文件成功 
1 未找到複製文件 
2 用戶通過CTRL C 終止了xcopy操作 
4 出現了初始化錯誤  
5 出現了磁盤寫入錯誤

 

 

擴展一則:

set  JenkinsPath=C:\CI_MMESGSN\jenkins\workspace\ST

qprocess | findstr "nginx.exe"
if %errorlevel% == 0 (
echo nginx server is running, no need to operator
) else (
echo nginx server is not running
cd %JenkinsPath%\nginx-1.11.10\nginx-1.11.10
start nginx
cd %JenkinsPath%
)

 

tasklist|find /i "nginx.exe">nul
if %errorlevel%==0 (
    if exist "%aaa%\config\user\hahaha.txt" (
        echo %date% %time% >>"%ResultDir%\run.log"
        echo %psttpath% >>"%ResultDir%\run.log"
    )
)

 

發佈了143 篇原創文章 · 獲贊 58 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章