How to Get Detailed List of Files from a Folder in PC

In this tutorial, we will learn how to get list of files from a folder in PC to a Excel sheet. Suppose that you have a folder in your computer that contains different files. You want to create a detailed list of the files with their file name, file type, date created, date modified, size of the file etc. in an Excel worksheet.

Follow below steps to do this.

  1. Get the VBA Code
  2. Install the VBA code in Excel
  3. Modify the Folder Path
  4. Run the code

Get the VBA Code

As we are going to do this by using VBA method, first of all you need to have the VBA code. You can copy the VBA code directly from below.

Sub ListFilesInFolder()

    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim ws As Worksheet
    Dim i As Long
    
    ' Specify the folder path
    Dim folderPath As String
    folderPath = "Your Folder Path"
    
    ' Create a FileSystemObject
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    ' Get the folder object
    Set objFolder = objFSO.GetFolder(folderPath)
    
    ' Create a new worksheet
    Set ws = ThisWorkbook.Sheets.Add
    
    ' Display headers
    ws.Range("A1").Value = "File Name"
    ws.Range("B1").Value = "File Type"
    ws.Range("C1").Value = "Date Created"
    ws.Range("D1").Value = "Date Modified"
    ws.Range("E1").Value = "Size (Bytes)"
    
    ' Initialize row counter
    i = 2
    
    ' Loop through each file in the folder
    For Each objFile In objFolder.Files
        ' Display file details
        ws.Cells(i, 1).Value = objFile.Name
        ws.Cells(i, 2).Value = objFSO.GetExtensionName(objFile.Path)
        ws.Cells(i, 3).Value = objFile.DateCreated
        ws.Cells(i, 4).Value = objFile.DateLastModified
        ws.Cells(i, 5).Value = objFile.Size
        
        ' Increment row counter
        i = i + 1
    Next objFile
    
    ' Auto-fit columns
    ws.Columns.AutoFit
    
    ' Clean up
    Set objFSO = Nothing
    Set objFolder = Nothing
    Set objFile = Nothing
    Set ws = Nothing

End Sub

Install the Code in Excel

To Install the code in Excel, follow these steps.

  1. Seclect and copy the code.
  2. In Excel window, click on “Developer” tab. Then “Visual Basic.”
  3. In VBA window, click on “Insert” tab. Then “Module”.
  4. Paste the copied VBA code.

Modify the Folder Path

To modify the folder path, copy the folder address from the folder address bar. Then, paste it in “Your File Path” section of VBA code. See below image animation for how to copy the folder path correctly.

Note: to display the folder path, single click inside the folder address bar. Right Click on the blue highlighted portion to get copy option.

Run the Code

Now, in the final step, run the code by clicking in the run button (in VBA window). The details of all the files in the folder will extract to excel sheet.

Once, all details extract, close the VBA window. See below image animation for how the details extracts in Excel sheet.

get list of files from a folder

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top