by Gene Baker (2 Submissions)
Category: Windows API Call/Explanation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Mon 28th January 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)
searches a directory using the api calls findfirstfile and findnextfile.
API Declarations
'the path i used was the system32 folder due to the high amount of dll's
'located you can use an outer loop to enum thru a dir and folders....
'add a module and copy the enum to it********************
Public Enum RESULT 'copy this to a module
NO_FILES_FOUND = 0
ERROR_IN_SEARCH = 1
SUCCESS = 2
End Enum
'********************************************************
Private AryFiles() As String
Private m_intCnt As Integer
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" _
(ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" _
(ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32.dll" (ByVal hFindFile As Long) As Long
Private Const MAX_PATH As Integer = 260
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private 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
Private FindFiles As WIN32_FIND_DATA
Private fileT As String
Private U_NAME As String
Private U_PWD As String
Public SetDailyDir As String
Public SetArchiveDir As String
Public SetDefaultDir As String
Public LookIn As String
Private Function Load_Ary(ByVal strPath As String, ByVal strMask As String) As RESULT
Dim handle As Long
Dim finfo As WIN32_FIND_DATA
Dim found As Long
Dim BUFFER As String
Dim rval As Long
On Error GoTo Eh
handle = FindFirstFile(strPath & strMask, finfo)
If handle = -1 Then
Load_Ary = RESULT.NO_FILES_FOUND
Exit Function
End If
Do
BUFFER = Space(260)
BUFFER = Left(finfo.cFileName, InStr(finfo.cFileName, Chr(0)) - 1) 'Extract the found filename
ReDim Preserve AryFiles(m_intCnt)
AryFiles(m_intCnt) = strPath & BUFFER
m_intCnt = m_intCnt + 1
found = FindNextFile(handle, finfo)
Loop Until found = 0
rval = FindClose(handle)
Load_Ary = RESULT.SUCCESS
Exit Function
Eh:
Load_Ary = RESULT.ERROR_IN_SEARCH
End Function
Private Sub Form_Load()
Dim strFound As String
Dim intRes As Integer
On Error GoTo Eh:
'change the path to your system32 folder....to demonstrate the speed of the search.
m_intCnt = 0
If strFound <> "." And strFound <> ".." Then
intRes = Load_Ary("C:\WINNT\system32\", "*.DLL")
If intRes = RESULT.ERROR_IN_SEARCH Then
Exit Sub
End If
End If
MsgBox UBound(AryFiles) & " FILES FOUND"
Exit Sub
Eh:
End Sub
No comments have been posted about searches a directory using the api calls findfirstfile and findnextfile.. Why not be the first to post a comment about searches a directory using the api calls findfirstfile and findnextfile..