Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

GetAllFiles - Search files in a directory or directory tree

Total Hit ( 4720)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Click here to copy the following block
Private Sub Command1_Click()
  Dim col As Collection

  '//Get all files from C:\ (No sub folder) which are older than 5 days and newer than 30 days
  Set col = GetAllFiles("c:\", "*.*", False, 30, 5)
  For Each f In col
    Debug.Print FileDateTime(f) & "=>" & f
  Next
End Sub


' Returns a collection with the names of all the files
' that match a file specification
'
' The file specification can include wildcards; multiple
' specifications can be provided, using a semicolon-delimited
' list, as in "*.tmp;*.bat"
'
' If RECURSEDIR is True the search is extended to all subdirectories
'
' It raises no error if path is invalid
'
' NewerThan (Days): If this is zero then no file stamp is check
'          If this is not zero then any file newer than N days will be skipped

' OlderThan (Days): If this is zero then no file stamp is check
'          If this is not zero then any file older than N days will be returned


Function GetAllFiles(ByVal path As String, Optional ByVal filespec As String = "*.*", _
 Optional RecurseDirs As Boolean = False, Optional NewerThan As Integer = 0, Optional OlderThan As Integer = 0) As Collection
  Dim spec As Variant
  Dim file As Variant
  Dim subdir As Variant
  Dim subdirs As New Collection
  Dim specs() As String
  Dim bFlag As Boolean
  Dim dtFileModified As Date

  ' initialize the result
  Set GetAllFiles = New Collection

  ' ensure that path has a trailing backslash
  If Right$(path, 1) <> "\" Then path = path & "\"

  ' get the list of provided file specifications
  specs() = Split(filespec, ";")

  ' this is necessary to ignore duplicates in result
  ' caused by overlapping file specifications
  On Error Resume Next

  ' at each iteration search for a different filespec
  For Each spec In specs
    ' start the search
    file = Dir$(path & spec)
    Do While Len(file)
      ' we've found a new file
      file = path & file

      If NewerThan > 0 Or OlderThan > 0 Then
        bFlag = True
        dtFileModified = FileDateTime(file)
        If (NewerThan > 0) Then
          If DateDiff("d", dtFileModified, Date) < NewerThan Then
            bFlag = True
          Else
            bFlag = False
          End If
        End If
        If (bFlag = True) And (OlderThan > 0) Then
          If DateDiff("d", dtFileModified, Date) > OlderThan Then
            bFlag = bFlag And True
          Else
            bFlag = False
          End If
        End If
        If bFlag = True Then
          GetAllFiles.Add file, file
        End If
      Else
        GetAllFiles.Add file, file
      End If

      ' get ready for the next iteration
      file = Dir$
    Loop
  Next

  ' first, build the list of subdirectories to be searched
  If RecurseDirs Then
    ' get the collection of subdirectories
    ' start the search
    file = Dir$(path & "*.*", vbDirectory)
    Do While Len(file)
      ' we've found a new directory
      If file = "." Or file = ".." Then
        ' exclude the "." and ".." entries
      ElseIf (GetAttr(path & file) And vbDirectory) = 0 Then
        ' ignore regular files
      Else
        ' this is a directory, include the path in the collection
        file = path & file
        subdirs.Add file, file
      End If
      ' get next directory
      file = Dir$
    Loop

    ' parse each subdirectory
    For Each subdir In subdirs
      ' use GetAllFiles recursively
      For Each file In GetAllFiles(subdir, filespec, True)
        If NewerThan > 0 Or OlderThan > 0 Then
          bFlag = True
          dtFileModified = FileDateTime(file)
          If (NewerThan > 0) Then
            If DateDiff("d", dtFileModified, Date) < NewerThan Then
              bFlag = True
            Else
              bFlag = False
            End If
          End If
          If (bFlag = True) And (OlderThan > 0) Then
            If DateDiff("d", dtFileModified, Date) > OlderThan Then
              bFlag = bFlag And True
            Else
              bFlag = False
            End If
          End If
          If bFlag = True Then
            GetAllFiles.Add file, file
          End If
        Else
          GetAllFiles.Add file, file
        End If
      Next
    Next
  End If
End Function


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.