Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBS Harddrive Free Space

Status
Not open for further replies.

normie411

Technical User
Oct 25, 2008
1
AU
Hey i'm trying to teach myself to use VBS and im trying to make a script that will allow me to bring up all the harddrives, removable decices and cd drives as such. Then show how much free space is on each drive.
Any help will be greatly appreciated. This is the code that i have come up with so far. (not sure how good it is!


Option Explicit

' Drive type constants
Const Unknown = 0
Const Removable = 1 ' Removable medium
Const Fixed = 2 ' Fixed medium (hard disk)
Const Remote = 3 ' Network drive
Const CDROM = 4 ' CD-ROM
Const RAMDisk = 5 ' RAM disk

Dim Text, Free
Dim fso, oDrive, curDrive ' Object variables

Dim drtype(6)
drtype(0) = " Unknown "
drtype(1) = " Removable "
drtype(2) = " Fixed "
drtype(3) = " Remote "
drtype(4) = " CDROM "
drtype(5) = " RAMDisk "


Text = "Drives" & vbCrLf & vbCrLf 'drives then space space


' Create FileSystemObject object to access the file system.
Set fso = WScript.CreateObject("Scripting.FileSystemObject")

Set oDrive = fso.Drives ' Get Drives collection.

For Each curDrive In oDrive ' All drive objects
Text = Text & curDrive.DriveLetter & vbTab ' Drive letter
Text = Text & drtype(curDrive.DriveType) &vbTab &vbTab

Select Case curDrive.DriveType ' Identify drive type.
Case Removable ' Removable medium
If curDrive.IsReady Then
Text = Text & curDrive.VolumeName & "Free: " & FormatNumber(oDrive.FreeSpace/1024, 0) & " KB" & vbCrLf' Local drive


End If

Case CDROM ' CD-ROM
If curDrive.IsReady Then
Text = Text & curDrive.VolumeName & "Free: " & FormatNumber(oDrive.FreeSpace/1024, 0) & " KB" & vbCrLf' Local drive ' Local drive
End If

Case Remote
Text = Text & curDrive.ShareName & "Free: " & FormatNumber(oDrive.FreeSpace/1024, 0) & " KB" & vbCrLf' Local drive ' Network drive

Case Else ' Other medium
Text = Text & curDrive.VolumeName & "Free: " & FormatNumber(oDrive.Freespace/1024, 0) & " KB" & vbCrLf' Local drive ' Local drive
End Select
Text = Text & vbCrLf
Next

MsgBox Text & Free
 
' display free space on all disk drives
Const CONVERSION_FACTOR = 1048576
' strComputer = "computername"
strComputer = "."
Set objWMIService = GetObject("winmgmts://" & strComputer)
Set colLogicalDisk = objWMIService.InstancesOf("Win32_LogicalDisk")
wscript.Echo strComputer
For Each objLogicalDisk In colLogicalDisk
FreeMegaBytes = objLogicalDisk.FreeSpace / CONVERSION_FACTOR
Wscript.Echo objLogicalDisk.DeviceID & " " & Int((FreeMegaBytes)/1000) & " GB"
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top