can i get a list of files in specified folder?

merlin_the_magician

Active Member
Joined
Jul 31, 2002
Messages
480
In order to create an external cell reference that will not result in highly annoying errors I need a formula or VBA code that will allow me to retrieve the filenames in a specified path. I know this is possible, just how?
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
This code loads all names of files from "C:\Workings" into array "FilesList", you will have to add a reference to the Microsoft Scripting Runtime.


Sub AllFilesInFolder()

Dim objFS As New Scripting.FileSystemObject
Dim objFolder As Scripting.folder
Dim objFile As Scripting.file

Dim FilesList() As String
Dim i As Integer

If objFS.FolderExists("C:\Workings") Then
Set objFolder = objFS.GetFolder("C:\Workings")
Else
MsgBox "Folder doesn't exist"
Exit Sub
End If


ReDim FilesList(1 To objFolder.Files.Count)
i = 1

For Each objFile In objFolder.Files
FilesList(i) = objFile.Name
i = i + 1
Next objFile

Set objFile = Nothing
Set objFolder = Nothing
Set objFS = Nothing

End Sub

HTH
 
Upvote 0
eeehmm.... question..:

i tried this, but it seems not to be functioning.

By "adding a reference to the Microsoft Scripting Runtime" I presume you mean defining an array range, right?
 
Upvote 0
Code:
Sub ListFiles()
    Dim fileList() As String
    Dim fName As String
    Dim fPath As String
    Dim I As Integer
   
    fPath = "C:\My Documents\"
    fName = Dir(fPath & "*.xls")
    While fName <> ""
        I = I + 1
        ReDim Preserve fileList(1 To I)
        fileList(I) = fName
        fName = Dir()
    Wend
    If I = 0 Then
        MsgBox "No files found"
        Exit Sub
    End If
     For I = 1 To UBound(fileList)
        Range("A" & I).Value = fileList(I)
     Next
    End Sub

Hope this helps,

Will
 
Upvote 0
As you want to use objects other than Excel objects you have to add references: while in VBA editor: Tools>References>Microsoft Scripting Runtime

HTH
 
Upvote 0
Both methods seems to be doing nothing really... What am i doing wrong? I follewed your code exactly Amigos, including the path you are using. Created the folder and copied some .xls files into it, but still no result.

Where are the names to be listed anyway? I created an array range as "fileslist", but no good... :oops:
 
Upvote 0
OK,
filenames (in both codes) are stored in array called "FilesList" in mine and "fileList" in WillR's code. To access them you use FilesList(i) where "i" is index number of file in the array. So change part of the code into:

For Each objFile In objFolder.Files
FilesList(i) = objFile.Name
ActiveSheet.Cells(i, 1).Value = FilesList(i)
i = i + 1
Next objFile

and you will display all files in first column starting from first row.
now you can access them from this sheet or you can still access them from "the memory" using FilesList(i), so FilesList(3) will give you third file.


HTH
 
Upvote 0
You have whole code below, what happends when you run it?

Sub AllFilesInFolder()

Dim objFS As New Scripting.FileSystemObject
Dim objFolder As Scripting.folder
Dim objFile As Scripting.file

Dim FilesList() As String
Dim i As Integer

If objFS.FolderExists("C:\Workings") Then ' change this part into your folder
Set objFolder = objFS.GetFolder("C:\Workings") ' change this part into your folder
Else
MsgBox "Folder doesn't exist" ' if folder doesn't exist - end sub
Exit Sub
End If


ReDim FilesList(1 To objFolder.Files.Count) ' array for file names resizes
i = 1

For Each objFile In objFolder.Files '- for each file in folder
FilesList(i) = objFile.Name ' - load file name to array
ActiveSheet.Cells(i, 1).Value = FilesList(i) ' - print file name on Sheet1 in first column and row i
i = i + 1
Next objFile

Set objFile = Nothing
Set objFolder = Nothing
Set objFS = Nothing

End Sub

I can email you file but I need your email - you can PM me.
 
Upvote 0
Merlin,

I tested my code before posting it and it works fine.

It lists the found files in the active worksheet, from A1 down.

If no files are found, you get a msgbox telling you

Obviously you need to amend my hard-coded directory path to one that suits you "C:\your path\your folder\"

Will
 
Upvote 0

Forum statistics

Threads
1,216,033
Messages
6,128,427
Members
449,450
Latest member
gunars

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