VBS回显来自txt文件

时间:2015-06-22 17:33:13

标签: vbscript

大家好我通常会通过谷歌挖掘或尝试我自己对vbs自动化的看法,但是我从txt文件中提取了数据。

所以我需要做的是让vbs从txt文件中提取特定数据并回显它

.eg Wscript.echo""& Total Dials&""

仅供参考,这是我拨打的总拨号数量 。以下是txt文件中数据的示例

 Agent Time Log:
 ---------------
      Agent-Hours Logged                   311.46
      Agent-Hours Talking                  159.67
      % Talking/Logged                      51.27
      Average minutes talking per call       0.76
      Agent-Hours Wrap-Up                    6.70
      % Wrap-Up/Logged                       2.15
      Agent-Hours Viewing                    0.00
      % Viewing/Logged                       0.00
      Total Conferencing Time                0.00
      Total Pre-Conference Time              0.00
      Total Transfer Call Time               0.00
 Dialing Results:
 ----------------
      Outbound Campaign                    
        Total Dials                             209
        Total Answers                           0
        Analysis Overrides                      0
        % Analysis Overrides/Answers         0.00

提前致谢

2 个答案:

答案 0 :(得分:3)

遵循Ekkehard.Horner提出的想法,但没有很多RegExp(s)

Option Explicit

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

Dim colMatches, oMatch
    With New RegExp
        .Global = True
        .Multiline = True
        .Pattern = "^\s*(\S.*?)(?:\s{2,}|[ ]*\t\s*)([0-9\.]+)\s*$"
        Set colMatches = .Execute( fso.OpenTextFile("data.txt").ReadAll )
    End With

    For Each oMatch In colMatches
        dic.Add oMatch.SubMatches(0), oMatch.SubMatches(1)
    Next 

    WScript.Echo dic.Item("Total Dials")
    WScript.Echo dic.Item("% Wrap-Up/Logged")

代码只读取完整文件,搜索以零或多个空格开头的行,后跟一些文本,至少两个空格的序列,数字序列,零个或多个空格以及行的结尾。

找到的每一行都是Match对象。每个Match对象包含每个捕获组的子匹配集合(括号中括起的正则表达式部分)。此信息用于填充字典,使用文本作为键,数字序列作为值。

答案 1 :(得分:2)

使用(很多)RegExp(s)来剪切值,使用Dictionary或Class来存储它们以便进一步处理:

>> s = Join(Array("Outbound Campaign", " Total Dials    209", "Total Answers"), vbCrLf)
>> Set r = New RegExp
>> r.Pattern = "Total\sDials\s+(\d+)"
>> WScript.Echo CLng(r.Execute(s)(0).SubMatches(0))
>>
209