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: 123

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
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,214,827
Messages
6,121,812
Members
449,048
Latest member
greyangel23

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