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

Nested FileSearchObject Statement using wildcards? 1

Status
Not open for further replies.

Flo79

Technical User
Nov 12, 2002
80
US
Hi there:

I am trying to build a custom function that searched my computer for a file and returns the path to that file.

I have successfull using a function build by Dev Ashish that enables me to dfind a file with a specific name which I have included bleow.

My problem is that I now need to find a file using wildcards and based on multiple criteria. For example:

Files containing "123" or "ABC"

Please note that I only need the file that was modified most recently returned.

Can somebody tell me how I can modifiy the below code to allow serches based on multiple criteria and wildcards?

--------------------------------------------

Option Explicit

'This code was originally written by Dev Ashish.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Dev Ashish
'
Function freturnfilepath(strFileName As String) As String

Dim varItem As Variant
Dim strFiles As String
Dim strTmp As String
Dim strDrive As String

strDrive = DFirst("[Doc_Location]", "TBL_System")

strFiles = ""
With Application.FileSearch
.NewSearch
.LookIn = strDrive
.SearchSubFolders = True
.Filename = strFileName
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles
If .Execute > 0 Then
For Each varItem In .FoundFiles
strTmp = fGetFileName(varItem)
If strFileName = Left(strTmp, InStrRev(strTmp, ".") - 1) Then
freturnfilepath = varItem
Exit Function
End If
Next varItem
End If
End With
End Function


Private Function fGetFileName(strFullPath) As String
Dim intPos As Integer, intLen As Integer
intLen = Len(strFullPath)
If intLen Then
For intPos = intLen To 1 Step -1
'Find the last \
If Mid$(strFullPath, intPos, 1) = "\" Then
fGetFileName = Mid$(strFullPath, intPos + 1)

Exit Function
End If
Next intPos
End If
End Function
------------------------------------------------------------
 
Flo79

Help said:
The name of the file may include the * (asterisk) or ? (question mark) wildcards. Use the question mark wildcard to match any single character. For example, type gr?y to match both "gray" and "grey." Use the asterisk wildcard to match any number of characters. For example, type *.txt to find all files that have the .TXT extension

Code:
With Application.FileSearch
   .NewSearch
   .LookIn = strDrive
   .SearchSubFolders = True
   .Filename = strFileName & "*.txt"
   .MatchTextExactly = True
   .FileType = 1 [green]' msoFileTypeAllFiles[/green]
   If .Execute(4, 1, True) > 0 Then [green]'msoSortByLastModified,msoSortOrderAscending[/green]
        freturnfilepath = .FoundFiles(.FoundFiles.Count)
....

The .Execute method has 3 arguments
Help said:
* SortBy Optional MsoSortBy. The method used to sort the returned file(s).
* SortOrder Optional MsoSortOrder. The order in which the returned file(s) are sorted.
* AlwaysAccurate Optional Boolean. True to have the file search include files that have been added, modified, or deleted since the file index was last updated. The default value is True.

In the above example the function shall return full path & file name of the file most recently modfied.


Also this property .MatchTextExactly = True will find only files whose body text or file properties contain the exact word or phrase that you've specified. But it is not necessary since you dont define the property .TextOrProperty = "A word"

As an aside info if you use Access 2000 or above
Code:
Private Function fGetFileName2(strFullPath) As String
Dim intPos As Integer
intPos = InStrRev(strFullPath, "\") [green]'Find the last \[/green]
fGetFileName = Mid$(strFullPath, intPos + 1)
End Function

The only problem here is that you cant run a OR in search criteria (easily) unless you have names like
"blahABCblah123blah.txt"--->"*ABC*123*.txt"
"blah123CblahABCblah.txt"--->"*123*ABC*.txt"
But it would be better to run the function 2 times.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top