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

FileSearch API using Wildcards?

Status
Not open for further replies.

Flo79

Technical User
Nov 12, 2002
80
US
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


 
Flo79,
I'm not familiar with the API but have you looked at the [tt]Application.FileSearch[/tt] object? In my experience it does everything you need and doesn't require the headache of an external API.

Just my thoughts,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT+08:00) Singapore
 
CautionMP,
I've used the Application.Filesearch object.
The problem it has is that it no longer works in Office 2007.
Also, I find it too slow.
From what I've read using APIs is the fastest method.

 
I found the answer I was looking for here:


Paul Welter has created "FileSearch is a simple and fast file searching component that uses the Windows file search API. Source code and examples included."

The FileSearch.dll makes Windows API File Searching capabilities far more accessible through ActiveX.

Usage Example:
In Excel's Visual Basic Editor, include a reference to the FileSearch.dll file referenced above.

The below function will return the file Path of a file based on the file's name and a Search Directory. Its is similar to the SearchTreeForFile Windows API, but permits Wildcards.

Code:

----------------------------------------------------------
Public Function GetPath(strFileName As String, strDirectory As String) As String

Dim i As Long

Dim objSearch As Object
Dim objFile As Object

Set objSearch = CreateObject("FileSearch.Search")
Call objSearch.SearchFiles(strDirectory, strFileName, True)

If objSearch.Files.Count > 0 Then
GetPath = objSearch.Files.Item(1).FilePath & objSearch.Files.Item(1).FileName
Else: GetPath = "Not Found"
End If

Set objSearch = Nothing
Set objFile = Nothing
End Function
----------------------------------------------------------
Hope this helps you as much as its helped me!

Cheers!

FGP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top