Results 1 to 3 of 3

Thread: List Of All Files In A Folder

  1. #1
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10

    List Of All Files In A Folder

    This macro creates a list of all files within a folder.

    Excel 2007+ version
    Code:
    Sub ListAllFile()
    
        Dim objFSO As Object
        Dim objFolder As Object
        Dim objFile As Object
        Dim wks As Worksheet
        
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set wks = Worksheets.Add
        
        'Get the folder object associated with the directory
        Set objFolder = objFSO.GetFolder("C:\")
        wks.Cells(1, 1).Value = "The files found in " & objFolder.Name & "are:"
        
        'Loop through the Files collection
        For Each objFile In objFolder.Files
            wks.Cells(wks.UsedRange.Rows.Count + 1, 1).Value = objFile.Name
        Next
        
        'Clean up!
        Set objFolder = Nothing
        Set objFile = Nothing
        Set objFSO = Nothing
    
    End Sub
    Excel 2003- version

    Code:
    Sub ListAllFiles()
    
        'Not for Excel 2007
        Dim objFileSearch As FileSearch, wks As Worksheet, lngLoop As Long
        Set objFileSearch = Application.FileSearch
        With objFileSearch
            .SearchSubFolders = False ' set to true if you want sub-folders included
            .FileType = msoFileTypeAllFiles 'can modify to just Excel files eg with msoFileTypeExcelWorkbooks
            .LookIn = "C:\"  'modify this to where you want to serach
            If .Execute > 0 Then
                Set wks = Worksheets.Add
                For lngLoop = 1 To .FoundFiles.Count
                    wks.Cells(lngLoop, 1) = Mid$(.FoundFiles(lngLoop), InStrRev(.FoundFiles(lngLoop), "\") + 1)
                Next
            Else
                MsgBox "No files found"
            End If
        End With
        
        Set objFileSearch = Nothing
        Set wks = Nothing
        lngLoop = Empty
        
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Additional info at Chip Pearson's site
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  3. #3
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi,

    Another one here
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

Similar Threads

  1. Moving Multiple Files From One Folder To Another
    By galang_ofel in forum Excel Help
    Replies: 5
    Last Post: 05-10-2013, 12:43 AM
  2. Save Processed Files Into Different Another Folder
    By DARSHANKmandya in forum Excel Help
    Replies: 1
    Last Post: 03-22-2013, 07:10 PM
  3. List File name in folder to excel with images
    By Ryan_Bernal in forum Excel Help
    Replies: 2
    Last Post: 01-15-2013, 11:37 AM
  4. Replies: 2
    Last Post: 09-24-2012, 09:20 PM
  5. Count Files In A Folder VBA
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 05-07-2011, 10:57 PM

Tags for this Thread

Posting Permissions

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