TriniCurry
TriniCurry

Reputation: 57

VBS echo specific data from txt file

Hello All i normally dig through google or try what i can think of myself for vbs automation but i hit a wall on this pulling data from txt file.

so what i need to do is to have vbs pull specific data from a txt file and echo it

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

FYI its the number for total dials i'm trying to echo .the following is an example of the data in the txt file

 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

Thanks in advance

Upvotes: 0

Views: 114

Answers (2)

MC ND
MC ND

Reputation: 70923

Following the idea exposed by Ekkehard.Horner, but without the lot of 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")

The code just reads the full file, search for lines that start with zero or more blanks, followed by some text, a sequence of at least two spaces, a numeric sequence, zero or more spaces and the end of the line.

Each line found is a Match object. Each Match object contains a Submatches collection for each of the capture groups (the parts of the regular expression enclosed in parenthesis). This information is used to populate a dictionary, using the text as a key and the numeric sequence as value.

Upvotes: 3

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

Use a (lot of) RegExp(s) to cut the values and a Dictionary or Class to store them for further processing:

>> 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

Upvotes: 2

Related Questions