View Full Version : How to check harddisk space


jack1234
07-15-2007, 02:38 AM
Hi, i wish to check the user C drive remaining spaces using vba, how to do this?

lagbolt
07-15-2007, 07:24 AM
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...

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

Spyhunter
04-27-2010, 12:43 AM
did you ever get this working?

namliam
04-27-2010, 01:32 AM
The given code works like a charm

ghudson
04-27-2010, 06:21 AM
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.

dfenton
04-27-2010, 03:49 PM
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.

Steve R.
04-28-2010, 05:06 AM
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.