Advertisement
2_2002-2004 Miscellaneous #122168

Directory and file enumeration

ListSubDirs: Lists names of all directories found under a given path. (Does not search recursively) ListFiles: Lists names of all files found under a given path. (Does not search recursively)

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
Function ListSubDirs(ByVal Path As String) As Variant
  'returns an array of directory names
  On Error Resume Next
  Dim Count, Dirs(), i, DirName ' Declare variables.
  DirName = Dir(Path, vbDirectory) ' Get first directory name.
  Count = 0
  Do While Not DirName = ""
    ' A file or directory name was returned
    If Not DirName = "." And Not DirName = ".." Then
      ' Not a parent or current directory entry so process it
      If GetAttr(Path & DirName) And vbDirectory Then
        ' This is a directory
        ' Increase the size of the array by one element
        ReDim Preserve Dirs(Count + 1)
        Dirs(Count) = DirName ' Add directory name to array
        Count = Count + 1 ' Increment counter.
      End If
    End If
    DirName = Dir ' Get another directory name.
  Loop
  ReDim Preserve Dirs(Count - 1) 'remove the last empty element
  ListSubDirs = Dirs()
End Function

Function ListFiles(ByVal Path As String) As Variant
  'returns an array of file names
  On Error Resume Next
  Dim Count, Files(), i, FileName ' Declare variables.
  Count = 0
  FileName = Dir(Path, 6) ' Get first file name.
  Do While Not FileName = ""
    If Not FileName = "." And Not FileName = ".." Then
      'Not a parent or current directory entry so process it
      If Not GetAttr(Path & FileName) And vbDirectory Then
        'This is a file
        'Increase the size of the array by one element
        ReDim Preserve Files(Count + 1)
        Files(Count) = FileName 'Add Filename to array.
        Count = Count + 1 'Increment counter
      End If
    End If
    FileName = Dir ' Get another file name.
  Loop
  ReDim Preserve Files(Count - 1) 'remove the last empty element
  ListFiles = Files()
End Function
Original Comments (3)
Recovered from Wayback Machine