0

我有以下代码检查特定字符串的日志文件,并根据匹配的日期戳执行某些任务。

现在下面的代码在日期时间格式为: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
)
4

1 回答 1

2

我注意到的第一件事是,如果你想在 FOR 循环中询问 filedate 的运行时值,你需要 SETLOCAL ENABLEDELAYEDEXPANSION 然后使用 ! 代替 %。很可能它恰好在 Windows 7 上工作,因为 success 和 filedate 的加载时间值是相同的。试试下面的代码。您可能会发现还有其他问题……但让我们先执行此操作。请注意,在 FOR/IF 构造中不询问成功,但 filedate 是。

setlocal enabledelayedexpansion
for /f "tokens=2" %%a in ('findstr /i /c:"DATABASE IS OK" log.txt') do set "success=%%a"
set success=%success:/=-%
for %%a in (log.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%]
if "!filedate:~0,10!"=="%success%" (
    call another.bat
) else (
    >>otherlogfile.log echo(%date% %time% DATABASE UNSUCCESSFUL
)
于 2013-07-31T15:16:08.930 回答