How to check harddisk space

jack1234

Registered User.
Local time
Today, 12:02
Joined
Jun 2, 2007
Messages
16
Hi, i wish to check the user C drive remaining spaces using vba, how to do this?
 
Started writing a solution here, but ran into a strange problem. On my machine the last line in Sub FreeGigs() fails, which makes no sense. Curious if others can replicate this problem.
Apart from that Jack, here are a few ways to solve your problem...

Code:
Sub TestFreeGigs()
   FreeGigs "C"
End Sub

Sub FreeGigs(drivespec As String)
   Dim fsoEarly As New Scripting.FileSystemObject
   Dim fsoLate As Object
   Set fsoLate = CreateObject("Scripting.FileSystemObject")
   
   'works
   Debug.Print fsoEarly.Drives(drivespec).FreeSpace / 2 ^ 30
   Debug.Print fsoLate.Drives("C").FreeSpace / 2 ^ 30
   Debug.Print fsoLate.Drives(CStr(drivespec)).FreeSpace / 2 ^ 30
   'fails
   Debug.Print fsoLate.Drives(drivespec).FreeSpace / 2 ^ 30
   
End Sub
Mark
 
did you ever get this working?
 
did you ever get this working?

You have to set a reference to the Microsoft Scripting Runtime. The last line also failed on my Windows XP computer but the first three gave the exact value correctly stating the amount of free space on my hard drive.
 
The scripting runtime can be used with late binding, so no need for a reference at all. In the code given, you'd just comment out the variable declaration for fsoEarly.
 
Amazing! Hang around long enough and the question will be asked. I have been thinking of how to test for available space. In my case for a USB drive before moving data onto it. Thanks.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom