Get size of all files in a directory?

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
308
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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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,225,760
Messages
6,186,876
Members
453,381
Latest member
tcell

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