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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching through text file using wildcard

Status
Not open for further replies.

hazeus

Technical User
Nov 18, 2008
6
CA
I am searching a huge text file for the following text;

3**40**

Where the * represents a wild card
How do I manipulate this line of code to search for this?

if InStr(226, strCurrentLine, "3**40**", vbTextCompare) = 226 Then

Thank you,
Steve
 
Search this forum for examples of Regular Expressions then post back here with your code if you have problems.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I read a few posts discussing Regular Expressions, in all cases it was searching for filenames with wild cards and I do not understand how to apply it.

Can someone tell me how I would insert a regular expression to solve this issue?

Const ForReading = 1
Const ForWriting = 2

Set objDialog = CreateObject("UserAccounts.CommonDialog")

objDialog.Filter = "Text Files|*.Txt"

intResult = objDialog.ShowOpen

If intResult = 0 Then
Wscript.Quit
Else

strFileName = objDialog.FileName
wscript.echo strFileName
End If

Set objDictionary = CreateObject("Scripting.Dictionary")

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile(strFileName, ForReading, True)
Set objFilew = objFSO.OpenTextFile("c:\outputfile.txt", ForWriting, True)

Do Until objFile.AtEndOfStream
strCurrentLine = objFile.ReadLine
if InStr(226, strCurrentLine, "3**40**", vbTextCompare) = 226 Then

intRight = Len(strCurrentLine) - 3
strRight = Right(strCurrentLine, intRight)
strLeft = Left(strCurrentLine, 3)
strInsert = "*******Insert Text*********"
strText = strLeft & strInsert & strRight
strContents = strCurrentLine & strText & vbCrLf
wscript.echo strContents

objFilew.WriteLine strText
Else

objFilew.WriteLine strCurrentLine

End If
Loop

objFile.Close
objFilew.Close


MsgBox "Complete
 
this is off the cuff, but something like (you may need to play around with the pattern):

Const ForReading = 1
Const ForWriting = 2

Set objDialog = CreateObject("UserAccounts.CommonDialog")

objDialog.Filter = "Text Files|*.Txt"

intResult = objDialog.ShowOpen

If intResult = 0 Then
Wscript.Quit
Else

strFileName = objDialog.FileName
wscript.echo strFileName
End If

Set objDictionary = CreateObject("Scripting.Dictionary")

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile(strFileName, ForReading, True)
Set objFilew = objFSO.OpenTextFile("c:\outputfile.txt", ForWriting, True)

[red]Set oRE = New RegExp
oRE.Pattern = ".{225}3.*40.*"[/red]
Do Until objFile.AtEndOfStream
strCurrentLine = objFile.ReadLine
if [red]oRE.Test(strCurrentLine)[/red] Then

intRight = Len(strCurrentLine) - 3
strRight = Right(strCurrentLine, intRight)
strLeft = Left(strCurrentLine, 3)
strInsert = "*******Insert Text*********"
strText = strLeft & strInsert & strRight
strContents = strCurrentLine & strText & vbCrLf
wscript.echo strContents

objFilew.WriteLine strText
Else

objFilew.WriteLine strCurrentLine

End If
Loop

objFile.Close
objFilew.Close


MsgBox "Complete"

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Wow, I dont understand that code at all but it seems to work!! The only thing I need it to do now is insert the text over the old text instead of indenting and pushing it over.

Thank you so much! I wonder if you happen to know how I can change it to insert the text and overwrite?
 
so you need to insert some text starting 3 characters from the end?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Right now this script pushes the text over:

The boy has a dog

This Script does this:
The*insert* boy has a dog

What I want:

The*insert* a dog
 
So you want to replace the matched text with something else?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I went through the regular expressions syntax... The best I could gather was that I need an "anchor"

I do not understand the regular expression code so I am not sure what to add to get it to "Overtype" instead of pushing the text over
 
I figured it out, I changed the number of characters it is capturing in my string manipulation

intRight = Len(strCurrentLine) - 3

changed to

intRight = Len(strCurrentLine) - 8

Thank you for all your help... great forum by the way
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top