VBS 从 txt 文件回显特定数据

VBS echo specific data from txt file

你好,我通常会深入研究 google 或尝试我自己能想到的 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

提前致谢

使用(很多)RegExp 来切割值和字典或 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

遵循 Ekkehard.Horner 提出的想法,但没有 大量 RegExp

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 对象都包含每个捕获组的 Submatches 集合(括号中正则表达式的部分)。此信息用于填充字典,使用文本作为键,使用数字序列作为值。