Extract file names with extensions and absolute path with VBA code

IreneFoncillas

New Member
Joined
Oct 13, 2020
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am trying to create a macro in excel for extract file names (with extension) and their absolute path, from one main path (always a local path), but I am afraid I am not able to.

My idea is to insert in cell C2 the main path, where there are files, folders and subfolders (with files inside them) and get a list of all files with their extension from B4 and absolute path from C4. Something like in the image I attach.

Can anyone help me?

Thank you!!!
 

Attachments

  • Screenshot_1.png
    Screenshot_1.png
    18.3 KB · Views: 143

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
This code is likely to do what you're after. Because of the self-repeating behavior of the code (recursion) it was necessary to split the code into two procedures.
The GetFiles procedure actually does the job, although so you have to call the ListFiles procedure for the desired result.

VBA Code:
Public Sub ListFiles()

    Dim sPathSource As String, raListFileName As Range, raListPathName As Range

    With ActiveSheet
        sPathSource = .Range("C2").Text
        Set raListFileName = .Range("B5")
        Set raListPathName = .Range("C5")
    End With

    Call GetFiles(sPathSource, raListFileName, raListPathName)
End Sub


Public Sub GetFiles(ByVal argSourcePath As String, ByRef argTopOfFileList As Range, ByRef argTopOfPathList As Range)

    Dim FSO As Object, oRoot As Object, oFile As Object, oFolder As Object
    Dim sPathSource As String

    sPathSource = argSourcePath
    Set FSO = CreateObject("Scripting.FileSystemObject")
    If FSO.FolderExists(sPathSource) And Not argTopOfFileList Is Nothing And Not argTopOfPathList Is Nothing Then
        Set oRoot = FSO.GetFolder(sPathSource)
        For Each oFile In oRoot.Files
            argTopOfFileList.Value = oFile.Name
            argTopOfPathList.Value = oFile.ParentFolder.Path
            Set argTopOfFileList = argTopOfFileList.Offset(1)
            Set argTopOfPathList = argTopOfPathList.Offset(1)
        Next oFile
        DoEvents
        For Each oFolder In oRoot.SubFolders
            Call GetFiles(oFolder.Path, argTopOfFileList, argTopOfPathList)
        Next oFolder
    End If
End Sub
 
Upvote 0
This is easy enough using Powerquery (In Excel: Data, Get Data, From File, From Folder).
 
Upvote 0
Thank you both for your response.

VBA Code is super, it works perfectly for what I need.

Regarding Powerquery, I didn't know about that, so thank you for the information. It could work for my purpose but VBA is faster for me. Anyway I will take it into account for further works.

Thanks!!!
 
Upvote 0
You are welcome and thanks for letting us know.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top