在VBS中解析日志文件中的字符串

时间:2014-05-17 08:00:36

标签: windows batch-file logging vbscript string-parsing

我有一个Windows系统将日志信息记录到文本文件中。它是一个bat文件,它将cygwin命令的当前日期,时间和结果记录到文本文件中。

当前文本文件位于下方。

Fri 05/16/2014 16:52:12.19 
JAVA_HOME = C:/jdk1.6.0_07
Checking rmi://ppm01:1094/KintanaServer
--> running (load: 343.0, mode: NORMAL)

Checking rmi://ppm02:1197/KintanaServer
--> running (load: 318.0, mode: NORMAL)

Checking rmi://ppm03:1297/KintanaServer
--> running (load: 0.0, mode: NORMAL)

------------------------------------------- 
Fri 05/16/2014 16:57:00.03 
JAVA_HOME = C:/jdk1.6.0_07
Checking rmi://ppm01:1094/KintanaServer
--> running (load: 334.0, mode: NORMAL)

Checking rmi://ppm02:1197/KintanaServer
--> running (load: 334.0, mode: NORMAL)

Checking rmi://ppm03:1297/KintanaServer
--> running (load: 0.0, mode: NORMAL)

我想将文本文件更改为

Fri 05/16/2014 16:52:12.19;ppm01;343
Fri 05/16/2014 16:52:12.19;ppm01;318
Fri 05/16/2014 16:52:12.19;ppm03;0
Fri 05/16/2014 16:57:00.03;ppm01;334
Fri 05/16/2014 16:57:00.03;ppm02;334 
Fri 05/16/2014 16:57:00.03;ppm02;0

如何使用VBS操作当前文本文件以获取信息。

我无法更改cygwin中的命令也是cygwin命令的结果。所以我唯一能做的就是改变已经写好的信息。

这是我的批处理脚本:

@echo off
C:
cd C:\ppm\bin
echo %date% %time% >> userload_log.txt
C:\cygwin\bin\bash.exe kStatus.sh >> userload_log.txt

1 个答案:

答案 0 :(得分:1)

对于文件格式更改(批处理文件,而不是vbscript)

@echo off
    setlocal enableextensions enabledelayedexpansion

    set /a "mon=100", "tue=100", "wed=100", "thu=100", "fri=100", "sat=100", "sun=100"
    set /a "checking=200", "running=300"

    for /f "usebackq tokens=1-8 delims=>-/:. " %%a in ("userload_log.txt") do (
        set /a "id=%%a"
        if !id! equ 100 (
            set "ts=%%a %%b/%%c/%%d %%e:%%f:%%g.%%h;"
        ) else if !id! equ 200 (
            set "ppm=%%c;"
        ) else if !id! equ 300 (
            echo(!ts!!ppm!%%c
        )
    )

此代码对行进行标记,并使用第一个标记来确定可从中检索的信息。其余的是在变量中保存加密的标记,每次检索完整的集合时,回显以控制所需的记录

对于VBS版本

Option Explicit

Const ForReading = 1

Dim fso
    Set fso = WScript.CreateObject("Scripting.FileSystemObject")

Dim inputFile
    Set inputFile = fso.OpenTextFile("userload_log.txt", ForReading )

Dim reDate, reChecking, reRunning    

Dim inputLine, id, ts, ppm

    Do While Not inputFile.AtEndOfStream
        inputLine = Trim(inputFile.ReadLine())
        If Len(inputLine) > 0 Then
            id = split(LCase(inputLine)," ")(0)
            Select Case id
                Case "mon","tue","wed","thu","fri","sat","sun"
                    ts = inputLine
                Case "checking"
                    ppm = Split(Mid(inputLine,16),":")(0)
                Case "-->"
                    WScript.StdOut.WriteLine ts & ";" & ppm & ";" & Split(Mid(inputLine,20),".")(0) 
            End Select
        End If
    Loop

    inputFile.Close()