Hi Guys,
Today I will show you how to select a folder from your computer and list all the files in it.
The Macro will also give you information about the files like:
1 - Name
2 - Size
3 - Date and Time
Sub ListFilesInAFolder()
'######################################################################
'Author Paolo Succo
'Date 12 December 2011
'Macro to list files in a folder
'######################################################################
Dim directory As String
Dim r As Long
Dim f As String
Dim path As String
'Select folder
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select a location for the file list"
.Show
Range("A:C").Select
Selection.Delete
ActiveSheet.Shapes.Range(Array("Bevel 1")).Select
Selection.ShapeRange.IncrementLeft 150#
Selection.ShapeRange.IncrementTop -0.75
If .SelectedItems.Count = 0 Then
MsgBox "Canceled"
Else
path = .SelectedItems(1)
path = directory
r = 1
'Headers
Cells(r, 1) = "Filename"
Cells(r, 2) = "Size"
Cells(r, 3) = "Date/Time"
Range("a1:c1").Font.Bold = True
'Get file
f = Dir(directory, vbReadOnly + vbHidden + vbSystem)
Do While f <> ""
r = r + 1
Cells(r, 1) = f
Cells(r, 2) = FileLen(directory & f)
Cells(r, 3) = FileDateTime(directory & f)
'get next file
f = Dir()
Loop
End If
End With
Cells.EntireColumn.AutoFit
End Sub
Download an example:
Files_List.xlsm
No comments:
Post a Comment