Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Reading contents of text file and search ...

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,3572
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Reading contents of text file and search ...
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Reading contents of text file and search ... - 6/29/2005 2:38:56 AM   
  BBistheKing

 

Posts: 107
Score: 0
Joined: 6/21/2005
Status: offline
I am new to programming and have tried modifying example scripts for this sort of thing I have found on the web (using FileSystemObject and ReadAll etc) but can't make it work. This is the scenario:

I use SendKeys to select-all and copy a web page (which is highly secure and url is invisible - we are authorised to view it but not tinker with it), which I then paste into a .txt file. I want to be able to find any instance of one of three things. For example, I want to look for either 256, 512, or 1.5 (one of those will be there).

Additionally, when one of them is found I want to be able to assign it to a variable so that I can use SendKeys to paste it into a different program. Also, is it possible that when an an instance of 1.5 is found, to convert it so that when I SendKeys the variable to the other program it will read as 150?

_____________________________

"Live simply, that others may simply live." - Mahatma Gandhi
 
 
Post #: 1
 
 Re: Reading contents of text file and search ... - 6/29/2005 2:16:24 PM   
  BBistheKing

 

Posts: 107
Score: 0
Joined: 6/21/2005
Status: offline
This works, but is there an easier way than this to do it? The reason I ask is that there is more information I want to extract from the text file than just the dsl speed.

Dim InputFile
Dim FSO, oFile, LineIn, LineNo
Dim strData
Dim TheVar, SpeedCode, ChargeCode

InputFile = "C:\My Documents\Scripts\Copy.txt"

Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFile = FSO.OpenTextFile(InputFile)

LineNo = 0
Do Until oFile.AtEndOfStream
LineIn = oFile.ReadLine
LineNo = LineNo + 1
If Left(Trim(LineIn), 1) <> "'" Then ' if line isn't a comment, process it...
strData = Split(LineIn)
If UBound(strData) > 0 Then ' do we have at least 2 fields?
strData(0) = Trim(strData(0)) ' trim the first field
strData(1) = Trim(strData(1)) ' trim the second field
TheVar = UCase(strData(0)) ' upper case the variable name for comparison
If TheVar = "SPEED" Then
SpeedCode = strData(1)
If SpeedCode = "1.5mbps" Then
ChargeCode = "150"
End if
Else
Wscript.Echo "OOPS!"
End if
End If
End If
Loop

oFile.Close
Set FSO = Nothing
Set oFile = Nothing

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate ("Paste")
Wscript.Sleep 1000
WshShell.Sendkeys (ChargeCode)

(in reply to BBistheKing)
 
 
Post #: 2
 
 Re: Reading contents of text file and search ... - 6/29/2005 5:12:50 PM   
  Xandros

 

Posts: 100
Score: 0
Joined: 6/23/2005
From:
Status: offline
Please post an example of the "screen data" you're capturing and the other values you want to extract... knowing that will allow us to better answer your question. You may not need/want to use the "parsing" routine shown in this script. Another form of parsing may be better suited depending on what the screen data looks like.

(in reply to BBistheKing)
 
 
Post #: 3
 
 Re: Reading contents of text file and search ... - 6/29/2005 11:58:53 PM   
  BBistheKing

 

Posts: 107
Score: 0
Joined: 6/21/2005
Status: offline
Part of the screen data will look something like this (there will be lots of other text before and after that I don't need):

Region Regional1
Speed 512Kbps/128Kbps
Domain N7774444K
IP Address A 222.333.444.555
IP Address B 444.555.666.777

I need the data to the right.

Some data appears like this:

Reference Number
WR222555 <-----The number is what I would like to get

(in reply to BBistheKing)
 
 
Post #: 4
 
 Re: Reading contents of text file and search ... - 6/30/2005 8:56:28 AM   
  Xandros

 

Posts: 100
Score: 0
Joined: 6/23/2005
From:
Status: offline
Because you have multple "styles" of data that you're trying to locate/extract you need more a more flexible set of parsing rules. The following script demonstrates how to parse the 3 styles indicated by your sample screen data...

Option Explicit
Dim InputFile
Dim FSO, oFile, LineIn, TempLine
Dim MyRegion, MySpeed, MyDomain
Dim MyIPa, MyIPb, MyRefNo

InputFile = "C:\My Documents\Scripts\Copy.txt"
MyRegion = "" : MySpeed = "" : MyDomain = ""
MyIPa = "" : MyIPb = "" : MyRefNo = ""

Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFile = FSO.OpenTextFile(InputFile)

Do Until oFile.AtEndOfStream
LineIn = oFile.ReadLine
TempLine = LCase(LineIn)
If FindString("region", MyRegion) Then
ElseIf FindSpeed("speed", MySpeed) Then
ElseIf FindString("domain", MyDomain) Then
ElseIf FindString("ip address a", MyIPa) Then
ElseIf FindString("ip address b", MyIPb) Then
ElseIf FindString2("reference number", MyRefNo) Then
End If
Loop

wscript.echo "MyRegion = " & MyRegion
wscript.echo "MySpeed = " & MySpeed
wscript.echo "MyDomain = " & MyDomain
wscript.echo "MyIPa = " & MyIPa
wscript.echo "MyIPb = " & MyIPb
wscript.echo "MyRefNo = " & MyRefNo

oFile.Close
Set oFile = Nothing
Set FSO = Nothing

Function FindString(sSearch, ByRef MyReturn)
Dim Ipos
FindString = False
iPos = InStr(TempLine, sSearch)
If iPos > 0 Then
MyReturn = Trim(Mid(LineIn, iPos + Len(sSearch) + 1))
FindString = True
End If
End Function

Function FindString2(sSearch, ByRef MyReturn)
Dim Ipos
FindString2 = False
iPos = InStr(TempLine, sSearch)
If iPos > 0 Then
On Error Resume Next ' protect against read past eof
MyReturn = oFile.ReadLine
If Err.Number = 0 Then
FindString2 = True
End If
On Error Goto 0
End If
End Function

Function FindSpeed(sSearch, ByRef MyReturn)
Dim Ipos, SpeedCode
FindSpeed = False
iPos = InStr(TempLine, "speed")
If iPos > 0 Then
FindSpeed = True
SpeedCode = Trim(Mid(TempLine, iPos + 5))
If InStr(SpeedCode, "1.5mbps") Then
MyReturn = "150"
ElseIf InStr(SpeedCode, "512kbps") Then
MyReturn = "512"
ElseIf InStr(SpeedCode, "128kbps") Then ' or whatever else...
MyReturn = "128"
Else
MyReturn = "OOPS!"
End If
End If
End Function

(in reply to BBistheKing)
 
 
Post #: 5
 
 Re: Reading contents of text file and search ... - 6/30/2005 11:57:52 AM   
  BBistheKing

 

Posts: 107
Score: 0
Joined: 6/21/2005
Status: offline
You are a legend, Xandros. Thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you.

(in reply to BBistheKing)
 
 
Post #: 6
 
 Re: Reading contents of text file and search ... - 7/1/2005 8:40:34 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
BBistheKing, I played a little bit with your question and find a solution (I think) with regular expressions.
This gives a lot more flexibility in adaptation of the searches, as Xandros already stressed in his post from 06/30/2005 : 2:56:28 PM.
This is the original text file (TestTxt.txt) I use as example :

Part of the screen data will look something like this (there will be lots of other text before and after that I don't need):

Region Regional1
Speed 512Kbps/128Kbps
Domain N7774444K
IP Address A 222.333.444.555
IP Address B 444.555.666.777

I need the data to the right.

Some data appears like this:

Reference Number
WR222555 <-----The number is what I would like to get
Great spirits have always encountered violent opposition from mediocre minds


This is my script :


conLog = "X:\Your path to\TestText.txt"
conForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(conLog)
If objFile.Size > 0 Then
Set ts = objFSO.OpenTextFile(conLog, conForReading)
ReadFile= ts.ReadAll
ts.Close
End If

Var1 = RegExpTest("Region ", ReadFile)
Var2 = RegExpTest("speed ", ReadFile)
Var3 = RegExpTest("domain ", ReadFile)
Var4 = RegExpTest("ip address a ", ReadFile)
Var5 = RegExpTest("ip address b ", ReadFile)
Var6 = RegExpTest("reference number" & "\r\n" & "WR", ReadFile)

WScript.Echo Var1 & vbCrLf & Var2 & vbCrLf & Var3 & vbCrLf & Var4 & vbCrLf & Var5 & vbCrLf & Var6


Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches                                                                                  ' Create variable.
Set regEx = New RegExp                                                                                     ' Create a regular expression.
regEx.Pattern = patrn                                                                                         ' Set pattern.
regEx.IgnoreCase = True                                                                                    ' Set case insensitivity.
regEx.Global = True                                                                                             ' Set global applicability.
Set Matches = regEx.Execute(strng)                                                                  ' Execute search.
For Each Match in Matches                                                                                  ' Iterate Matches collection.
BeginPos = Match.FirstIndex + Match.Length + 1                                             ' FirstIndex has zero-based offset
EndPosCrLf = InStr(BeginPos, strng, vbCrLf)                                                     ' Found item ends with CrLf
EndPosSpace = InStr(BeginPos, strng, Space(1))                                               ' Found item ends with Space
If EndPosCrLf > EndPosSpace Then                                                                    ' First occurrence of CrLf or Space
EndPos = EndPosSpace
Else
EndPos = EndPosCrLf
End If
RetStr = Mid(strng, BeginPos, EndPos-BeginPos)
Next
RegExpTest = RetStr
End Function


This is the (last part of the) final output

Regional1
512Kbps/128Kbps
N7774444K
222.333.444.555
444.555.666.777
222555

I have left the change from real to hundred fold integer out (for the time being)

Regards.

< Message edited by didorno -- 7/20/2005 5:53:20 AM >

(in reply to BBistheKing)
 
 
Post #: 7
 
 Re: Reading contents of text file and search ... - 7/1/2005 10:22:40 AM   
  ehvbs

 

Posts: 2171
Score: 50
Joined: 6/22/2005
From: Germany
Status: online
didorno, I do like your code! Thinking about flexibility I came up with
this code

      
and a question. "dicSearch( sKey ) = aCopy" to change the dictionary
is an ugly hack. Does anybody know a way to access Dictionary data
directly?

(in reply to BBistheKing)
 
 
Post #: 8
 
 Re: Reading contents of text file and search ... - 7/1/2005 10:25:01 PM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
ehvbs, thank you !

You show an interesting approach, but I do not understand the "?" parameter. Can you explain this ?

It is possible to exploit regular expressions still better and be more flexible in the next way.

conLog = "X:\Your path to\TestText.txt"
conForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(conLog)
If objFile.Size > 0 Then
Set ts = objFSO.OpenTextFile(conLog, conForReading)
ReadFile= ts.ReadAll
ts.Close
End If

Var1 = RegExpTest("Region (\S+)", ReadFile)                                                    ' "Region " followed by submatch of 1 or more non-whitespace characters
Var2 = RegExpTest("Speed (\S+)", ReadFile)                                                     ' "Speed "     ,,              ,,             ,,                    ,,
Var3 = RegExpTest("Domain (\S+)", ReadFile)                                                   ' "Domain "    ,,              ,,             ,,                    ,,
Var4 = RegExpTest("IP Address A (\S+)", ReadFile)                                          ' "IP Address A "                                                  ,,
Var5 = RegExpTest("IP Address B (\S+)", ReadFile)                                          ' "IP Address B "                                                   ,,
Var6 = RegExpTest("Reference Number" & "\r\n" & "WR(\S+)", ReadFile)        ' "Reference Number" followed by
' Carriage Return & NewLine followed by "WR" followed by 1 or more non-whitespace characters.

WScript.Echo Var1 & vbCrLf & Var2 & vbCrLf & Var3 & vbCrLf & Var4 & vbCrLf & Var5 & vbCrLf & Var6


Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches                                                                                  ' Create variable.
Set regEx = New RegExp                                                                                     ' Create a regular expression.
regEx.Pattern = patrn                                                                                          ' Set pattern.
regEx.IgnoreCase = True                                                                                     ' Set case insensitivity.
regEx.Global = True                                                                                              ' Set global applicability.
Set Matches = regEx.Execute(strng)                                                                    ' Execute search.
For Each Match in Matches                                                                                    ' Iterate Matches collection.
RegExpTest = Match.SubMatches(0)                                                                    ' Delivers the first submatch of the matches
Next
End Function     ' RegExpTest

In principle this is all you need and easy to adapt to specific wishes.

Good luck.

< Message edited by didorno -- 7/20/2005 6:03:19 AM >

(in reply to BBistheKing)
 
 
Post #: 9
 
 Re: Reading contents of text file and search ... - 7/2/2005 1:41:21 AM   
  BBistheKing

 

Posts: 107
Score: 0
Joined: 6/21/2005
Status: offline
Gee, thanks guys. Your help is greatly appreciated.

didorno, thanks for the comment lines - it makes it so much easier to understand for a beginner like me.

I'll give it a go at work next week. Many thanks to all of you.

(in reply to BBistheKing)
 
 
Post #: 10
 
 Re: Reading contents of text file and search ... - 7/2/2005 2:09:24 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
BBistheKing, you're welcome !

If you have multiple variables of Region, Speed, etc. in your text file,
then you get them all by the next code :

conLog = "X:\Your path to\TestText.txt"
conForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(conLog)
Set ts = objFSO.OpenTextFile(conLog, conForReading)
ReadFile = ts.ReadAll
ts.Close

Var1 = RegExpTest("(Region )(\S+)")
Var2 = RegExpTest("(Speed )(\S+)")
Var3 = RegExpTest("(Domain )(\S+)")
Var4 = RegExpTest("(IP Address A )(\S+)")
Var5 = RegExpTest("(IP Address B )(\S+)")
Var6 = RegExpTest("(Reference Number" & "\r\n" & "WR)(\S+)")


Function RegExpTest(patrn)
Dim regEx, Match, Matches
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(ReadFile)
For Each Match in Matches
WScript.Echo Match.SubMatches(0) & " : " & Match.SubMatches(1)
Next
End Function ' RegExpTest


Good luck !

< Message edited by didorno -- 7/20/2005 6:04:25 AM >

(in reply to BBistheKing)
 
 
Post #: 11
 
 Re: Reading contents of text file and search ... - 7/2/2005 2:21:04 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
About your question concerning the 1.5 conversion.
quote:
Originally posted by BBistheKing
.............................................
Also, is it possible that when an an instance of 1.5 is found, to convert it so that when I SendKeys the variable to the other program it will read as 150?



I have a question :
1. Is 1.5 to 150 the only real to integer change to be expected,
or can it also be 0.37 or 2.009 to for instance 370 or 2009 etc. ?
If your answer is Yes, then you can add somewhere in your code :

If strVarX = "1.5" Then strVarX = "150"

Or did I understand you wrong ?

Bye !

(in reply to BBistheKing)
 
 
Post #: 12
 
 Re: Reading contents of text file and search ... - 7/2/2005 1:16:47 PM   
  BBistheKing

 

Posts: 107
Score: 0
Joined: 6/21/2005
Status: offline
The reason I need to represent 1.5mbps as "150" is that it is part of a billing code that we use in our provisioning system. The other speeds get entered as is (eg 256, 512). I will also be changing the regions too (eg Metro gets entered as "U", Regional1 as "M", Regional2 as "R"). But I know how to do that. Thanks again for your help.

didorno, I also wanted to ask something about your code to find multiple variables of "Region" etc. What part of the code actually performs that task? I'm not sure which part, but is it the extra parentheses in the RegExpTest?

I won't actually need it but it is good to know. I put in another variable of "Region" in the Test txt file and the other codes only found the last instance of "Region" where as your last code found both.

(in reply to BBistheKing)
 
 
Post #: 13
 
 Re: Reading contents of text file and search ... - 7/2/2005 9:21:44 PM   
  ehvbs

 

Posts: 2171
Score: 50
Joined: 6/22/2005
From: Germany
Status: online
BBistheKing,
from your last two postings I gather that your specific problem is
solved, but that you would like to learn more about regular expressions.

Perhaps you have started with the chapters on RegExp, Matches, and Match in
the "Windows Script Technologies" documentation; certainly you found
"Mastering Regular Expressions" by Jeffrey E. F. Friedl at

  • http://www.oreilly.com/catalog/regex/index.html

  • http://www.oreilly.com/catalog/regex2/


To experiment with regular expressions I use "The Regex Coach"
  • http://weitz.de/regex-coach/

on my Window 2000 computer or I add a reference ("Verweis") to
  • Microsoft VBScript Regular Expressions [5.5]

to a Excel, Word, or Access 'document' and use VBA/VBE.

By the way, my code snippet was intended to make it easy for you
to test RegExps against your data. For example: just by adding lines like

      
you can test/improve your RegExps (in the context of your specific
problem). [I should have said so explicitly.]

If you are interested in the step by step development of a commandline
script to explore regular expressions in a more general way - sample
output from step 3:

      
- I will post step 1 here to see if other members consider it compatible
with the main purpose of this forum.

(in reply to BBistheKing)
 
 
Post #: 14
 
 Re: Reading contents of text file and search ... - 7/3/2005 1:45:34 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
Thank you very much, ehvbs, for the links, I can use that certainly !

BBistheKing,
quote:
didorno, I also wanted to ask something about your code to find multiple variables of "Region" etc. What part of the code actually performs that task? I'm not sure which part, but is it the extra parentheses in the RegExpTest?

In fact, the script from 07/02/2005 : 04:25:01 AM does find all matches, but due to the final use of WScript.Echo with the Var's, only the last value of each Var is shown.
By shifting the WScript.Echo within the For-loop of the function you get all the values (script 07/02/2005 : 08:09:24 AM).
The additional parentheses gives the opportunity to use that parts later, for instance for showing the names of the variables (indicated by Match.SubMatches(0)).

I think a minor disadvantage of the latter code is the not-logical order of the results (1st all the regions, next all the speeds, etc. in the text file).
I composed the next code to solve this, the matches shown are in the order of occurrences in the text file.

conLog = "X:\Your path to\TestText.txt"
conForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(conLog)
Set ts = objFSO.OpenTextFile(conLog, conForReading)
ReadFile = ts.ReadAll
ts.Close

Var = RegExpTest("(Region )(\S+)|(Speed )(\S+)|(Domain )(\S+)|(IP Address A )(\S+)" _
& "|(IP Address B )(\S+)|(Reference Number" & "\r\n" & "WR)(\S+)")

Function RegExpTest(patrn)
Dim regEx, Matches, Match, i, SubStr
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(ReadFile)
For Each Match in Matches
i = 0
Do While i < 12
SubStr = Match.SubMatches(i)
If SubStr <> "" Then Wscript.Echo SubStr & vbTab & vbTab & ": " & Match.SubMatches(i+1)
i = i + 2
Loop
Next
End Function ' RegExpTest


Regards.

< Message edited by didorno -- 7/20/2005 6:05:58 AM >

(in reply to BBistheKing)
 
 
Post #: 15
 
 Re: Reading contents of text file and search ... - 7/3/2005 1:04:06 PM   
  BBistheKing

 

Posts: 107
Score: 0
Joined: 6/21/2005
Status: offline
Thanks very much for your help.

(in reply to BBistheKing)
 
 
Post #: 16
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Reading contents of text file and search ... Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts