我有以下代码检查特定字符串的日志文件,并根据匹配的日期戳执行某些任务。
现在下面的代码在日期时间格式为:YY-MM-DD hh:mm 的 Windows 7 机器上运行良好,但在 Windows Server 2008 上执行完全相同的批处理文件,日期时间格式为:YY-MM- DD hh:mm 它不起作用 - 我怀疑它可能是日期时间格式......有人可以确认批处理文件中使用的日期时间格式是否适用于 YY-DD-MM 日期格式吗?
另外,如果日志文件中的日期时间格式与日志文件本身的日期时间格式不同怎么办?代码还能用吗?
for /f "tokens=2" %%a in ('findstr /i /c:"DATABASE IS OK" log.txt') do set "success=%%a"
for %%a in (log.txt) do set "filedate=%%~ta"
if "%filedate:~0,10%"=="%success%" (
call another.bat
) else (
>>otherlogfile.log echo(%date% %time% DATABASE UNSUCCESSFUL
)
谢谢
更新1:
C:\Utilities\Filter>for %a in (logfile.txt) do set "filedate=%~ta"
C:\Utilities\Filter>set "filedate=2013-07-31 21:31"
C:\Utilities\Filter>REM If it still does not work remove REM from next
line so we can see what is being compared
C:\Utilities\Filter>ECHO.filedate=!filedate:~0,10!] success=2013/07/
31]
filedate=2013-07-31] success=2013/07/31]
C:\Utilities\Filter>pause
Press any key to continue . . .
如您所见,比较的日期永远不会匹配,因为格式不正确。
filedate=2013-07-31] success=2013/07/31]
你有什么建议?
更新 2:
setlocal enabledelayedexpansion
for /f "tokens=2" %%a in ('findstr /i /c:"DATABASE IS OK" logfile.txt') do set "success=%%a"
set "%success:^/=-%"
echo %success%
pause
for %%a in (logfile.txt) do set "filedate=%%~ta"
REM If it still does not work remove REM from next line so we can see what is being compared
ECHO.filedate=!filedate:~0,10!] success=%success%]
pause
if "!filedate:~0,10!"=="%success%" (
call another.bat
) else (
>>readlogFail.txt echo(%date% %time% DATABASE UNSUCCESSFUL
)