Visual Basic File API Routines
SearchTreeForFile: Search a Drive to Locate a Specific File
Posted:   Saturday June 12, 1999
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows NT4
OS restrictions:   Requires Imagehlp.dll
Author:   Eduardo Morcillo


FindFirstFile: Save a Recursive Search of All Drives to Disk
FindFirstFile: Save a Recursive Search of Specified Drives to Disk
FindFirstFile: Performance Comparison - FSO vs. API
Imagehlp.dll. On NT, requires version 4.0 or later. On Win9x, requires Windows 95 or later. The DLL is available as a redistributable for Windows 95x.

This is possibly one of the simplest methods to locate a specific file on a drive, given the filename and drive letter.  Returns the full path to the file, or the original string passed (Trim$(x) = ""). Just place the code in the form or module as appropriate for your need.

Taken from a msnews.winapi posting by Eduardo Morcillo.

To search for multiple file types (i.e. a search for all *.frm;*.bas files) by specifying such a pattern as the extension of interest, see FindFirstFile: Recursive File Search for Single or Multiple File Types (minimal code) and FindFirstFile: Recursive File Search Including/Excluding Single or Multiple File Types (minimal code)

 BAS Module Code

 Form Code
To a form with a command button, add the following code:

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
Private Const MAX_PATH As Long = 260
Private Declare Function SearchTreeForFile Lib "imagehlp" _
(ByVal sRootPath As String, _
 ByVal InputPathName As String, _
 ByVal OutputPathBuffer As String) As Boolean

Private Sub Command1_Click()

   Dim sFile2Find As String
   Dim sRootPath2Search As String
   sFile2Find = "msvbvm50.dll"
   sDrive2Scan = "c:\"
   Label1.Caption = "Working ..."
   Label1.Caption = LCase$(GetFileLocation(sDrive2Scan, sFile2Find))

End Sub

Private Function GetFileLocation(sDrive2Scan As String, sFile2Find As String) As String

   'API returns True if found, or False otherwise.
   'If True, sResult holds the full path
    Dim sResult As String
    sResult = Space(MAX_PATH)

    If SearchTreeForFile(sDrive2Scan, sFile2Find, sResult) Then
       GetFileLocation = Left(sResult, InStr(sResult, vbNullChar) - 1)
       GetFileLocation = "File was not found.  Error: " & Err.LastDllError
    End If

End Function
Wildcards are not permitted in the file name to locate. But because the data is cached, subsequent searches along the same path are instantaneous.


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter