Results 1 to 5 of 5

Thread: Get all file names from a folder

  1. #1

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    Get all file names from a folder

    Hey all,

    How can I get the names of all objects inside a folder with the microsoft scripting object?

    thanks

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Get all file names from a folder

    Do you mean using the FileSystemObject? If so:

    VB Code:
    1. Private sFiles() As String
    2.  
    3. Private Sub Form_Load()
    4.   Erase sFiles
    5.   ReDim sFiles(0)
    6.   GetFiles App.Path
    7.   For N = LBound(sFiles) To (UBound(sFiles))
    8.     Debug.Print sFiles(N)
    9.   Next N
    10. End Sub
    11.  
    12. Private Sub GetFiles(ByVal sFolder As String)
    13.   Dim fso As New FileSystemObject
    14.   Dim fCurFolder As Folder
    15.   Dim fFolder As Folder
    16.   Dim fFile As File
    17.  
    18.   Set fCurFolder = fso.GetFolder(sFolder)
    19.   For Each fFolder In fCurFolder.SubFolders
    20.     GetFiles fFolder.Path
    21.   Next fFolder
    22.   For Each fFile In fCurFolder.Files
    23.     If sFiles(0) = vbNullString Then
    24.       sFiles(0) = fFile.Path
    25.     Else
    26.       ReDim Preserve sFiles(UBound(sFiles) + 1)
    27.       sFiles(UBound(sFiles)) = fFile.Path
    28.     End If
    29.   Next fFile
    30. End Sub
    Edit: this gives you files in SubFolders as well, btw.

  3. #3
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Get all file names from a folder

    Something like this?
    VB Code:
    1. Dim oFS As FileSystemObject, oFolder As Folder, oFile As File, oSub As Folder
    2.  
    3. Set oFS = New FileSystemObject
    4. Set oFolder = oFS.GetFolder("C:\")
    5.  
    6. For Each oSub In oFolder.SubFolders
    7.     Debug.Print oSub.Path
    8. Next oSub
    9.  
    10. For Each oFile In oFolder.Files
    11.     Debug.Print oFile.Name
    12. Next oFile
    13.  
    14. Set oFolder = Nothing
    15. Set oFS = Nothing

  4. #4

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    Re: Get all file names from a folder

    Thanks, but I can't find the File System Object in the references.

  5. #5
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Get all file names from a folder

    It's part of the Microsoft Scripting Runtime. If it isn't in your References list, browse for scrrun.dll.

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