How Can I Determine the Percentage of Free Space on a Drive?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I know how to determine the amount of free space on a drive, but how can I determine the percentage of free disk space on a drive?

— MS

SpacerHey, Scripting Guy! AnswerScript Center

Hey, JP. You’re right: determining the amount of free disk space on a drive is pretty easy. For example, this simple little script tells you the amount of free space (in bytes) for drive C on a computer:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set colDisks = objWMIService.ExecQuery _
    (“Select * from Win32_LogicalDisk Where DeviceID = ‘C:'”)
For Each objDisk in colDisks
    Wscript.Echo objDisk.FreeSpace
Next

And we have good news for you: determining the percentage of free disk space is almost as easy. That’s because the percentage of free disk space requires just a simple calculation: you take the available disk space and divide it by the total size of the drive. Suppose we had a really small drive, a 10-byte drive with 2 bytes of free disk space. To determine the percentage of free disk space, we’d divide 2 by 10 (free space by total size) and get a value of .20. In other words, 20% free space on our teeny-tiny drive.

It’s a simple equation, and we already have half of it: the FreeSpace property of the Win32_LogicalDisk class tells us the amount of free space. And although you might not have realized it, we also have the second half of the equation: the Size property of that same class tells us the size of the drive. All we need to do in our script is divide the FreeSpace by the Size, and we have our percentage:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set colDisks = objWMIService.ExecQuery _
    (“Select * from Win32_LogicalDisk Where DeviceID = ‘C:'”)
For Each objDisk in colDisks
    intFreeSpace = objDisk.FreeSpace
    intTotalSpace = objDisk.Size
    pctFreeSpace = intFreeSpace / intTotalSpace
    Wscript.Echo pctFreeSpace 
Next

Nifty, huh? The only problem with this script is that it reports back a decimal number like 0.22457268101427. Technically accurate, yes, but an answer that doesn’t really look much like a percentage. So let’s use VBScript’s FormatPercent function to get back a value like 22.45%:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set colDisks = objWMIService.ExecQuery _
    (“Select * from Win32_LogicalDisk Where DeviceID = ‘C:'”)
For Each objDisk in colDisks
    intFreeSpace = objDisk.FreeSpace
    intTotalSpace = objDisk.Size
    pctFreeSpace = intFreeSpace / intTotalSpace
    Wscript.Echo FormatPercent(pctFreeSpace)
Next

That’s much better.

Just for the heck of it, here’s another variation. By default, the Win32_LogicalDisk class looks at all disk drives on your computer, including floppy drives, removable Zip drives, etc. If you’d like to get back free space information on just your hard drives, modify your WQL Query so that it selects all drives with a DriveType of 3 (which, in the world of WMI, represents a hard drive). In other words:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set colDisks = objWMIService.ExecQuery _
    (“Select * from Win32_LogicalDisk Where DriveType = 3”)
For Each objDisk in colDisks
    intFreeSpace = objDisk.FreeSpace
    intTotalSpace = objDisk.Size
    pctFreeSpace = intFreeSpace / intTotalSpace
    Wscript.Echo objDisk.DeviceID, FormatPercent(pctFreeSpace)
Next

0 comments

Discussion is closed.

Feedback usabilla icon