I am working on a function that returns a file's path based on its name and a search directory.
I am using the "SearchTreeForFile" API to do this.
The problem is that this API does not accept wildcard characters in the file name.
From What I've read the "FindFirstFile" API is a much more flexible solution.
All code i've seen using the latter seems for to complicated for my needs, as all I am trying to do is find a file with a specific name and return the file's complete path if its found and nothing if its not. Again, I need to be able to use wilcars in the file name.
e.g. strFileName = strFilePRefix & "*" & FileSuffix
Here is my existing code that works well except for the fact that It doesn't accept wildcard characters:
------------------------------
Private Declare Function apiSearchTreeForFile Lib "imagehlp" Alias "SearchTreeForFile" _
(ByVal RootPath As String, _
ByVal InputPathName As String, _
ByVal OutputBuffer As String) As Long
Private Const MAX_PATH = 260
Function fFindFile(strFile As String, Optional varPath As Variant) As String
' Function to return the location of a file, given the name of it, searching a maximum depth of 32 directories
' Accepts:
' strFile - The name of the file to be found
' varPath (Optional) - the path to start searching for the file.
' Returns:
' The full path and name of the file if it is found, or "" if not found
Dim strReturn As String
Dim lngReturn As Long
If IsMissing(varPath) Then varPath = "C:\"
strReturn = String(MAX_PATH, 0)
lngReturn = apiSearchTreeForFile(varPath, strFile, strReturn)
If lngReturn <> 0 Then
fFindFile = Left(strReturn, InStr(strReturn, Chr(0)) - 1)
End If
End Function
---------------------------------------------------------
I am looking for something equally simple.
Any help would be greatly appreciated.
FGP
I am using the "SearchTreeForFile" API to do this.
The problem is that this API does not accept wildcard characters in the file name.
From What I've read the "FindFirstFile" API is a much more flexible solution.
All code i've seen using the latter seems for to complicated for my needs, as all I am trying to do is find a file with a specific name and return the file's complete path if its found and nothing if its not. Again, I need to be able to use wilcars in the file name.
e.g. strFileName = strFilePRefix & "*" & FileSuffix
Here is my existing code that works well except for the fact that It doesn't accept wildcard characters:
------------------------------
Private Declare Function apiSearchTreeForFile Lib "imagehlp" Alias "SearchTreeForFile" _
(ByVal RootPath As String, _
ByVal InputPathName As String, _
ByVal OutputBuffer As String) As Long
Private Const MAX_PATH = 260
Function fFindFile(strFile As String, Optional varPath As Variant) As String
' Function to return the location of a file, given the name of it, searching a maximum depth of 32 directories
' Accepts:
' strFile - The name of the file to be found
' varPath (Optional) - the path to start searching for the file.
' Returns:
' The full path and name of the file if it is found, or "" if not found
Dim strReturn As String
Dim lngReturn As Long
If IsMissing(varPath) Then varPath = "C:\"
strReturn = String(MAX_PATH, 0)
lngReturn = apiSearchTreeForFile(varPath, strFile, strReturn)
If lngReturn <> 0 Then
fFindFile = Left(strReturn, InStr(strReturn, Chr(0)) - 1)
End If
End Function
---------------------------------------------------------
I am looking for something equally simple.
Any help would be greatly appreciated.
FGP