Need Vbscript help with parsing a file

  • Thread starter Thread starter jamstraz
  • Start date Start date
J

jamstraz

My boss has asked me to write a vbscript to parse data from a text
file, but the fun thing is I have 0 experience with doing anything like
this. But here's the situation:

I have a batch file that downloads 3 files from an ftp site and then
stores the output of the transfer statistics to a textfile. Anothe
batchfile uses regular expression to remove extraneous lines so that I
now only have 3 lines (1 for each of the files)

Ex (In notepad, file is c:\receivelog.log)
---------- C:\FTPLOG.LOG
ftp: 176768000 bytes received in 36.11Seconds 4895.81Kbytes/sec.

ftp: 31838208 bytes received in 7.12Seconds 4471.03Kbytes/sec.

ftp: 1411072 bytes received in 0.27Seconds 5304.78Kbytes/sec.


What needs to be done is to somehow extract the data per each run of
the batch files (this file will contain different stats the next time
it is recreated by running the batch files) and then place it into
another file that can then be used by Excel to create charts to examine
bandwidth statistics. The file I would need would look something like:

36.11 4895.81
7.12 4471.03
0.27 5302.78

at first of course, and after each successive run, this file would have
more statistics listed per each iteration.

PLEASE HELP!!!! I'm on a deadline.

An exact code would be great, and I would credit you in the file...
 
I'd be inclined to modify the code that uses a regex to remove unwanted
lines to capture and output the values you want. For lines that match
this pattern (or something similar)

^ftp: \d+ bytes received in ([0-9\.]+)Seconds ([0-9\.]+)Kbytes/sec.$

output $1\t$2 or "$1" & Chr(9) & "$2" or whatever the language you're
using requires.

With Perl you can do it with a single command in a batch file (all on
one line, replacing XXX and YYY with the paths to the files you want to
read and write respectively):

perl -ne "print qq[$1\t$2\n] if m[^ftp: \d+ bytes received in
([0-9\.]+)Seconds ([0-9\.]+)Kbytes/sec.$]" XXX > YYY

In VBScript it's a lot more work. Here's a rough script, based on a
template I happened to have by me:

'aaTEMPLATE.vbs
'Template script for processing a text file line by line, doing
something
'to each line

Option Explicit
Const BAK_EXT = ".bak"
Dim fso 'FileSystemObject
Dim fF 'File
Dim fIn, fOut 'Textstreams
Dim strL 'String

Dim oRE 'Regex Object

'Regular Expression
Const Pattern = "^ftp: \d+ bytes received in ([0-9\.]+)Seconds
([0-9\.]+)Kbytes/sec.$"
Const Replacement = "$1\t$2"


Function ProcessLine(ByVal strS)
'This does all the work.
'Insert code here that returns a string to
'write to the output file
If oRE.Test(strS) Then
strS = oRE.Replace(strS, "$1" & Chr(9) & "$2")
Else
strS = ""
End If
ProcessLine = strS
End Function


'Main body of script iterates through the file
'applying the ProcessLine function to each line
If WScript.Arguments.Count = 1 Then
Set fso = CreateObject("Scripting.FileSystemObject")
Set fF = fso.GetFile(WScript.Arguments(0))

Set oRE = CreateObject("VBScript.Regexp")
With oRE
.Pattern = Pattern
End With

'If backup file aready exists, delete it
If fso.FileExists(fF.Path & BAK_EXT) Then
fso.DeleteFile fF.Path & BAK_EXT
End If
fF.Name = fF.Name & BAK_EXT
Set fIn = fF.OpenAsTextStream(1) 'ForReading
Set fOut = fso.CreateTextFile(WScript.Arguments(0))

Do 'Read line by line
strL = fIn.ReadLine
strL = ProcessLine(strL)
If Len(strL) > 0 Then
fOut.Write strL & vbCRLF
End If
Loop Until fIn.AtEndOfStream

'Tidy up
fIn.Close
fOut.Close
Else
MsgBox "Drag a file onto the icon to process it. " _
& vbcrlf & "Original file will be renamed with " _
& ".bak extension"
End If
 
Well I'm guessing this would work but I need a vbscript as is to make
it work, I don't have any compilers or anything like that. He wants a
single vbscript to do this. I can't do it any other way, he's adamant
I'd be inclined to modify the code that uses a regex to remove unwanted
lines to capture and output the values you want. For lines that match
this pattern (or something similar)

^ftp: \d+ bytes received in ([0-9\.]+)Seconds ([0-9\.]+)Kbytes/sec.$

output $1\t$2 or "$1" & Chr(9) & "$2" or whatever the language you're
using requires.

With Perl you can do it with a single command in a batch file (all on
one line, replacing XXX and YYY with the paths to the files you want to
read and write respectively):

perl -ne "print qq[$1\t$2\n] if m[^ftp: \d+ bytes received in
([0-9\.]+)Seconds ([0-9\.]+)Kbytes/sec.$]" XXX > YYY

In VBScript it's a lot more work. Here's a rough script, based on a
template I happened to have by me:

'aaTEMPLATE.vbs
'Template script for processing a text file line by line, doing
something
'to each line

Option Explicit
Const BAK_EXT = ".bak"
Dim fso 'FileSystemObject
Dim fF 'File
Dim fIn, fOut 'Textstreams
Dim strL 'String

Dim oRE 'Regex Object

'Regular Expression
Const Pattern = "^ftp: \d+ bytes received in ([0-9\.]+)Seconds
([0-9\.]+)Kbytes/sec.$"
Const Replacement = "$1\t$2"


Function ProcessLine(ByVal strS)
'This does all the work.
'Insert code here that returns a string to
'write to the output file
If oRE.Test(strS) Then
strS = oRE.Replace(strS, "$1" & Chr(9) & "$2")
Else
strS = ""
End If
ProcessLine = strS
End Function


'Main body of script iterates through the file
'applying the ProcessLine function to each line
If WScript.Arguments.Count = 1 Then
Set fso = CreateObject("Scripting.FileSystemObject")
Set fF = fso.GetFile(WScript.Arguments(0))

Set oRE = CreateObject("VBScript.Regexp")
With oRE
.Pattern = Pattern
End With

'If backup file aready exists, delete it
If fso.FileExists(fF.Path & BAK_EXT) Then
fso.DeleteFile fF.Path & BAK_EXT
End If
fF.Name = fF.Name & BAK_EXT
Set fIn = fF.OpenAsTextStream(1) 'ForReading
Set fOut = fso.CreateTextFile(WScript.Arguments(0))

Do 'Read line by line
strL = fIn.ReadLine
strL = ProcessLine(strL)
If Len(strL) > 0 Then
fOut.Write strL & vbCRLF
End If
Loop Until fIn.AtEndOfStream

'Tidy up
fIn.Close
fOut.Close
Else
MsgBox "Drag a file onto the icon to process it. " _
& vbcrlf & "Original file will be renamed with " _
& ".bak extension"
End If

My boss has asked me to write a vbscript to parse data from a text
file, but the fun thing is I have 0 experience with doing anything like
this. But here's the situation:

I have a batch file that downloads 3 files from an ftp site and then
stores the output of the transfer statistics to a textfile. Anothe
batchfile uses regular expression to remove extraneous lines so that I
now only have 3 lines (1 for each of the files)

Ex (In notepad, file is c:\receivelog.log)
---------- C:\FTPLOG.LOG
ftp: 176768000 bytes received in 36.11Seconds 4895.81Kbytes/sec.

ftp: 31838208 bytes received in 7.12Seconds 4471.03Kbytes/sec.

ftp: 1411072 bytes received in 0.27Seconds 5304.78Kbytes/sec.


What needs to be done is to somehow extract the data per each run of
the batch files (this file will contain different stats the next time
it is recreated by running the batch files) and then place it into
another file that can then be used by Excel to create charts to examine
bandwidth statistics. The file I would need would look something like:

36.11 4895.81
7.12 4471.03
0.27 5302.78

at first of course, and after each successive run, this file would have
more statistics listed per each iteration.

PLEASE HELP!!!! I'm on a deadline.

An exact code would be great, and I would credit you in the file...
 
That's why I gave you a VBScript.

Well I'm guessing this would work but I need a vbscript as is to make
it work, I don't have any compilers or anything like that. He wants a
single vbscript to do this. I can't do it any other way, he's adamant
I'd be inclined to modify the code that uses a regex to remove unwanted
lines to capture and output the values you want. For lines that match
this pattern (or something similar)

^ftp: \d+ bytes received in ([0-9\.]+)Seconds ([0-9\.]+)Kbytes/sec.$

output $1\t$2 or "$1" & Chr(9) & "$2" or whatever the language you're
using requires.

With Perl you can do it with a single command in a batch file (all on
one line, replacing XXX and YYY with the paths to the files you want to
read and write respectively):

perl -ne "print qq[$1\t$2\n] if m[^ftp: \d+ bytes received in
([0-9\.]+)Seconds ([0-9\.]+)Kbytes/sec.$]" XXX > YYY

In VBScript it's a lot more work. Here's a rough script, based on a
template I happened to have by me:

'aaTEMPLATE.vbs
'Template script for processing a text file line by line, doing
something
'to each line

Option Explicit
Const BAK_EXT = ".bak"
Dim fso 'FileSystemObject
Dim fF 'File
Dim fIn, fOut 'Textstreams
Dim strL 'String

Dim oRE 'Regex Object

'Regular Expression
Const Pattern = "^ftp: \d+ bytes received in ([0-9\.]+)Seconds
([0-9\.]+)Kbytes/sec.$"
Const Replacement = "$1\t$2"


Function ProcessLine(ByVal strS)
'This does all the work.
'Insert code here that returns a string to
'write to the output file
If oRE.Test(strS) Then
strS = oRE.Replace(strS, "$1" & Chr(9) & "$2")
Else
strS = ""
End If
ProcessLine = strS
End Function


'Main body of script iterates through the file
'applying the ProcessLine function to each line
If WScript.Arguments.Count = 1 Then
Set fso = CreateObject("Scripting.FileSystemObject")
Set fF = fso.GetFile(WScript.Arguments(0))

Set oRE = CreateObject("VBScript.Regexp")
With oRE
.Pattern = Pattern
End With

'If backup file aready exists, delete it
If fso.FileExists(fF.Path & BAK_EXT) Then
fso.DeleteFile fF.Path & BAK_EXT
End If
fF.Name = fF.Name & BAK_EXT
Set fIn = fF.OpenAsTextStream(1) 'ForReading
Set fOut = fso.CreateTextFile(WScript.Arguments(0))

Do 'Read line by line
strL = fIn.ReadLine
strL = ProcessLine(strL)
If Len(strL) > 0 Then
fOut.Write strL & vbCRLF
End If
Loop Until fIn.AtEndOfStream

'Tidy up
fIn.Close
fOut.Close
Else
MsgBox "Drag a file onto the icon to process it. " _
& vbcrlf & "Original file will be renamed with " _
& ".bak extension"
End If

My boss has asked me to write a vbscript to parse data from a text
file, but the fun thing is I have 0 experience with doing anything like
this. But here's the situation:

I have a batch file that downloads 3 files from an ftp site and then
stores the output of the transfer statistics to a textfile. Anothe
batchfile uses regular expression to remove extraneous lines so that I
now only have 3 lines (1 for each of the files)

Ex (In notepad, file is c:\receivelog.log)
---------- C:\FTPLOG.LOG
ftp: 176768000 bytes received in 36.11Seconds 4895.81Kbytes/sec.

ftp: 31838208 bytes received in 7.12Seconds 4471.03Kbytes/sec.

ftp: 1411072 bytes received in 0.27Seconds 5304.78Kbytes/sec.


What needs to be done is to somehow extract the data per each run of
the batch files (this file will contain different stats the next time
it is recreated by running the batch files) and then place it into
another file that can then be used by Excel to create charts to examine
bandwidth statistics. The file I would need would look something like:

36.11 4895.81
7.12 4471.03
0.27 5302.78

at first of course, and after each successive run, this file would have
more statistics listed per each iteration.

PLEASE HELP!!!! I'm on a deadline.

An exact code would be great, and I would credit you in the file...
 
Back
Top