VBScript: Drivespace

Here is a handy script that was written to gather the local hard drive storage, both avaliable and free, and appends to the file. This works great for places where you want to see a trend. I run this monthly, but you could run it weekly or whenever you want. It creates a sepereate file for each logical partition, and each entry is on a single line so it works great.

' Writes drive space to the Tools directory 
' 
' Written by Jason Olson 
' Based on work by Inge B. (ibr@lyse.net)

' Get the available logical drives and load them into an array from WMI

Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") 
Set colItems = objWMIService.ExecQuery _ 
("SELECT * FROM Win32_LogicalDisk Where DriveType = 3") 

' Calculate scale

function freespace(drive, name, space, space2, filesys2) 
pct = round(space/space2 , 2)*100
if space >= 1073741824 then 
	space = Round(space / 1073741824, 2) 
	units = "GB" 
	elseif space >= 1048576 then 
	space = Round(space / 1048576, 2) 
	units = "MB" 
	elseif space >= 1024 then 
	space = Round(space / 1024, 2) 
	units = "KB" 
	else 
	units = "B" 
End If 
if space2 >= 1073741824 then 
	space2= Round(space2/ 1073741824, 2) 
	units2 = "GB" 
	elseif space2>= 1048576 then 
	space2=Round(space2/1048576, 2) 
	units2 = "MB" 
	elseif space2>= 1024 then 
	space2=Round(space2/1024, 2) 
	units2 = "KB" 
	else 
	units2 = "B" 
End If 
freespace = Date() & Chr(9) & " " & space2 & " " & units2 & Chr(9) & space & " " & units & " free " & Chr(9) & "(" & pct & "%) " & Chr(9) & filesys2
End function 

' Strips out the colon from the drive name so we can use it as a filename
function drivename(name) 
	drivename = left(name,1)
End function 

' Find the tools directory, create it if missing

For Each objItem In colItems
 fldrx = objItem.DeviceID & "\Tools\"
 set fso = CreateObject("Scripting.FileSystemObject")
 if NOT fso.FolderExists(fldrx) Then
    fso.CreateFolder (fldrx)
    fldcx = True
   Exit For
   End If
 Exit For
Next 

' Write the data to the tools directory

For Each objItem In colItems
 fs = freespace(objItem.DeviceID, objItem.VolumeName, objItem.FreeSpace, objItem.Size, objItem.FileSystem)
 drvlet = drivename(objItem.DeviceID)
 fsl = fldrx & "drivespace_" & drvlet & ".txt"
 set fso = CreateObject("Scripting.FileSystemObject")
 set f = fso.OpenTextFile(fsl, "8", True)
 f.WriteLine(fs)
 f.Close()
Next