在动态日志文件中搜索字符串

时间:2013-09-05 04:35:14

标签: batch-file

我想为以下内容编写一个Windows批处理脚本。

我在路径C:\test\下有一些日志文件,每天都会在其名称中创建当天的日期。即。

u_ex130828.log     (created on 28/08/2013)
u_ex130827.log     (created on 28/08/2013)
u_ex130826.log     (created on 28/08/2013)

我想在今天创建的所有文件中搜索特定的错误消息。 一旦找到错误消息,然后立即将包含错误消息的完整行附加到名为Output.txt的文本文件中,如果找不到错误消息,则不要附加Output.txt文件。

2 个答案:

答案 0 :(得分:2)

这应该做你需要的:

@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set "dt=%%a"
set "YY=%dt:~2,2%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"

set "today=%YY%%MM%%DD%"

findstr /i /c:"File attached above for last 2 occasions." "C:\inetpub\logs\LogFiles\W3SVC1\*%today%.log" > "C:\inetpub\logs\LogFiles\W3SVC1\output.txt"

答案 1 :(得分:1)

cd /d C:\inetpub\logs\LogFiles\W3SVC1\
set "today=%date:~-4%%date:~-10,2%%date:~-7,2%"
for %%a in (*%today%.log) do (
    for /f "delims=" %%b in ('findstr /c:"File attached above for last 2 occasions." "%%~a"') do (
        (echo %%~a: %%~b)>>Output.txt
    )
)

根据需要修改文件搜索模式*%today%.log

相关问题