搜索日志文件并根据日志文件中的字符串将文件名记录在两个不同部分的文本文件中

时间:2014-09-14 11:03:48

标签: batch-file

我想编写一个批处理文件,可以在当前目录的所有日志文件中搜索Script Compiled Successfully,并在一个部分中将包含此字符串的文件名打印到Summarized.txt以及文件中所有日志文件的名称,不包含第二部分的字符串。

我有一个批处理代码,可以将包含此字符串的文件打印到Summarized.txt。我想修改它,以便它可以打印文件名和预定义的消息。不包含字符串的文件也应包含在Summarized.txt

如果所有文件都包含搜索字符串,则输出消息None应在Script Not Compiled Successfully部分写入Summarized.txt

Summarized.txt的输出应如下所示:

**********Script Compiled Successfully********************

1234.log
3456.log
7899.log

**********Script Not Compiled Successfully****************

8888.log
6666.log

**********End*********************************************

这是批次代码。非常感谢任何帮助。

@echo off
findstr /m /C:"Script Compiled Successfully" *.log > Summarized.txt
if %errorlevel%==0 (
    echo Found! logged files into Summarized.txt
) else (
    echo No matches found
)

@pause

1 个答案:

答案 0 :(得分:0)

以下是此任务的批处理文件。这很简单。

我在代码上面添加了一些评论,我认为需要一些解释。

@echo off
rem Delete temporarily used files if exist from a previous
rem (aborted) run as it is important that those 2 files do
rem not exist because of the two conditions after the loop.
if exist Success.txt del Success.txt
if exist Failed.txt del Failed.txt

rem Run command findstr on each log file individually as it must be
rem also recorded in which log files the string could not be found.
for %%F in (*.log) do (
    %SystemRoot%\System32\findstr.exe /M "/C:Script Compiled Successfully" "%%F" >nul
    if errorlevel 1 (
        echo %%F>>Failed.txt
    ) else (
        echo %%F>>Success.txt
    )
)

echo **********Script Compiled Successfully********************>Summarized.txt
echo.>>Summarized.txt

rem Was the string found in any log file?
if exist Success.txt (
    type Success.txt >>Summarized.txt
    del Success.txt
) else (
    echo None>>Summarized.txt
)

echo.>>Summarized.txt
echo **********Script Not Compiled Successfully****************>>Summarized.txt
echo.>>Summarized.txt

rem Was the string not found in any log file?
if exist Failed.txt (
    type Failed.txt >>Summarized.txt
    del Failed.txt
) else (
    echo None>>Summarized.txt
)

echo.>>Summarized.txt
echo **********End*********************************************>>Summarized.txt
相关问题