vba code to display total size of a directory

Rudi GOUSSEY

New Member
Joined
Jan 8, 2004
Messages
32
Does anybody knows the VBA-code which shows the total size of a directory (subdirectories included).
Thanks
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Hi!
try this one!


<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> ShowFreeSpace()
drvPath = "c:\"
    <SPAN style="color:#00007F">Dim</SPAN> fs, d, s
    <SPAN style="color:#00007F">Set</SPAN> fs = CreateObject("Scripting.FileSystemObject")
    <SPAN style="color:#00007F">Set</SPAN> d = fs.GetDrive(fs.GetDriveName(drvPath))
    s = "Drive " & UCase(drvPath) & " - "
    s = s & d.VolumeName & vbCrLf
    s = s & "Free Space: " & FormatNumber(d.FreeSpace / 1024, 0)
    s = s & " Kbytes"
    MsgBox s
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
 
Upvote 0
Hi Rudi,

Try this:
Code:
Sub FolderSize()
    Dim fso As Object, fsoFolder As Object
    Const strFolderName As String = "C:\My Documents"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fsoFolder = fso.GetFolder(strFolderName)

    MsgBox fsoFolder.Size & " bytes"

    Set fsoFolder = Nothing
    Set fso = Nothing

End Sub
HTH
 
Upvote 0
If you just had c:\windows\windows.exe in one cell, could you have a formula that would return the file size, assuming of course that you have access to that directory?
 
Upvote 0
Wouldn't it be faster to just try it rather than ask a question and wait for an answer? {grin}
If you just had c:\windows\windows.exe in one cell, could you have a formula that would return the file size, assuming of course that you have access to that directory?
 
Upvote 0
Ok, so you have successfully answered your own question!
I asked:
If you just had c:\windows\windows.exe in one cell, could you have a formula that would return the file size, assuming of course that you have access to that directory?
It doesn't seem as though this particular macro will do exactly that, so therefore I have proven it's impossible to return the size of a file via a UDF?

Upgrade me to MVP status, I've reached the boundaries of Excel!
 
Last edited:
Upvote 0
You mean like this?

Code:
Public Function FileSize(FileName As String)
Dim FSO As Object
Set FSO = CreateObject("scripting.filesystemobject")
Set file = FSO.Getfile(FileName)
FileSize = file.Size
Set FSO = Nothing
End Function

Put the filename in the formula (including the drive and directory) and it will return the size in bytes
 
Upvote 0

Forum statistics

Threads
1,216,126
Messages
6,129,007
Members
449,480
Latest member
yesitisasport

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