PDA

View Full Version : with VBA search text file with wildcards



RCGUA123
05-03-2010, 09:24 AM
with the following code I open a text file and I want to search for every html image tag, I want to search for <img src=myimage.jpg> (or whatever the image name is) Is there any way to use wildcards like an asterisk
<img src=*> or is there a better way to do this? to use wildcards I think is would need to make a range object and set the InputData to the range??

Sub test2()
Dim InputData
Dim strSearchText As String
Dim strNewFile As String
strSearchText = "<img scr*>"
strNewFile = "c:\data\imageNamesList.txt"
Open "C:\data\myfile.txt" For Input As #1 ' Open file for input.
Do While Not EOF(1) ' Check for end of file.
Line Input #1, InputData ' Read line of data.
If InStr(InputData, strSearchText) <> 0 Then
hFile = FreeFile
Open strNewFile For Append As #hFile
Print #hFile, "print something here"
Close #hFile
End If
Loop
Close #1 ' Close file.
End Sub

fumei
05-03-2010, 10:03 AM
You can not use wildcards like that with a text file.

The function InStr works with string literals, not wildcards.

Two possibilities.

1. Open the file as a Word document (not an Open For Append random access file), and then use Find (which can use wildcards);

2. RegEx

RCGUA123
05-03-2010, 11:51 AM
I tried opening the text file as a Word document but it was slow and I couldn't get it to work right, the code below uses RegEx and I think that will work well, except, how to I modify "ThisDocument.Range.Text" to set the range to my text file that I opened "For Input As #1"
or for the "Line Input #1, InputData"

Sub BoldUpperCaseWords()

Dim regEx, Match, Matches

Set regEx = New RegExp ' Create a regular expression.
regEx.Pattern = "\b[A-Z]+\b" ' Set pattern.
regEx.IgnoreCase = False ' Set case insensitivity.
regEx.Global = True ' Set global applicability.

Set Matches = regEx.Execute(ThisDocument.Range.Text) ' Execute search.

For Each Match In Matches ' Iterate Matches collection.
'selects a range from the index of the character to the index of the character plus the length of the word
ThisDocument.Range(Match.FirstIndex, _
Match.FirstIndex + Len(Match.Value)).Bold = True
Next

End Sub

fumei
05-03-2010, 12:04 PM
" how to I modify "ThisDocument.Range.Text" to set the range to my text file that I opened "For Input As #1"
or for the "Line Input #1, InputData"


You can not. The text file (opened as a random access file) does not HAVE a range.

RCGUA123
05-03-2010, 12:18 PM
How can I use RegEx to search my text file?

fumei
05-03-2010, 12:27 PM
By using the entire contents as one string. THAT, unfortunately messes up your objective of trying to match to a specific Line.

But...you can not Bold a plain text file anyway. So I am not following what - exactly - you are doing. This does not work for a text file.

Len(Match.Value)).Bold = True


It HAS no .Bold property.

fumei
05-03-2010, 12:44 PM
Set Matches = RegExp.Execute(a_string)

ThisDocument.Range.Text is a string. It also has a Parent property of the Range. Your Open as random access means just that...it is random access. yes, you can go Line by line.

That being said, you may want to try playing around with....
Function GetRegExResult(strInputData As String) As String
Dim Match, Matches
Dim RegExp As Object
Set RegExp = CreateObject("VBScript.RegExp")
With RegExp
.Global = True
.IgnoreCase = True
.Pattern = "\b[A-Z]+\b"
End With

Set Matches = RegExp.Execute(strInputData) ' Execute search.
For Each Match In Matches
MsgBox Match
Next
End Function

Sub test2()
Dim InputData
Dim hFile
Dim strSearchText As String
Dim strNewFile As String
strNewFile = "c:\zzz\test\imageNamesList.txt"
Open "C:\zzz\Test\myfile.txt" For Input As #1 ' Open file for input.
Do While Not EOF(1) ' Check for end of file.
Line Input #1, InputData ' Read line of data.
GetRegExResult (InputData)
hFile = FreeFile
Open strNewFile For Append As #hFile
Print #hFile, "print something here"
Close #hFile
Loop
Close #1 ' Close file.
End Sub
So you CAN pass your InputData (by line from your random access file) into the Function operating the RegEx. I have just slightly played with it, so it is NOT set up fully for your requirements. It may be a start for you though.

Good luck.