Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vbscript regexp-need help-better explaination-ignore my prev posts

Status
Not open for further replies.

cisco777

Programmer
Jul 22, 2005
13
US
Sorry about the prev posts.

What I am trying to do is: To search a text file.
Then see if this text file contains ended in success or ended in errors. This is why I am using regexp and also using open text file. I need to read/search this entire text file and locate these words.

Lastly, I want to print/display a message showing that there either were errors or it ended successfully. I think I did not make this clear.

may I ask to show how my code should look in order for it read/search this text file, find the words ended with errors and print the message?


Since I am just starting to learn cscript and regular expressions, I obviously do not yet have a grip on this.

Maybe I am also missing a read stmt.


'Create File System Objects
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
'Set objtxt = objFSO.OpenTextFile(c:\fileSuces, ForReading)
Set objtxt = objFSO.OpenTextFile(c:\fileError, ForReading)


Set StdOut = WScript.StdOut

' Set up the string that will be searched for the existence of a sub-string
' Pattern specifies the Regular Expression that is to be searched for
' False = Only match the first occurence of the pattern

StringToSearch = "Ended with errors "
' StringToSearch = "Ended with success "

StdOut.WriteLine "Contents of StringToSearch = " & StringToSearch
Set RegularExpressionObject = New RegExp
With RegularExpressionObject
' .Pattern = "with errors"
.Pattern = "with success"
.IgnoreCase = True
.Global = False
End With

expressionmatch = RegularExpressionObject.Test(StringToSearch)

If expressionmatch Then
StdOut.WriteLine "RegularExpressionObject.Pattern match was found in " & StringToSearch
Else
StdOut.WriteLine "RegularExpressionObject.Pattern match was Not found in " & StringToSearch
End If

objTxt.close

Set objFSO = Nothing
Set objTextFile = Nothing
set objStdOut = Nothing
Set RegularExpressionObject = Nothing

thank you.

steve
 
You are not restricted not to repost to the same thread. So please do not create new threads while staying with the same subject matter.

I have corrected some of your syntax error. You do not seem to take notice.

To make thing work.
[tt]
[red]const ForReading=1[/red]
'Create File System Objects
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objtxt = objFSO.OpenTextFile([red]"[/red]c:\fileError[red]"[/red], ForReading)
'StringToSearch = "Ended with errors "
[blue]StringToSearch=objtxt.readall[/blue]
objTxt.close
Set [red]objtxt[/red] = Nothing
Set objFSO = Nothing

Set StdOut = WScript.StdOut

' Set up the string that will be searched for the existence of a sub-string
' Pattern specifies the Regular Expression that is to be searched for
' False = Only match the first occurence of the pattern

StdOut.WriteLine "Contents of StringToSearch = " & StringToSearch

Set RegularExpressionObject = New RegExp
With RegularExpressionObject
.Pattern = [blue]"\bwith\b\s+\bsuccess\b"[/blue]
.IgnoreCase = True
.Global = False
End With

expressionmatch = RegularExpressionObject.Test(StringToSearch)

If expressionmatch Then
StdOut.WriteLine "RegularExpressionObject.Pattern match was found in " & StringToSearch
Else
StdOut.WriteLine "RegularExpressionObject.Pattern match was Not found in " & StringToSearch
End If

Set RegularExpressionObject = Nothing
set objStdOut = Nothing
[/tt]
 
That's ridiculous. You started 5 threads on the same subject and got nowhere but confusing the forum & members. Could you stay with the thread where you feel the question has a chance to be self referential?

>lastly is there a way to not display the entire contents of the file i am searching and just display the matched or not matched line?
[tt]
If expressionmatch Then
StdOut.WriteLine "RegularExpressionObject.Pattern match was found in [blue]the file.[/blue]" ' & StringToSearch
Else
StdOut.WriteLine "RegularExpressionObject.Pattern match was Not found in [blue]the file.[/blue]" ' & StringToSearch
End If
[/tt]
 
Or if you want to display the pattern itself then it is this.
[tt]
If expressionmatch Then
StdOut.WriteLine RegularExpressionObject.Pattern & " match was found in the file." ' & StringToSearch
Else
StdOut.WriteLine RegularExpressionObject.Pattern & " match was Not found in the file." ' & StringToSearch
End If
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top