Get size of all files in a directory?

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
270
Office Version
  1. 365
Platform
  1. Windows
I have the following which lists all files in a directory, it works perfectly
Code:
Option Explicit
Sub GetFileNames()
Dim xRow As Long
Dim xDirect$, xFname$, InitialFoldr$
InitialFoldr$ = "D:\" '<<< Startup folder to begin searching from
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select a folder to list Files from"
.InitialFileName = InitialFoldr$
.Show
If .SelectedItems.Count <> 0 Then
xDirect$ = .SelectedItems(1) & "\"
xFname$ = Dir(xDirect$, 7)
Do While xFname$ <> ""
ActiveCell.Offset(xRow) = xFname$
xRow = xRow + 1
xFname$ = Dir
Loop
End If
End With
End Sub

How can I modify this to bring in the filesize in the next column?

Thanks in advance.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try this

VBA Code:
Sub jec()
 With Application.FileDialog(msoFileDialogFolderPicker)
     InitialFoldr = "D:\"
    .Title = "Please select a folder to list Files from"
    .InitialFileName = InitialFoldr
     If .Show Then
    
       c00 = .SelectedItems(1)
       Set Folder = CreateObject("Scripting.FileSystemObject").getfolder(c00)
       ReDim ar(Folder.Files.Count, 2)
     
       For Each it In Folder.Files
          ar(x, 0) = it.Name
          ar(x, 1) = Int(it.Size / 1000)
          ar(x, 2) = "KB"
          x = x + 1
       Next
      Sheets(1).Cells(1, 1).Resize(x, 3) = ar
    End If
 End With
End Sub
 
Upvote 0
Solution
Thanks a lot, had to initialise the variables via a Dim statement but it works perfectly!
 
Upvote 0
Nice! You’re welcome!
 
Upvote 0

Forum statistics

Threads
1,215,030
Messages
6,122,762
Members
449,095
Latest member
m_smith_solihull

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