0

jar ファイルのアーカイブに失敗した場合でも、%errorlevel% の戻り値が 0 になるという問題に直面しています。

私が達成しようとしているのは、運用環境でログ ファイルをアーカイブすることです。ROBOCOPY コマンドを使用して、ログ ファイルを別のフォルダーにコピーし、それらのログ ファイルをアーカイブしようとしています。robocopy は実行後に値を 1 に設定するため、「verify > nul」を使用して %errorlevel% を 0 にリセットしました。

ただし、jar コマンドの実行中にエラーが発生した場合 (つまり、アーカイブするファイルがない場合)、%errorlevel% は 0 のままで、プログラムは続行されます。

@ECHO off
SETLOCAL

ECHO [info] LogFileArchivaer Started at time %date% %time% ...
SET LOG_FILE_AGE_IN_DAY=7

REM Directory variable setting
FOR /F "tokens=1,2 delims==" %%G IN (config.properties) DO (

    IF "%%G"=="log.app.dir.ori" (
        IF NOT EXIST %%H\NUL (
            ECHO [error] Directory "%%H" not exist!
            GOTO ERROR
        )
        SET APP_LOG_DIR_ORI=%%H
    )

    IF "%%G"=="log.server.dir.ori" (
        IF NOT EXIST %%H\NUL (
            ECHO [error] Directory "%%H" not exist!
            GOTO ERROR
        )
        SET SERVER_LOG_DIR_ORI=%%H
    )
    IF "%%G"=="log.batch.dir.ori" (
        IF NOT EXIST %%H\NUL (
            ECHO [error] Directory "%%H" not exist!
            GOTO ERROR
        )
        SET BATCH_LOG_DIR_ORI=%%H
    )
    IF "%%G"=="log.app.dir.dest" (
        IF NOT EXIST %%H\NUL (
            ECHO [error] Directory "%%H" not exist!
            GOTO ERROR
        )
        SET APP_LOG_DIR_DEST=%%H
    )
    IF "%%G"=="log.server.dir.dest" (
        IF NOT EXIST %%H\NUL (
            ECHO [error] Directory "%%H" not exist!
            GOTO ERROR
        )
        SET SERVER_LOG_DIR_DEST=%%H
    )
    IF "%%G"=="log.batch.dir.dest" (
        IF NOT EXIST %%H\NUL (
            ECHO [error] Directory "%%H" not exist!
            GOTO ERROR
        )
        SET BATCH_LOG_DIR_DEST=%%H
    )
    IF "%%G"=="log.file.age" (
        SET /a LOG_FILE_AGE_IN_DAY="%%H"*1
    )
)

REM Prepare formatted system date
SET year=%date:~-4%

SET month=%date:~3,2%
IF "%month:~0,1%" == " " SET month=0%month:~1,1%

SET day=%date:~0,2%
IF "%day:~0,1%" == " " SET day=0%day:~1,1%

SET hour=%time:~0,2%
IF "%hour:~0,1%" == " " SET hour=0%hour:~1,1%

SET min=%time:~3,2%
IF "%min:~0,1%" == " " SET min=0%min:~1,1%

SET datetime_formatted=%year%%month%%day%_%hour%%min%

ECHO [info] Age of log files to be archived: [%LOG_FILE_AGE_IN_DAY%] days.

ECHO [info] === Processing batch job log files ===

ROBOCOPY %BATCH_LOG_DIR_ORI% %BATCH_LOG_DIR_DEST% *.log /MINAGE:%LOG_FILE_AGE_IN_DAY%
IF %errorlevel% GEQ 2 GOTO ERROR

REM Reset the errorLevel value back to 0
verify > nul

IF exist %BATCH_LOG_DIR_DEST%*.log (
    CD %BATCH_LOG_DIR_DEST%
    jar -cvMf %BATCH_LOG_DIR_DEST%batch_job_log_%datetime_formatted%.zip *.log

    IF %errorlevel% NEQ 0 GOTO ERROR

    FOR /F %%J IN ('dir /b /A-D %BATCH_LOG_DIR_DEST%*.log') DO (
        ECHO [info] Deleting file: %%J
        DEL %BATCH_LOG_DIR_DEST%%%J
        DEL %BATCH_LOG_DIR_ORI%%%J
        IF %errorlevel% NEQ 0 GOTO ERROR
    )
) ELSE (
    ECHO [info] No batch log file to process.
)

ECHO [info] === Batch job log files archived completed ===

GOTO COMPLETED

:ERROR
ECHO [error] Error Occurred, program exit at time %date% %time%!    
GOTO END

:COMPLETED
ECHO [info] LogFileArchivaer Completed at time %date% %time% ...    
GOTO END

:END

注: 変数はプロパティ ファイルから読み取られます。サードパーティ製ライブラリのインストールが許可されていなかったため、jar コマンドを使用することにしました

エラーが発生したかどうかを検出する方法のヒントはありますか?

ありがとう。

4

2 に答える 2

2

遅延展開を使用する

SETLOCAL EnableDelayedExpansion
...
    IF !errorlevel! NEQ 0 GOTO ERROR

スコープ内で変更された括弧スコープ内のすべての変数に対してこれを行うようにしてください。

括弧で囲まれたスコープ内で変数を展開するときはいつでも()、遅延展開または回避策で展開しない限り、変数はスコープが始まる前の値に展開されます。

@echo off
setlocal EnableDelayedExpansion

set "x=0"
echo %x%

if 1==1 (
    set "x=1"
    echo %x%
)
endlocal
于 2013-03-04T13:28:12.230 に答える
1

私が +1 をサポートしている David メソッド(遅延展開を使用)の代わりに、代わりにif errorlevelコマンドを使用することもできます。私はそうします。

if errorlevel 1 goto error

The Old New ThingブログのRaymond Chen エントリERRORLEVEL is not %ERRORLEVEL% http://blogs.msdn.com/b/oldnewthing/archive/2008/09/26/8965755.aspxを参照してください。

そして、この抜粋を試してください..

robocopy %ORI% %DEST% *.log ....
if errorlevel 2 goto error
...
if exist *.log ( 
   jar -cvMf ..
   if errorlevel 1 goto error
   for %%a in *.log do (
     del %%a ....
     if errorlevel 1 goto error
   )
)
于 2013-03-04T18:33:26.780 に答える