一、V1版本:通过bat脚本运行程序,并将log保存成时间命名的log文件中。
代码如下:
@echo off
set "current_path=%~dp0"
set "log_file_name=%date:~0,4%-%date:~5,2%-%date:~8,2%_%time:~0,2%-%time:~3,2%-%time:~6,2%.log"
if not exist %current_path%\log (mkdir %current_path%\log)
python -u test_39.py >> %current_path%\log\%log_file_name%
if %ERRORLEVEL% neq 0 (
echo The command failed with error code %ERRORLEVEL%
) else (
echo The command succeeded.
)
pause
二、V2版本:兼容不同系统编码集(utf-8,中文编码集:936)
系统默认编码集为utf-8,date显示格式如下:
代码如下:
@echo off
for /F "tokens=2 delims=:" %%a in ('chcp') do set "codepage=%%a"
if %codepage% == 65001 (
set "log_file_name=%date:~3,4%-%date:~8,2%-%date:~11,2%_%time:~0,2%-%time:~3,2%-%time:~6,2%.log"
) else (
set "log_file_name=%date:~0,4%-%date:~5,2%-%date:~8,2%_%time:~0,2%-%time:~3,2%-%time:~6,2%.log"
)
set "current_path=%~dp0"
if not exist %current_path%\log (mkdir %current_path%\log)
python -u test_37.py >> %current_path%\log\%log_file_name%
if %ERRORLEVEL% neq 0 (
echo The command failed with error code %ERRORLEVEL%
) else (
echo The command succeeded.
)
pause
三、V3版本:修改系统时间小于10时,文件命名出错问题
系统变量time,小于10点时会填充空格,导致处理log文件名出错。
修改代码如下:
@echo off
set hour=%time:~0,2%
set hour=%hour: =0%
for /F "tokens=2 delims=:" %%a in ('chcp') do set "codepage=%%a"
if %codepage% == 65001 (
set "log_file_name=%date:~3,4%-%date:~8,2%-%date:~11,2%_%hour%-%time:~3,2%-%time:~6,2%.log"
) else (
set "log_file_name=%date:~0,4%-%date:~5,2%-%date:~8,2%_%hour%-%time:~3,2%-%time:~6,2%.log"
)
set "current_path=%~dp0"
if not exist %current_path%\log (mkdir %current_path%\log)
python -u test_37.py >> %current_path%\log\%log_file_name%
if %ERRORLEVEL% neq 0 (
echo The command failed with error code %ERRORLEVEL%
) else (
echo The command succeeded.
)
pause