Results 1 to 2 of 2

Thread: Generating a list of files in a directory

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Location
    London
    Posts
    1

    Question

    I'm sure this has been asked before, but I've looked around and not found any help.

    I'm trying to generate a list of all files and folders in a specified directory, but am having no luck. I want to store a list of something like:

    directory level, path+filename

    The code I've got so far is at the end of this message. It is for a function that should recursively call itself as it walks down the directory tree, building a buffer of all files and directories it finds called dirNames() as it goes. However, it stops at the Dir() call as it pops out of the first recursive call of itself.

    Am I going about this the wrong way? Any ideas of what's going wrong, and how to remedy it?

    Thanks,

    Rob

    ---

    Sub FindAllFiles(ByVal nDir As Integer, dirNames() As String, ByVal SearchPath As String, ByVal SearchString As String, ByVal SearchTarget As String, ByVal SearchType As Boolean)

    Dim DirName As String ' SubDirectory Name.

    Const NO_FILES_IN_DIR As Long = 9
    Const INVALID_DIR As Long = 13

    On Error GoTo Test_Err

    If Right(SearchPath, 1) <> "\" Then SearchPath = SearchPath & "\"

    ReDim dirNames(nDir)
    DirName = Dir(SearchPath, vbDirectory Or vbHidden)
    Do While Len(DirName) > 0
    ' Ignore the current and encompassing directories.
    If (DirName <> ".") And (DirName <> "..") Then
    ' Check for directory with bitwise comparison.
    If GetAttr(SearchPath & DirName) And vbDirectory Then
    dirNames(nDir) = SearchPath + DirName + "\"
    ' parse a directory name
    nDir = nDir + 1
    ReDim Preserve dirNames(nDir)
    NewSearchPath = SearchPath & DirName
    FindAllFiles nDir, dirNames(), NewSearchPath, SearchString, SearchTarget, SearchType
    Else
    ' parse the filename

    End If
    End If
    DirName = Dir() ' Get next subdirectory.
    Loop

    AbortFunction:
    Exit Sub

    Test_Err:
    Select Case Err.Number
    Case NO_FILES_IN_DIR
    MsgBox "The directory named '" & strDirName & "' contains no files."
    Case INVALID_DIR
    MsgBox "'" & strDirName & "' is not a valid directory."
    Case 0
    Case Else
    MsgBox "Error #" & Err.Number & " - " & Err.Description
    End Select

    End Sub

    ---

    Where (for example):

    SearchPath = "C:\WINDOWS\Temporary Test"
    SearchString = ".url"
    SearchTarget = "_top"
    SearchType = True
    nDir = 0

    nDir is the number of directories found.
    SearchType, SearchTarget and SearchString will be used later to process the filenames.

  2. #2
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    There are actually plenty of examples on this site.

    This Thread has a couple.
    Iain, thats with an i by the way!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width