Submitted By: Charles Rutledge

Displays disk usage (the used, free and total space) for all fixed disks on a computer.

Visual Basic
Edit|Remove
' diskusage.vbs
' VBScript that displays the used, free and total space for all fixed disks.
'
' Written by Charles Rutledge
' Copywright (c) 2006 Centauri Computer Works, Inc.

' Funtion to display the disk space information with nice formating.
Sub DpySpaceInfo(ByVal infotype, ByVal drvSpace, ByVal percentage)

	textline = Space(12 - Len(infotype)) & infotype & Space(17 - Len(drvSpace)) & drvSpace
	'If percentage <> "" Then textline = textline & Space(33 - Len(textline)) & percentage
	If percentage <> "" Then textline = textline & Space(11 - Len(percentage)) & percentage
	WScript.Echo textline

End Sub

' Function to calculate the used and free space on the disk drive.
Sub GetDriveSpace(ByRef drive)

	totalSpace = drive.TotalSize / 1024
	freeSpace = drive.AvailableSpace / 1024
	percentFree = freeSpace / totalSpace
	percentUsed = 1 - percentFree
	
	dpyUsedSpace = FormatNumber(totalSpace - freeSpace, 0, vbTrue, vbFalse, vbTrue) & " KB"
	dpyFreeSpace = FormatNumber(freeSpace, 0, vbTrue, vbFalse, vbTrue) & " KB"
	dpyTotalSpace = FormatNumber(totalSpace, 0, vbTrue, vbFalse, vbTrue) & " KB"
	dpyPercentUsed = "(" & FormatPercent(percentUsed, 2, vbTrue, vbFalse, vbTrue) & ")"
	dpyPercentFree = "(" & FormatPercent(percentFree, 2, vbTrue, vbFalse, vbTrue) & ")"

	WScript.Echo
	WScript.Echo "----------------------------------------"
	WScript.Echo "SPACE INFO FOR DRIVE " & drive.DriveLetter & ":"
	WScript.Echo "----------------------------------------"
	DpySpaceInfo "Used Space:", dpyUsedSpace, dpyPercentUsed
	DpySpaceInfo "Free Space:", dpyFreeSpace, dpyPercentFree
	DpySpaceInfo "Total Space:", dpyTotalSpace, ""
	
End Sub

Set oFileSystem = CreateObject("Scripting.FileSystemObject")
Set drivesList = oFileSystem.Drives

' Iterage through all drives ignoring all but fixed drives.
For Each d In drivesList
	If d.DriveType = 2 Then GetDriveSpace d
Next