VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Recursively searches folders to find a file.

by Malcolm Worthy (1 Submission)
Category: Files/File Controls/Input/Output
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Mon 7th June 2004
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Recursively searches folders to find a file.

API Declarations


Public Const MAX_PATH As Long = 260
Public Const INVALID_HANDLE_VALUE As Long = -1
Public Const FILE_ATTRIBUTE_ARCHIVE As Long = &H20
Public Const FILE_ATTRIBUTE_COMPRESSED As Long = &H800
Public Const FILE_ATTRIBUTE_DIRECTORY As Long = &H10
Public Const FILE_ATTRIBUTE_HIDDEN As Long = &H2
Public Const FILE_ATTRIBUTE_NORMAL As Long = &H80
Public Const FILE_ATTRIBUTE_READONLY As Long = &H1
Public Const FILE_ATTRIBUTE_TEMPORARY As Long = &H100
Public Const FILE_ATTRIBUTE_FLAGS = FILE_ATTRIBUTE_ARCHIVE Or _
FILE_ATTRIBUTE_HIDDEN Or _
FILE_ATTRIBUTE_NORMAL Or _
FILE_ATTRIBUTE_READONLY

Public Const DRIVE_UNKNOWNTYPE As Long = 1
Public Const DRIVE_REMOVABLE As Long = 2
Public Const DRIVE_FIXED As Long = 3
Public Const DRIVE_REMOTE As Long = 4
Public Const DRIVE_CDROM As Long = 5
Public Const DRIVE_RAMDISK As Long = 6

Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Public Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type

Public Declare Function FindFirstFile Lib "kernel32" _
Alias "FindFirstFileA" _
(ByVal lpFileName As String, _
lpFindFileData As WIN32_FIND_DATA) As Long

Public Declare Function FindNextFile Lib "kernel32" _
Alias "FindNextFileA" _
(ByVal hFindFile As Long, _
lpFindFileData As WIN32_FIND_DATA) As Long

Rate Recursively searches folders to find a file.



    ' sPath = Path to start search in, must end in "\"
    ' eg. "c:\windows\"
    ' sFileToFind = Name of the file you want to find
    ' Returns the full path of the first instance of the file,
    ' a blank string if the file is not found.
    
    Dim WFD As WIN32_FIND_DATA
    Dim hFile As Long
    Dim sFileFound As String
    
    hFile = FindFirstFile(sPath & "*.*", WFD)
    
    If hFile <> INVALID_HANDLE_VALUE Then
        Do
            sFileFound = WFD.cFileName
            sFileFound = Replace(Trim(sFileFound), Chr(0), "")
            
            If (WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) _
                = WFD.dwFileAttributes Then
                ' Is Folder
                If sFileFound <> "." And sFileFound <> ".." Then
                    FindMyFile = FindMyFile(sPath & sFileFound & "\", sFileToFind)
                    If FindMyFile <> "" Then
                        Exit Function
                    End If
                End If
            Else
                ' Is File, check if found
                ' Could make a change here so it handles wildcard searches
                If LCase(sFileToFind) = LCase(sFileFound) Then
                    FindMyFile = sPath & sFileFound
                    Exit Function
                End If
            End If
            WFD.cFileName = Space(MAX_PATH)
        Loop While FindNextFile(hFile, WFD)
    End If
    FindMyFile = ""
End Function


Download this snippet    Add to My Saved Code

Recursively searches folders to find a file. Comments

No comments have been posted about Recursively searches folders to find a file.. Why not be the first to post a comment about Recursively searches folders to find a file..

Post your comment

Subject:
Message:
0/1000 characters