Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


SEARCH TEXT FILE

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

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> SEARCH TEXT FILE
  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 >>
 SEARCH TEXT FILE - 10/13/2005 8:23:07 AM   
  brado

 

Posts: 7
Score: 0
Joined: 7/19/2005
Status: offline
Hi There,

New to VBS could someone please help.  I want to search a text file for the word "error", then be able to ECHO the line ABOVE where the error word was found.
Much appreciated for any help.
Thanks..
 
 
Post #: 1
 
 RE: SEARCH TEXT FILE - 10/13/2005 1:38:50 PM   
  BBistheKing

 

Posts: 107
Score: 0
Joined: 6/21/2005
Status: offline
Hi

NB: Code has been edited since first posted

I am new to VBScript too, but I got this to work (finally, huff, puff)

It is an ugly work around and someone more experienced will have something better, but here it goes...

This is the test file I was working with ( saved to C:\ error.txt):

*******************
text
text
text
This is the line I need
error
text
text
text

**************
And this is my ugly code:

conLog        = "C:\error.txt"
conForReading    = 1

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


MyError = Instr(ReadFile, "error")

Wscript.Echo MyError

Set ts    = objFSO.OpenTextFile(conLog, conForReading)

MyText1 = MyError - 1
MyText2 = ts.Read(MyText1)

Wscript.Echo MyText2

sFSpec = "C:\MyFile.txt"
Const ForWriting = 2

Set fso = CreateObject( "Scripting.FileSystemObject" )
Set ts = fso.OpenTextFile( sFSpec, ForWriting )
ts.Write MyText2


Const ForReading = 1

Set ts = fso.OpenTextFile( sFSpec, ForReading )

ts.ReadAll

NumLines = ts.Line

Wscript.Echo NumLines

MyLine = NumLines - 2

Wscript.Echo MyLine

Set ts = fso.OpenTextFile( sFSpec, ForReading )

For i = 1 to MyLine
    ts.ReadLine
Next

strLine = ts.ReadLine
Wscript.Echo strLine

**********************

Hope it helps.



 

< Message edited by BBistheKing -- 10/13/2005 2:30:07 PM >


_____________________________

"Live simply, that others may simply live." - Mahatma Gandhi

(in reply to brado)
 
 
Post #: 2
 
 RE: SEARCH TEXT FILE - 10/13/2005 2:16:46 PM   
  BBistheKing

 

Posts: 107
Score: 0
Joined: 6/21/2005
Status: offline
Hmmf, I just realized that this code may require the word "error" to be the first word on the line. If it isn't, you might want to remove the line 

MyText1 = MyError - 1

 and change

 
MyText2 = ts.Read(MyText1)

 to

 MyText2 = ts.Read(MyError)


This is untested so it might not work. I don't know if it matters actually. I dunno, to quote Janet: "I'm tired, I'm hungry and I'm just plain scared!" (which is how I always feel when working with VBScript).

< Message edited by BBistheKing -- 10/13/2005 2:27:17 PM >


_____________________________

"Live simply, that others may simply live." - Mahatma Gandhi

(in reply to BBistheKing)
 
 
Post #: 3
 
 RE: SEARCH TEXT FILE - 10/13/2005 2:31:17 PM   
  BBistheKing

 

Posts: 107
Score: 0
Joined: 6/21/2005
Status: offline
Tested it - still works.

_____________________________

"Live simply, that others may simply live." - Mahatma Gandhi

(in reply to BBistheKing)
 
 
Post #: 4
 
 RE: SEARCH TEXT FILE - 10/13/2005 4:36:21 PM   
  brado

 

Posts: 7
Score: 0
Joined: 7/19/2005
Status: offline
LEGEND!  Thanks heaps exactly what i was after....

(in reply to BBistheKing)
 
 
Post #: 5
 
 RE: SEARCH TEXT FILE - 10/13/2005 11:05:12 PM   
  mbouchard


Posts: 1916
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
Instead of using readall to get the number of lines and then stepping through the text file using readline you could read the file into an array then step through the array.


      

You could further spilt each line into an array if needed.
Inside the For next above.
Bold = from above script

      

_____________________________

Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.

(in reply to brado)
 
 
Post #: 6
 
 RE: SEARCH TEXT FILE - 10/14/2005 3:37:28 AM   
  BBistheKing

 

Posts: 107
Score: 0
Joined: 6/21/2005
Status: offline
Ah Ha! Now, see...there you go brado, what did I tell you. I had been trying to avoid having to create a text file to capture the line brado needed but couldn't figure it out.

While something about Mike's script when sailing over my head (couldn't get it to work - don't have much experience dealing with arrays), I tinkered with it until I could get to work.

This is a much better way than the 'war and peace' version I posted earlier, brado. Much prettier.

****************************

conLog        = "C:\error.txt"
conForReading    = 1

Set fso    = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(conLog, conForReading)
 
'Read the file into a variable
theFile = f.ReadAll
 
'Split TheFile variable into an array, using vbcrlf as the delimiter
MyArray =   split(theFile, vbCrLf)
MyLine = Ubound(MyArray)

For I = 0 to MyLine
    If MyArray(I) = "error" then
    Exit For
    End if
Next

j = I - 1
msgbox MyArray(j)

***********************

Cheers, Mike. Didn't really know how to deal with arrays until now. Much obliged.

< Message edited by BBistheKing -- 10/14/2005 3:42:09 AM >


_____________________________

"Live simply, that others may simply live." - Mahatma Gandhi

(in reply to brado)
 
 
Post #: 7
 
 RE: SEARCH TEXT FILE - 10/14/2005 9:12:41 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
brado, may I give you my solution by means of regular expressions ?
Because I think its larger flexibility can be important in case of changes in the search goals.

This example is best used by means of cscript.exe.

str    = "This is the beginning of the 1st line." & vbCrLf & "This is the 2nd line witherro" _
        & vbCrLf & "    3rd line (rror)    ghhj><" & vbCrLf & "        hereerrormade." _
        & vbCrLf & "Error has been made in line above (4th) !"

WScript.Echo str & vbCrLf

Set regEx           = New RegExp
regEx.Pattern    = "^(.+)\r\n.*error"  ' pattern is : after a new line character followed by one or more characters, not being new line characters,
                                                             ' followed by CRLF, followed by zero or more characters, not being new line characters, followed by "error"
regEx.MultiLine  = True
regEx.Global      = True
Set Matches       = regEx.Execute(str)
For Each Match in Matches
    RetStr            = Match.Value
    Next
WScript.Echo "The line above the line containing the substring 'error' is :  "  & vbCrLf & regEx.Replace(RetStr, "$1")


This works now only for the last "error" substring in the str.
In case of more than 1 "error" string in the text you can replace the "For loop" by

For Each Match in Matches
    RetStr    = Match.Value
    WScript.Echo "The line above the line containing the substring 'error' is :  " _
        & vbCrLf & regEx.Replace(RetStr, "$1")
    Next


Regards.

< Message edited by didorno -- 10/14/2005 9:17:36 AM >


_____________________________

Regular Expression ? I (L+o{1,}v{1,3}e\s)+[iI]t!$

(in reply to BBistheKing)
 
 
Post #: 8
 
 
 
  

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 >> SEARCH TEXT FILE 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