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
------------------------------------------------------------
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
------------------------------------------------------------