Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for freespace.vbs

(view source code of freespace.vbs as plain text)

  1. ' FreeSpace.vbs,  Version 1.00
  2. ' Display free disk space for all local drives.
  3. '
  4. ' Written by Rob van der Woude
  5. ' http://www.robvanderwoude.com
  6.  
  7.  
  8. ' Check command line parameters
  9. Select Case WScript.Arguments.Count
  10. 	Case 0
  11. 		' Default if none specified is local computer (".")
  12. 		Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
  13. 		Set colItems = objWMIService.ExecQuery( "Select * from Win32_ComputerSystem", , 48 )
  14. 		For Each objItem in colItems
  15. 			strComputer = objItem.Name
  16. 		Next
  17. 	Case 1
  18. 		' Command line parameter can either be a computer name
  19. 		' or "/?" to request online help
  20. 		strComputer = Wscript.Arguments(0)
  21. 		if InStr( strComputer, "?" ) > 0 Then Syntax
  22. 	Case Else
  23. 		' Maximum is 1 command line parameter
  24. 		Syntax
  25. End Select
  26.  
  27. Display( strComputer )
  28. WScript.Quit(0)
  29.  
  30.  
  31. Function Display( strComputer )
  32. 	strMsg = vbCrLf & "Name:" & vbTab & "Drive:" & vbTab & "Size:" & _
  33. 	         vbTab & "Free:" & vbTab & "% Free:" & vbCrLf & "=====" & _
  34. 	         vbTab & "======" & vbTab & "=====" & vbTab & "=====" & _
  35. 	         vbTab & "=======" & vbCrLf
  36. 	On Error Resume Next
  37. 	Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
  38. 	If Err.Number Then
  39. 		WScript.Echo vbCrLf & "Error # " & CStr( Err.Number ) & _
  40. 		             " " & Err.Description
  41. 		Err.Clear
  42. 		Syntax
  43. 	End If
  44. 	On Error GoTo 0
  45. 	' Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk where MediaType=12",,48)
  46. 	Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk where DriveType=3",,48)
  47. 	For Each objItem in colItems
  48. 		strMsg = strMsg & strComputer & vbTab & _
  49. 		         objItem.Name & vbTab & _
  50. 		         CStr( Int( 0.5 + ( objItem.Size / 1073741824 ) ) ) & _
  51. 		         vbTab & _
  52. 		         CStr( Int( 0.5 + ( objItem.FreeSpace / 1073741824 ) ) ) & _
  53. 		         vbTab & _
  54. 		         CStr( Int( 0.5 + ( 100 * objItem.FreeSpace / objItem.Size) ) ) & _
  55. 		         vbCrLf
  56. 	Next
  57. 	WScript.Echo strMsg
  58. End Function
  59.  
  60.  
  61. Sub Syntax
  62. 	strMsg = vbCrLf & "FreeSpace.vbs,  Version 1.00" & vbCrLf & _
  63. 	         "Display free disk space for all local drives." & vbCrLf & _
  64. 	         vbCrLf & _
  65. 	         "Usage:  CSCRIPT  FREESPACE.VBS  [ computer_name ]" & _
  66. 	         vbCrLf & vbCrLf & _
  67. 	         "Where:  " & Chr(34) & "computer_name" & Chr(34) & _
  68. 	         " is the name of a WMI enabled computer on the network" & _
  69. 	         vbCrLf & vbCrLf & _
  70. 	         "Written by Rob van der Woude" & vbCrLf & _
  71. 	         "http://www.robvanderwoude.com" & vbCrLf
  72. 	WScript.Echo strMsg
  73. 	WScript.Quit(1)
  74. End Sub
  75.  

page last uploaded: 2017-08-21, 14:26