我正在考虑创建一个批处理文件,该文件从最近3个月的日志文件中提取txt文件中的信息,并输出包含以下信息的文件:
取自文件路径的计算机名称:
\\Server\e$\Users\\**COMPUTER_NAME**\path\search_file.txt
搜索文件中的总发生次数(很高兴 - 不必)
发生时间: 找到的文件的格式为:
09:24:08:17 (WS:PED:RunRequest)
Response: (0) None
Error : -2
因此...
COMPUTER_NAME
Number of occurrences found
Time of occurrence i.e. 09:24:08:17
我将此作为开头:
@echo off
echo.
echo Searching for all Error : -2...
setlocal enabledelayedexpansion
for %%a in (\\Sc0320svr0001\e$\Users\SC0320POS0003\E2ELOGS\ped_20140812_092355.dbg) do
(
set found=false
for /f "skip=2 tokens=*" %%b in ('find "Error : -2" "%%a"') do (
if "!found!"=="false" (
echo %%b >>output.txt
set found=true
)
)
)
有了这个,我只提取"错误:-2",并且可能有多个实例。
找到批处理进行计数:
@find /c /i "Error : -2" "\\Sc0320svr0001\e$\Users\SC0320POS0003\E2ELOGS\*.dbg" >>output.txt
但是,如果大于零,只能查看输出。
文件的一部分:
09:23:55:68 (WS:PED:OpenHandler)
Exit
09:23:55:76 (WS:PED:RunRequest)
Request: (1) Check PED status
09:23:55:86 (WS:PED:Write)
Data: 97
09:24:08:08 (WS:PED:Write)
Error: 30
09:24:08:17 (WS:PED:RunRequest)
Response: (0) None
Error : -2
Message :
Receipt :
答案 0 :(得分:0)
这只处理一个文件......检查它是否正在执行此任务:
@echo off
setlocal enableDelayedExpansion
set file_txt=apple.txt
set "current_line="
set "prev_line="
set "prev_prev_line="
set counter=0
for /f "tokens=* delims=" %%a in (%file_txt%) do (
rem echo -%%a-
if defined prev_line (
set prev_prev_line=!prev_line!
)
if defined current_line (
set prev_line=!current_line!
)
set "current_line=%%~a"
if "!current_line: =!" EQU "Error:-2" (
set /a counter=counter+1
echo(
echo Error -2 found
echo in %file_txt%
echo time is !prev_prev_line!
echo responces !prev_line!
echo(
)
)
echo total count %counter%
endlocal
编辑。处理多个文件
@echo off
setlocal enableDelayedExpansion
:: separate the file names with ;
set files_to_process=apple.txt;banana.txt;pear.txt
set "current_line="
set "prev_line="
set "prev_prev_line="
set counter=0
for %%f in (%files_to_process%) do (
for /f "tokens=* delims=" %%a in (%file_txt%) do (
rem echo -%%a-
if defined prev_line (
set prev_prev_line=!prev_line!
)
if defined current_line (
set prev_line=!current_line!
)
set "current_line=%%~a"
if "!current_line: =!" EQU "Error:-2" (
set /a counter=counter+1
echo(
echo Error -2 found
echo in %file_txt%
echo time is !prev_prev_line!
echo responces !prev_line!
echo(
)
)
)
echo total count %counter%
endlocal