Getting List of file in certain folder

nareshjoshy

New Member
Joined
Mar 6, 2019
Messages
23
Dera Sir,
I want to get all the name of files contain in certain folder in Excel by using VBA Code. I want to update all the files name in certain column in certain sheet. Can It possibe in excell.

Thank You in advance.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Change text in red by your data

Code:
Sub getFiles()
    Dim folder As String, sfiles As Variant, i As Long
    folder = "[COLOR=#ff0000]C:\trabajo\files\[/COLOR]"
    If Right(folder, 1) <> "\" Then folder = folder & "\"
    
    wfiles = Dir(folder & "*.*")
    i = 1
    Do While wfiles <> ""
        Sheets("[COLOR=#ff0000]Sheet1[/COLOR]").Cells(i, "[COLOR=#ff0000]A[/COLOR]").Value = wfiles
        i = i + 1
        wfiles = Dir()
    Loop
End Sub
 
Upvote 0
Thank you danteamor, It is working well.

Can we get
"C:\Users\DEO\Desktop\guarantee"
automatically where my file located.
I mean to say if My fie is located in "C:\Users\DEO\Desktop\guarantee" then it should be "C:\Users\DEO\Desktop\guarantee" but when I shift my file to folder
"C:\Users\DEO\Desktop\filefolder" it should be change to "C:\Users\DEO\Desktop\guarantee" automatic.
 
Upvote 0
Change:
Code:
folder = "C:\trabajo\files\"
By:
Code:
folder = thisworkbook.path & "\"
 
Last edited:
Upvote 0
ilThank You sir, One More query to you.

Is there any option or code to execute this macro automatic when I open this file.
 
Upvote 0
In the events of thisworkbook put:

Code:
Private Sub Workbook_Open()
    Call getFiles
End Sub

Press Alt+F11 to VBA-Editor, press doubleclick on Thisworkbook, in the panel paste de code.
 
Upvote 0
Thank You. It is working.

In This Code:
Code:
[COLOR=#333333]Sub getFiles()[/COLOR]    Dim folder As String, sfiles As Variant, i As Long
    folder = "[COLOR=#ff0000]C:\trabajo\files\[/COLOR]"
    If Right(folder, 1) <> "\" Then folder = folder & "\"
    
    wfiles = Dir(folder & "*.*")
    i = 1
    Do While wfiles <> ""
        Sheets("[COLOR=#ff0000]Sheet1[/COLOR]").Cells(i, "[COLOR=#ff0000]A[/COLOR]").Value = wfiles
        i = i + 1
        wfiles = Dir()
    Loop [COLOR=#333333]End Sub[/COLOR]
what should be added to clear previous data of updated column only and then add new data.
 
Upvote 0
Thank You. It is working.

In This Code:

what should be added to clear previous data of updated column only and then add new data.

Code:
Sub getFiles()    Dim folder As String, sfiles As Variant, i As Long
    folder = "C:\trabajo\files\"
    If Right(folder, 1) <> "\" Then folder = folder & "\"
    
    [COLOR=#0000ff]Sheets("Sheet1").columns("A").clearcontents[/COLOR]


    wfiles = Dir(folder & "*.*")
    i = 1
    Do While wfiles <> ""
        Sheets("Sheet1").Cells(i, "A").Value = wfiles
        i = i + 1
        wfiles = Dir()
    Loop 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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