You can use my function I wrote a while back. Drop a listbox and a command button:
Then you can use this function like this:Code: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 GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long Private Const MAX_PATH = 260 Private Const MAXDWORD = &HFFFF Private Const INVALID_HANDLE_VALUE = -1 Private Const FILE_ATTRIBUTE_ARCHIVE = &H20 Private Const FILE_ATTRIBUTE_DIRECTORY = &H10 Private Const FILE_ATTRIBUTE_HIDDEN = &H2 Private Const FILE_ATTRIBUTE_NORMAL = &H80 Private Const FILE_ATTRIBUTE_READONLY = &H1 Private Const FILE_ATTRIBUTE_SYSTEM = &H4 Private Const FILE_ATTRIBUTE_TEMPORARY = &H100 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 m_arrFiles() As String Private m_intCount As Integer Function FindFiles(ByRef p_strPath As String, ByRef p_strSearch As String, ByRef p_intFileCount As Integer, ByRef p_intDirCount As Integer, p_ListView As ListView) Dim strDirName As String Dim arrDirNames() As String Dim strFileName As String Dim intDirCount As Integer Dim i As Integer Dim lngSearchHandle As Long Dim WFD As WIN32_FIND_DATA Dim intCount As Integer Dim itmListItem As ListItem If Right(p_strPath, 1) <> "\" Then p_strPath = p_strPath & "\" ' Search for subdirectories. intDirCount = 0 ReDim arrDirNames(intDirCount) intCount = True lngSearchHandle = FindFirstFile(p_strPath & "*", WFD) If lngSearchHandle <> INVALID_HANDLE_VALUE Then Do While intCount strDirName = StripNull(WFD.cFileName) If (strDirName <> ".") And (strDirName <> "..") Then ' Check if it's a directory If GetFileAttributes(p_strPath & strDirName) And FILE_ATTRIBUTE_DIRECTORY Then arrDirNames(intDirCount) = strDirName p_intDirCount = p_intDirCount + 1 intDirCount = intDirCount + 1 ReDim Preserve arrDirNames(intDirCount) End If End If intCount = FindNextFile(lngSearchHandle, WFD) 'next subdirectory. Loop intCount = FindClose(lngSearchHandle) End If 'Walk through this directory and get file sizes. lngSearchHandle = FindFirstFile(p_strPath & p_strSearch, WFD) intCount = True If lngSearchHandle <> INVALID_HANDLE_VALUE Then Do While intCount strFileName = StripNull(WFD.cFileName) If (strFileName <> ".") And (strFileName <> "..") Then FindFiles = FindFiles + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow p_intFileCount = p_intFileCount + 1 ReDim Preserve m_arrFiles(m_intCount) m_arrFiles(m_intCount) = p_strPath & strFileName m_intCount = m_intCount + 1 End If intCount = FindNextFile(lngSearchHandle, WFD) 'Get next file Loop intCount = FindClose(lngSearchHandle) End If 'Check for SubDirectories If intDirCount > 0 Then For i = 0 To intDirCount - 1 FindFiles = FindFiles + FindFiles(p_strPath & arrDirNames(i) _ & "\", p_strSearch, p_intFileCount, p_intDirCount, p_ListView) Next i End If End Function Private Function IsArrayDimensioned(p_Array As Variant) As Boolean On Error Resume Next IsArrayDimensioned = IsNumeric(UBound(p_Array)) End Function Public Function StripNull(p_strString As String) As String If InStr(p_strString, vbNullChar) Then p_strString = Left(p_strString, InStr(p_strString, vbNullChar) - 1) End If StripNull = p_strString End Function
Regards,Code:Private Sub Command1_Click() Dim i As Integer Dim intNumFiles As Integer Dim intNumDirs As Integer Dim lngFileSize As Long Dim strMsg As String Dim strPath As String strPath = "C:\ARTISTS" lngFileSize = FindFiles(strPath, "*.*", intNumFiles, intNumDirs, ListView1) If IsArrayDimensioned(m_arrFiles) Then For i = 0 To UBound(m_arrFiles) List1.AddItem m_arrFiles(i) Next End If strMsg = intNumFiles & " Files found in " & intNumDirs + 1 & " Directories" & vbCrLf strMsg = strMsg & "Size of files found under " & strPath & " = " strMsg = strMsg & Format(lngFileSize, "#,###,###,##0") & " Bytes" MsgBox strMsg End Sub




Reply With Quote