Repost (completed) Application.Filesearch with wildcards

R

Rich K

I'm using the application.filesearch to find files that have file path files
that fulfill certain text comparisons.
This is what my code looks like:

Dim vItem As Variant
Dim db As DAO.Database
Dim searchString As String
Dim searchLocation As String

searchString = Nz(Me.txtSearchString.Value, "")
searchString = Replace(searchString, "_", "[_]")
searchString = Replace(searchString, "#", "[#]")
searchLocation = Nz(Me.txtSearchLocation.Value, "")

Select Case Me.SearchType.Value
Case 1
searchString = searchString & "*"
Case 2
searchString = "*" & searchString
Case 3
searchString = "*" & searchString & "*"
Case Else
End Select

Set db = CurrentDb
With Application.FileSearch
.FileName = searchString
.LookIn = searchLocation
.MatchTextExactly = False
.SearchSubFolders = True
.Execute

For Each vItem In .FoundFiles
Me.lisFiles.AddItem vItem
Next vItem
End With
Set db = Nothing
-----------------------------------------------

I'll actual ask the question this time.

When using wildcards I have success when I use the * in the search, but when
I try to use wildcards for a single letter or number the process returns no
records. What is the process for using wildcards in searches other than *?

Thanks in advance.
 
P

Paolo

Hi Rich K,

I think you can try with ? as a wildcard for single letter or number.

HTH Paolo
 
D

Dirk Goldgar

Rich K said:
I'm using the application.filesearch to find files that have file path
files
that fulfill certain text comparisons.
This is what my code looks like:

Dim vItem As Variant
Dim db As DAO.Database
Dim searchString As String
Dim searchLocation As String

searchString = Nz(Me.txtSearchString.Value, "")
searchString = Replace(searchString, "_", "[_]")
searchString = Replace(searchString, "#", "[#]")
searchLocation = Nz(Me.txtSearchLocation.Value, "")

Select Case Me.SearchType.Value
Case 1
searchString = searchString & "*"
Case 2
searchString = "*" & searchString
Case 3
searchString = "*" & searchString & "*"
Case Else
End Select

Set db = CurrentDb
With Application.FileSearch
.FileName = searchString
.LookIn = searchLocation
.MatchTextExactly = False
.SearchSubFolders = True
.Execute

For Each vItem In .FoundFiles
Me.lisFiles.AddItem vItem
Next vItem
End With
Set db = Nothing
-----------------------------------------------

I'll actual ask the question this time.

When using wildcards I have success when I use the * in the search, but
when
I try to use wildcards for a single letter or number the process returns
no
records. What is the process for using wildcards in searches other than
*?

Thanks in advance.


According to the help file, the FileSearch object accepts asterisk (*) and
question mark (?) as wild cards. It doesn't say anything about any other
pattern matching. I haven't used the FileSearch object myself, but I
suspect you're out of luck, as far as that object is concerned.

You can write your own code that uses Dir() to loop through the folder (and
subfolders, as needed) and compiles the list of matching files. For that,
you can use the VBA Like operator, which does support more complex patterns.
 
R

Rich K

Thanks both of you.

I thought I had tested the ? and had it fail, but just to humor you both I
went back and tested it again and it worked!

Thanks for the help.

Rich

Dirk Goldgar said:
Rich K said:
I'm using the application.filesearch to find files that have file path
files
that fulfill certain text comparisons.
This is what my code looks like:

Dim vItem As Variant
Dim db As DAO.Database
Dim searchString As String
Dim searchLocation As String

searchString = Nz(Me.txtSearchString.Value, "")
searchString = Replace(searchString, "_", "[_]")
searchString = Replace(searchString, "#", "[#]")
searchLocation = Nz(Me.txtSearchLocation.Value, "")

Select Case Me.SearchType.Value
Case 1
searchString = searchString & "*"
Case 2
searchString = "*" & searchString
Case 3
searchString = "*" & searchString & "*"
Case Else
End Select

Set db = CurrentDb
With Application.FileSearch
.FileName = searchString
.LookIn = searchLocation
.MatchTextExactly = False
.SearchSubFolders = True
.Execute

For Each vItem In .FoundFiles
Me.lisFiles.AddItem vItem
Next vItem
End With
Set db = Nothing
-----------------------------------------------

I'll actual ask the question this time.

When using wildcards I have success when I use the * in the search, but
when
I try to use wildcards for a single letter or number the process returns
no
records. What is the process for using wildcards in searches other than
*?

Thanks in advance.


According to the help file, the FileSearch object accepts asterisk (*) and
question mark (?) as wild cards. It doesn't say anything about any other
pattern matching. I haven't used the FileSearch object myself, but I
suspect you're out of luck, as far as that object is concerned.

You can write your own code that uses Dir() to loop through the folder (and
subfolders, as needed) and compiles the list of matching files. For that,
you can use the VBA Like operator, which does support more complex patterns.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top