Difference between revisions of "Searching directories recursively"

From HashVB
Jump to: navigation, search
 
(Tidied up the sample code and corrected the function name)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
{{VB6}}
 
This is a useful technique that allows you to search for files and build directory trees a lot faster than the intrinsic VB methods allow.
 
This is a useful technique that allows you to search for files and build directory trees a lot faster than the intrinsic VB methods allow.
  
 
It involves the use of the file finding APIs, the declarations for which are as follows:
 
It involves the use of the file finding APIs, the declarations for which are as follows:
  
    Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
+
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" _
    Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
+
  (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
+
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" _
    Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
+
  (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
   
+
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" _
    Const MAX_PATH = 260
+
  (ByVal lpFileName As String) As Long
   
+
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    Const INVALID_HANDLE_VALUE = -1
+
    Const FILE_ATTRIBUTE_ARCHIVE = &H20
+
Const MAX_PATH = 260
    Const FILE_ATTRIBUTE_DIRECTORY = &H10
+
    Const FILE_ATTRIBUTE_HIDDEN = &H2
+
Const INVALID_HANDLE_VALUE = -1
    Const FILE_ATTRIBUTE_NORMAL = &H80
+
Const FILE_ATTRIBUTE_ARCHIVE = &H20
    Const FILE_ATTRIBUTE_READONLY = &H1
+
Const FILE_ATTRIBUTE_DIRECTORY = &H10
    Const FILE_ATTRIBUTE_SYSTEM = &H4
+
Const FILE_ATTRIBUTE_HIDDEN = &H2
    Const FILE_ATTRIBUTE_TEMPORARY = &H100
+
Const FILE_ATTRIBUTE_NORMAL = &H80
   
+
Const FILE_ATTRIBUTE_READONLY = &H1
    Const MAXDWORD As Long = &HFFFFFFFF
+
Const FILE_ATTRIBUTE_SYSTEM = &H4
   
+
Const FILE_ATTRIBUTE_TEMPORARY = &H100
    Private Type FILETIME
+
        dwLowDateTime As Long
+
Const MAXDWORD As Long = &HFFFFFFFF
        dwHighDateTime As Long
+
    End Type
+
Private Type FILETIME
   
+
  dwLowDateTime As Long
    Private Type WIN32_FIND_DATA
+
  dwHighDateTime As Long
        dwFileAttributes As Long
+
End Type
        ftCreationTime As FILETIME
+
        ftLastAccessTime As FILETIME
+
Private Type WIN32_FIND_DATA
        ftLastWriteTime As FILETIME
+
  dwFileAttributes As Long
        nFileSizeHigh As Long
+
  ftCreationTime As FILETIME
        nFileSizeLow As Long
+
  ftLastAccessTime As FILETIME
        dwReserved0 As Long
+
  ftLastWriteTime As FILETIME
        dwReserved1 As Long
+
  nFileSizeHigh As Long
        cFileName As String * MAX_PATH
+
  nFileSizeLow As Long
        cAlternate As String * 14
+
  dwReserved0 As Long
    End Type
+
  dwReserved1 As Long
 +
  cFileName As String * MAX_PATH
 +
  cAlternate As String * 14
 +
End Type
  
 
NOTE: I have also included all the other necessary declarations for using the code below.
 
NOTE: I have also included all the other necessary declarations for using the code below.
Line 43: Line 47:
 
The code that will actually recurse through the directories goes like this:
 
The code that will actually recurse through the directories goes like this:
  
    Public Function RecurseCopyDirs(ByVal StartDir As String, ByVal DestDir As String) As Double
+
Public Function CountDirSize(ByVal StartDir As String) As Double
        Dim s As String
+
Dim Filename As String
        Dim hSearch As Long
+
Dim hSearch As Long
        Dim WFD As WIN32_FIND_DATA
+
Dim WFD As WIN32_FIND_DATA
        Dim Cont As Integer
+
Dim Cont As Integer
        Dim DirSize As Double
+
Dim DirSize As Double
       
+
        If Right$(StartDir, 1) <> "\" Then StartDir = StartDir & "\"
+
  'Add the final \
       
+
  If Right$(StartDir, 1) <> "\" Then StartDir = StartDir & "\"
        hSearch = FindFirstFile(StartDir & "*", WFD)
+
 
        If hSearch <> INVALID_HANDLE_VALUE Then
+
  'Enumerate the directories
            Cont = True
+
  hSearch = FindFirstFile(StartDir & "*", WFD)
            Do While Cont
+
  If hSearch <> INVALID_HANDLE_VALUE Then
                s = StripNulls(WFD.cFileName)
+
    Cont = True
               
+
    Do While Cont
                '-- Ignore the current and encompassing directories.
+
      Filename = StripNulls(WFD.cFileName)
                If (s <> ".") And (s <> "..") Then
+
     
                    '-- Check if it's a directory
+
      'Ignore the current and encompassing directories.
                    If GetFileAttributes(StartDir & s) And FILE_ATTRIBUTE_DIRECTORY Then
+
      If (Filename <> ".") And (Filename <> "..") Then
                        Debug.Print "DIR", StartDir & s
+
        'Check if it's a directory
                        DirSize = DirSize + RecurseCopyDirs(StartDir & s)
+
        If GetFileAttributes(StartDir & Filename) And FILE_ATTRIBUTE_DIRECTORY Then
                    Else
+
          Debug.Print "DIR", StartDir & Filename
                        Debug.Print "FILE", StartDir & s
+
          DirSize = DirSize + CountDirSize(StartDir & Filename)
                        DirSize = DirSize + CDbl(CStr(WFD.nFileSizeHigh)) * (MAXDWORD + 1) + WFD.nFileSizeLow
+
        Else
                    End If
+
          Debug.Print "FILE", StartDir & Filename
                End If
+
          DirSize = DirSize + CDbl(CStr(WFD.nFileSizeHigh)) * (MAXDWORD + 1) + WFD.nFileSizeLow
                Cont = FindNextFile(hSearch, WFD)
+
        End If
            Loop
+
      End If
            Cont = FindClose(hSearch)
+
      Cont = FindNextFile(hSearch, WFD)
        End If
+
    Loop
       
+
    Cont = FindClose(hSearch)
        RecurseCopyDirs = DirSize
+
  End If
    End Function
+
 
   
+
  CountDirSize = DirSize
    Function StripNulls(OriginalStr As String) As String
+
End Function
        If (InStr(OriginalStr, Chr(0)) > 0) Then
+
            OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
+
Function StripNulls(OriginalStr As String) As String
        End If
+
  If (InStr(OriginalStr, Chr(0)) > 0) Then
        StripNulls = OriginalStr
+
    OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
    End Function
+
  End If
 +
  StripNulls = OriginalStr
 +
End Function
  
 
This will print all of the files, directories and subdirectories under the directory that you provide it with to the Immediate window, and, as an added bonus, returns the total size of the files in the directory tree.
 
This will print all of the files, directories and subdirectories under the directory that you provide it with to the Immediate window, and, as an added bonus, returns the total size of the files in the directory tree.
  
 
Enjoy.
 
Enjoy.

Latest revision as of 13:53, 5 January 2007

float
 This article is based on Visual Basic 6. Find other Visual Basic 6 articles.

This is a useful technique that allows you to search for files and build directory trees a lot faster than the intrinsic VB methods allow.

It involves the use of the file finding APIs, the declarations for which are as follows:

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

Const MAX_PATH = 260

Const INVALID_HANDLE_VALUE = -1
Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_ATTRIBUTE_TEMPORARY = &H100

Const MAXDWORD As Long = &HFFFFFFFF

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

NOTE: I have also included all the other necessary declarations for using the code below.

The code that will actually recurse through the directories goes like this:

Public Function CountDirSize(ByVal StartDir As String) As Double
Dim Filename As String
Dim hSearch As Long
Dim WFD As WIN32_FIND_DATA
Dim Cont As Integer
Dim DirSize As Double

  'Add the final \
  If Right$(StartDir, 1) <> "\" Then StartDir = StartDir & "\"
  
  'Enumerate the directories
  hSearch = FindFirstFile(StartDir & "*", WFD)
  If hSearch <> INVALID_HANDLE_VALUE Then
    Cont = True
    Do While Cont
      Filename = StripNulls(WFD.cFileName)
      
      'Ignore the current and encompassing directories.
      If (Filename <> ".") And (Filename <> "..") Then
        'Check if it's a directory
        If GetFileAttributes(StartDir & Filename) And FILE_ATTRIBUTE_DIRECTORY Then
          Debug.Print "DIR", StartDir & Filename
          DirSize = DirSize + CountDirSize(StartDir & Filename)
        Else
          Debug.Print "FILE", StartDir & Filename
          DirSize = DirSize + CDbl(CStr(WFD.nFileSizeHigh)) * (MAXDWORD + 1) + WFD.nFileSizeLow
        End If
      End If
      Cont = FindNextFile(hSearch, WFD)
    Loop
    Cont = FindClose(hSearch)
  End If
  
  CountDirSize = DirSize
End Function

Function StripNulls(OriginalStr As String) As String
  If (InStr(OriginalStr, Chr(0)) > 0) Then
    OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
  End If
  StripNulls = OriginalStr
End Function

This will print all of the files, directories and subdirectories under the directory that you provide it with to the Immediate window, and, as an added bonus, returns the total size of the files in the directory tree.

Enjoy.