Login | |
|
 |
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
|
|
| |
|
|
|
 |
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!$
|
|
| |
|
|
|
| |
|
|
 |
|
 |
|
|