List all files in a folder

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Guest
I am writing a macro to get a list of subfolders and files in a folder and link the cells to the corresponding files. Here is the macro


Sub ShowAllList(folderspec)
Dim fs, f, f1, f2, f3, fc, fd, fe
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set fc = f.Files
Set fd = f.subfolders
i = 1
For Each f1 In fc
Cells(i, 1) = f1.Name
ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, 1), Address:=f1
i = i + 1
Next
For Each f2 In fd
Cells(i, 1) = f2.Name
i = i + 1
Set fe = f2.Files
For Each f3 In fe
Cells(i, 1) = f2.Name
Cells(i, 2) = f3.Name
ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, 2), Address:=f3
i = i + 1
Next
Next
End Sub

Sub listallfiles()
ShowAllList ("h:kcy")
End Sub


It works if in there is only one level of subfolders (which is usually not the case). I want to rewrite it so that it can handle arbitray numbers of levels of subfolders. Can anybody help? Thank you.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Example of what dave suggested;

Sub List_Files()
Dim cDir as string
cDir = "C:Downloads"

With Application.FileSearch
.NewSearch
.FileType = msoFileTypeAllFiles
.LookIn = cDir
.SearchSubFolders = True
.Execute
For i = 1 To .FoundFiles.Count
Cells(i, 1) = .FoundFiles(i)
Next

End sub


Ivan
 
Upvote 0
Nice code Ivan
There's only a END WITH missing before END SUB ?
Let's build the code a bit more...
Be sure there is a Userform1 with a Listbox1
VBA Code:
Sub List_Files()
Dim cDir As String
cDir = "C:Downloads"

UserForm1.ListBox1.Clear
With Application.FileSearch
    .NewSearch
    .FileType = msoFileTypeAllFiles
    .LookIn = cDir
    .SearchSubFolders = True
    .Execute
    For i = 1 To .FoundFiles.Count
        Cells(i, 1) = .FoundFiles(i)
        UserForm1.ListBox1.AddItem .FoundFiles(i)
    Next
End With
'show the list
UserForm1.Show
' as an example open the selected file
Workbooks.Open FileName:=UserForm1.ListBox1.Value
End Sub


This message was edited by Bruno on 2002-03-06 23:14
 
Last edited by a moderator:
Upvote 0
On 2002-03-06 22:33, Anonymous wrote:
Example of what dave suggested;

Sub List_Files()
Dim cDir as string
cDir = "C:Downloads"

With Application.FileSearch
.NewSearch
.FileType = msoFileTypeAllFiles
.LookIn = cDir
.SearchSubFolders = True
.Execute
For i = 1 To .FoundFiles.Count
Cells(i, 1) = .FoundFiles(i)
Next

End sub


Ivan

Thank you.
But can I link the file names to the files, list details such as file sizes etc.

I tried the following (I don't understand FileSearch well):

Sub ShowFileList(folderspec)
Dim fs, f, f1, s, sf
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
Set sf = f.Files
i = ActiveCell.Row
For Each f1 In sf
Cells(i, 1) = f1.Path
Cells(i, 2) = f1.Size
Cells(i, 3) = f1.DateLastModified
ActiveSheet.Hyperlinks.Add Anchor:=Cells(i, 1), Address:=f1
i = i + 1
Next
Cells(i, 1).Select
End Sub

Sub Showdd(folderspec)
Dim fs, f, f1, s, sf
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
ShowFileList (f)
Set sf = f.SubFolders
For Each f1 In sf
Showdd (f1)
Next
End Sub


Sub ShowAll()
Cells(1, 1).Select
Showdd ("h:kcy")
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,401
Messages
6,171,892
Members
452,431
Latest member
TiffanyMcllwain

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