Link to home
Start Free TrialLog in
Avatar of changjia
changjia

asked on

VB script to list disk name and size and free space

Hi All,

I have a text file that has about 200 server names. I need a vbscript to read in the text file and generate a report on the physical disk size and free space for each server.

Please help!

Thanks
Avatar of RobSampson
RobSampson
Flag of Australia image

Hi there,

Have a file called computers.txt in the same folder as this script, and it will report the space of your C, D, E, and F drives.  It was easier to hard code the drive letters.

Regards,

Rob.
strInputFile = "computers.txt"
strOutputFile = "hard_disk_space.csv"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1

Const adVarChar = 200
Const MaxCharacters = 255

Dim DataList
Set DataList = CreateObject("ADOR.Recordset")
DataList.Fields.Append "Server", adVarChar, MaxCharacters
DataList.Fields.Append "C Free Space", adVarChar, MaxCharacters
DataList.Fields.Append "C Percent Free", adVarChar, MaxCharacters
DataList.Fields.Append "D Free Space", adVarChar, MaxCharacters
DataList.Fields.Append "D Percent Free", adVarChar, MaxCharacters
DataList.Fields.Append "E Free Space", adVarChar, MaxCharacters
DataList.Fields.Append "E Percent Free", adVarChar, MaxCharacters
DataList.Fields.Append "F Free Space", adVarChar, MaxCharacters
DataList.Fields.Append "F Percent Free", adVarChar, MaxCharacters
DataList.Open

Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading)
While Not objInputFile.AtEndOfStream
	strComputer = objInputFile.ReadLine
	Get_Free_Space_Details(strComputer)
Wend

Set objOutputFile = objFSO.CreateTextFile(strOutputFile, True)
objOutputFile.WriteLine """SERVER"",""C FREE SPACE"",""C PERCENT FREE"",""D FREE SPACE"",""D PERCENT FREE"",""E FREE SPACE"",""E PERCENT FREE"",""F FREE SPACE"",""F PERCENT FREE"""
DataList.MoveFirst
While Not DataList.EOF
	objOutputFile.WriteLine """" & DataList("Server") & """,""" & DataList("C Free Space") & """,""" & DataList("C Percent Free") & """,""" & DataList("D Free Space") & """,""" & DataList("D Percent Free") & """,""" & DataList("E Free Space") & """,""" & DataList("E Percent Free") & """,""" & DataList("F Free Space") & """,""" & DataList("F Percent Free") & """"
	DataList.MoveNext
Wend
DataList.Close
objOutputFile.Close

MsgBox "Done. Please see " & strOutputFile
'==============

Sub Get_Free_Space_Details(strComputer)

	DataList.AddNew
	DataList("Server") = strComputer
	If Ping(strComputer) = True Then 
		On Error Resume Next
		Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
		If Err.Number = 0 Then
			Err.Clear
			On Error GoTo 0
			Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType = 3")
			For Each objDisk In colDisks
				strDrive = objDisk.DeviceID
				intFreeSpace = objDisk.FreeSpace
				intTotalSpace = objDisk.Size
				pctFreeSpace = intFreeSpace / intTotalSpace
				Select Case UCase(strDrive)
					Case "C:"
						DataList("C Free Space") = Round(intFreeSpace / 1024 / 1024 / 1024, 2) & " GB"
						DataList("C Percent Free") = FormatPercent(pctFreeSpace)
					Case "D:"
						DataList("D Free Space") = Round(intFreeSpace / 1024 / 1024 / 1024, 2) & " GB"
						DataList("D Percent Free") = FormatPercent(pctFreeSpace)
					Case "E:"
						DataList("E Free Space") = Round(intFreeSpace / 1024 / 1024 / 1024, 2) & " GB"
						DataList("E Percent Free") = FormatPercent(pctFreeSpace)
					Case "F:"
						DataList("F Free Space") = Round(intFreeSpace / 1024 / 1024 / 1024, 2) & " GB"
						DataList("F Percent Free") = FormatPercent(pctFreeSpace)
				End Select
			Next
			
			Set objDisk = Nothing
			Set colDisks = Nothing
			Set objWMIService = Nothing
		Else
			DataList("C Free Space") = "WMI ERROR"
			DataList("C Percent Free") = "WMI ERROR"
			DataList("D Free Space") = "WMI ERROR"
			DataList("D Percent Free") = "WMI ERROR"
			DataList("E Free Space") = "WMI ERROR"
			DataList("E Percent Free") = "WMI ERROR"
			DataList("F Free Space") = "WMI ERROR"
			DataList("F Percent Free") = "WMI ERROR"
		End If
	Else
		DataList("C Free Space") = "OFFLINE"
		DataList("C Percent Free") = "OFFLINE"
		DataList("D Free Space") = "OFFLINE"
		DataList("D Percent Free") = "OFFLINE"
		DataList("E Free Space") = "OFFLINE"
		DataList("E Percent Free") = "OFFLINE"
		DataList("F Free Space") = "OFFLINE"
		DataList("F Percent Free") = "OFFLINE"
	End If
	DataList.Update
End Sub

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

Avatar of Bill Prew
Bill Prew

==>RobSampson

Interesting read...

~bp
Thanks Bill....I'm sure it's not the most efficient way to do it, and it's not dynamic in terms of getting the drives no matter what the letter is, but this was the easiest way I could think of to write it up....

Rob.
Ha, and I hope that's "interesting" in a good way ;-)
Yes, I appreciated that you didn't try to make it a work of art, but I found it interesting to follow along and see how you attacked it and trapped certain errors, etc.  I might have done it a little differently (not better or worse, just different), but I always enjoy following other solid programmers code.  You clearly have skills!

~bp
Avatar of changjia

ASKER

Great script, but the output is missing the capacity of the disk. It only shows free space and % of the space.

Thanks
OK, that's easy fixed.

Rob.
strInputFile = "computers.txt"
strOutputFile = "hard_disk_space.csv"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1

Const adVarChar = 200
Const MaxCharacters = 255

Dim DataList
Set DataList = CreateObject("ADOR.Recordset")
DataList.Fields.Append "Server", adVarChar, MaxCharacters
DataList.Fields.Append "C Size", adVarChar, MaxCharacters
DataList.Fields.Append "C Free Space", adVarChar, MaxCharacters
DataList.Fields.Append "C Percent Free", adVarChar, MaxCharacters
DataList.Fields.Append "D Size", adVarChar, MaxCharacters
DataList.Fields.Append "D Free Space", adVarChar, MaxCharacters
DataList.Fields.Append "D Percent Free", adVarChar, MaxCharacters
DataList.Fields.Append "E Size", adVarChar, MaxCharacters
DataList.Fields.Append "E Free Space", adVarChar, MaxCharacters
DataList.Fields.Append "E Percent Free", adVarChar, MaxCharacters
DataList.Fields.Append "F Size", adVarChar, MaxCharacters
DataList.Fields.Append "F Free Space", adVarChar, MaxCharacters
DataList.Fields.Append "F Percent Free", adVarChar, MaxCharacters
DataList.Open

Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading)
While Not objInputFile.AtEndOfStream
	strComputer = objInputFile.ReadLine
	Get_Free_Space_Details(strComputer)
Wend

Set objOutputFile = objFSO.CreateTextFile(strOutputFile, True)
objOutputFile.WriteLine """SERVER"",""C SIZE"",""C FREE SPACE"",""C PERCENT FREE"",""D SIZE"",""D FREE SPACE"",""D PERCENT FREE"",""E SIZE"",""E FREE SPACE"",""E PERCENT FREE"",""F SIZE"",""F FREE SPACE"",""F PERCENT FREE"""
DataList.MoveFirst
While Not DataList.EOF
	objOutputFile.WriteLine """" & DataList("Server") & """,""" & DataList("C Size") & """,""" & DataList("C Free Space") & """,""" & DataList("C Percent Free") & """,""" & DataList("D Size") & """,""" & DataList("D Free Space") & """,""" & DataList("D Percent Free") & """,""" & DataList("E Size") & """,""" & DataList("E Free Space") & """,""" & DataList("E Percent Free") & """,""" & DataList("F Size") & """,""" & DataList("F Free Space") & """,""" & DataList("F Percent Free") & """"
	DataList.MoveNext
Wend
DataList.Close
objOutputFile.Close

MsgBox "Done. Please see " & strOutputFile
'==============

Sub Get_Free_Space_Details(strComputer)

	DataList.AddNew
	DataList("Server") = strComputer
	If Ping(strComputer) = True Then 
		On Error Resume Next
		Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
		If Err.Number = 0 Then
			Err.Clear
			On Error GoTo 0
			Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType = 3")
			For Each objDisk In colDisks
				strDrive = objDisk.DeviceID
				intFreeSpace = objDisk.FreeSpace
				intTotalSpace = objDisk.Size
				pctFreeSpace = intFreeSpace / intTotalSpace
				Select Case UCase(strDrive)
					Case "C:"
						DataList("C Size") = Round(intTotalSpace / 1024 / 1024 / 1024, 2) & " GB"
						DataList("C Free Space") = Round(intFreeSpace / 1024 / 1024 / 1024, 2) & " GB"
						DataList("C Percent Free") = FormatPercent(pctFreeSpace)
					Case "D:"
						DataList("D Size") = Round(intTotalSpace / 1024 / 1024 / 1024, 2) & " GB"
						DataList("D Free Space") = Round(intFreeSpace / 1024 / 1024 / 1024, 2) & " GB"
						DataList("D Percent Free") = FormatPercent(pctFreeSpace)
					Case "E:"
						DataList("E Size") = Round(intTotalSpace / 1024 / 1024 / 1024, 2) & " GB"
						DataList("E Free Space") = Round(intFreeSpace / 1024 / 1024 / 1024, 2) & " GB"
						DataList("E Percent Free") = FormatPercent(pctFreeSpace)
					Case "F:"
						DataList("F Size") = Round(intTotalSpace / 1024 / 1024 / 1024, 2) & " GB"
						DataList("F Free Space") = Round(intFreeSpace / 1024 / 1024 / 1024, 2) & " GB"
						DataList("F Percent Free") = FormatPercent(pctFreeSpace)
				End Select
			Next
			
			Set objDisk = Nothing
			Set colDisks = Nothing
			Set objWMIService = Nothing
		Else
			DataList("C Free Space") = "WMI ERROR"
			DataList("C Percent Free") = "WMI ERROR"
			DataList("D Free Space") = "WMI ERROR"
			DataList("D Percent Free") = "WMI ERROR"
			DataList("E Free Space") = "WMI ERROR"
			DataList("E Percent Free") = "WMI ERROR"
			DataList("F Free Space") = "WMI ERROR"
			DataList("F Percent Free") = "WMI ERROR"
		End If
	Else
		DataList("C Free Space") = "OFFLINE"
		DataList("C Percent Free") = "OFFLINE"
		DataList("D Free Space") = "OFFLINE"
		DataList("D Percent Free") = "OFFLINE"
		DataList("E Free Space") = "OFFLINE"
		DataList("E Percent Free") = "OFFLINE"
		DataList("F Free Space") = "OFFLINE"
		DataList("F Percent Free") = "OFFLINE"
	End If
	DataList.Update
End Sub

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

Man! amazingly good! Only one question, wut if one of the server has G: and H: drive?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you are so talented! well worth the point!
Thanks for the grade.  The new version is neater, and easier to modify too.

Regards,

Rob.
I would like to salute you for this awesome script.
Thanks :-)
When I run this against a list of servers, I get this error:

Script: C:\disksize.vbs
Line:    66
Char:   6
Error:   Invalid use of Null: 'Round'
Code:   800A005E
Source: Microsoft VBScript runtime error

That's odd.  That would suggest a problem retrieving the full disk size. This will check for any error with that.

Regards,

Rob.
strInputFile = "computers.txt"
strOutputFile = "hard_disk_space.csv"
Dim arrDrives
arrDrives = Array("C","D","E","F")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1

Const adVarChar = 200
Const MaxCharacters = 255


Dim DataList
Set DataList = CreateObject("ADOR.Recordset")
DataList.Fields.Append "Server", adVarChar, MaxCharacters
For Each strDrive In arrDrives
	DataList.Fields.Append strDrive & " Size", adVarChar, MaxCharacters
	DataList.Fields.Append strDrive & " Free Space", adVarChar, MaxCharacters
	DataList.Fields.Append strDrive & " Percent Free", adVarChar, MaxCharacters
Next
DataList.Open

Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading)
While Not objInputFile.AtEndOfStream
	strComputer = objInputFile.ReadLine
	Get_Free_Space_Details(strComputer)
Wend

Set objOutputFile = objFSO.CreateTextFile(strOutputFile, True)
strHeader = """SERVER"""
For Each strDrive In arrDrives
	strHeader = strHeader & ",""" & strDrive & " SIZE"",""" & strDrive & " FREE SPACE"",""" & strDrive & " PERCENT FREE"""
Next
objOutputFile.WriteLine strHeader
DataList.MoveFirst
While Not DataList.EOF
	strLine = """" & DataList("Server") & """"
	For Each strDrive In arrDrives
		strLine = strLine & ",""" & DataList(strDrive & " Size") & """,""" & DataList(strDrive & " Free Space") & """,""" & DataList(strDrive & " Percent Free") & """"
	Next
	objOutputFile.WriteLine strLine
	DataList.MoveNext
Wend
DataList.Close
objOutputFile.Close

MsgBox "Done. Please see " & strOutputFile
'==============

Sub Get_Free_Space_Details(strComputer)

	DataList.AddNew
	DataList("Server") = strComputer
	If Ping(strComputer) = True Then 
		On Error Resume Next
		Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
		If Err.Number = 0 Then
			Err.Clear
			On Error GoTo 0
			For Each strDrive In arrDrives
				Set colDisks = objWMIService.ExecQuery("Select FreeSpace,Size From Win32_LogicalDisk Where DriveType = 3 And DeviceID = '" & strDrive & ":'")
				For Each objDisk In colDisks
					intFreeSpace = objDisk.FreeSpace
					intTotalSpace = objDisk.Size
					pctFreeSpace = intFreeSpace / intTotalSpace
					On Error Resume Next
					DataList(strDrive & " Size") = Round(intTotalSpace / 1024 / 1024 / 1024, 2) & " GB"
					If Err.Number <> 0 Then
						DataList(strDrive & " Size") = intTotalSize & " Bytes"
						Err.Clear
					End If
					DataList(strDrive & " Free Space") = Round(intFreeSpace / 1024 / 1024 / 1024, 2) & " GB"
					If Err.Number <> 0 Then
						DataList(strDrive & " Free Space") = intFreeSpace & " Bytes"
						Err.Clear
					End If
					DataList(strDrive & " Percent Free") = FormatPercent(pctFreeSpace)
					On Error GoTo 0
				Next
			Next			
			Set objDisk = Nothing
			Set colDisks = Nothing
			Set objWMIService = Nothing
		Else
			For Each strDrive In arrDrives
				DataList(strDrive & " Size") = "WMI ERROR"
				DataList(strDrive & " Free Space") = "WMI ERROR"
				DataList(strDrive & " Percent Free") = "WMI ERROR"
			Next
		End If
	Else
		For Each strDrive In arrDrives
			DataList(strDrive & " Size") = "OFFLINE"
			DataList(strDrive & " Free Space") = "OFFLINE"
			DataList(strDrive & " Percent Free") = "OFFLINE"
		Next
	End If
	DataList.Update
End Sub

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window