如何在jar文件归档失败检测错误? [英] How to detect error if archive of jar file failed?
问题描述
我现在面临一个问题,即0%ERRORLEVEL%返回值,即使jar文件归档失败。
I'm facing a problem where %errorlevel% return value of 0 even when archive of jar files failed.
我试图做到的,是存档在生产环境中的日志文件。我使用ROBOCOPY命令复制日志文件到一个文件夹中分离并尝试存档这些日志文件。 验证> NUL被用来重置%ERRORLEVEL%返回到0,因为会的Robocopy后执行。其值设置为1。
What I'm trying to achieve is to archive log files in production environment. I use ROBOCOPY command to copy log files to a separated folder and try to archive those log files. "verify > nul" was used to reset the %errorlevel% back to 0 because the robocopy will set the value to 1 after execution.
然而,当jar命令的执行过程中发生错误(即没有文件存档),%ERRORLEVEL%仍然为0,程序继续进行。
However, when the error happened during the execution of jar command (i.e. no file to archive), %errorlevel% remain as 0 and program continue.
@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
请注意:Varible从属性文件读取。第三方库不允许安装,所以我选择使用jar命令
Note: Varible are read from properties file. 3rd party library were not allowed to install, therefore I choose to use the jar command
任何暗示如何检测是否发生错误?
Any hint how to detect if error occurred?
感谢。
推荐答案
作为一种替代方法,大卫的(使用延迟扩展)的,我支持+1,你可能使用 如果错误级别
命令来代替。我会做。
As an alternative to David method (use delayed expansion) which I support +1, you might use the if errorlevel
command instead. I'd do.
if errorlevel 1 goto error
阅读雷蒙德陈输入 ERRORLEVEL并不%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
)
)
这篇关于如何在jar文件归档失败检测错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!