我有以下代码检查日志文件中的特定字符串,并根据匹配的日期戳执行某些任务。
现在,以下代码在日期时间格式为:YY-MM-DD hh:mm的Windows 7计算机上运行良好,但在Windows Server 2008上执行完全相同的批处理文件,日期时间格式为:YY -MM-DD hh:mm它不起作用 - 我怀疑它可能是日期时间格式...... 任何人都可以确认批处理文件中使用的日期时间格式是否适用于YY-DD-MM日期格式?
另外,如果日志文件中的日期时间格式与日志文件本身的dat-time格式不同,该怎么办?代码仍然有用吗?
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 . . .
如您所见,由于格式不正确,所以比较的日期beign将永远不会匹配。
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
)
答案 0 :(得分:2)
我注意到的第一件事是,如果你想在FOR循环中询问提交者的运行时值,你需要SETLOCAL ENABLEDELAYEDEXPANSION然后使用!代替 %。最有可能它恰好在Windows 7上工作,因为成功和归档的加载时间值是相同的。请尝试下面的代码。你可能会发现还有其他问题......但是我们先这样做。请注意,成功不会在FOR / IF构造中被询问,但归档是。
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
)