vb Drive information

Sub GetDrives()
	'opens txt file and creates list of drives
	Dim strReturn, strLine
	Dim f, fs
	Dim aLine, strName, strPath

	Set fs=Server.CreateObject("Scripting.FileSystemObject")
	Set f=fs.OpenTextFile(Server.MapPath("drives.txt"), 1)
	
	Do While f.AtEndOfStream = false
		strLine = f.ReadLine
		
		If Len(strLine)>1 Then 'allow for blanks
			If Left(strLine,1) <> "#" Then 'allow for comments
				aLine = Split(strLine,"|")
				strName = aLine(0)
				strPath = aLine(1)

				Call ShowDriveInfo(strName,strPath)
			End If
		End If
	Loop
	
	f.Close

	Set f=Nothing
	Set fs=Nothing
End Sub

Sub ShowDriveInfo(drvName,drvPath)
	On Error Resume Next
	Dim fso, drv, s
	Dim intTotal, intFree, intPercent, intUsed, strClass
	
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set drv = fso.GetDrive(fso.GetDriveName(drvPath))
	
	intTotal = ConvertBytes(drv.TotalSize,"Gb")
	intFree = ConvertBytes(drv.FreeSpace,"Gb")
	intUsed = ConvertBytes((drv.TotalSize - drv.FreeSpace),"Gb")
	
	'all of the below are available
	'drv.VolumeName
	'drv.DriveLetter
	'GetDriveType(drv.DriveType)
	'drv.FileSystem
	'drv.IsReady
	'drv.Path
	'drv.RootFolder
	'drv.SerialNumber
	'ConvertBytes(drv.TotalSize,"Gb")
	'ConvertBytes(drv.FreeSpace,"Gb")
	'ConvertBytes(drv.AvailableSpace,"Gb")
	
	Set fso = Nothing
	Set drv = Nothing
	
	intPercent = FormatNumber(((intFree/intTotal)*100),0)
	strClass = ""
	If intPercent < 12 Then strClass = "class=""low"""
	
	Response.Write("<tr " & strClass & ">")
	Response.Write("<td>" & drvName & "</td>")
	Response.Write("<td align=""right"">" & intTotal & "</td>")
	Response.Write("<td align=""right"">" & intFree & "</td>")
	Response.Write("<td align=""right"">" & intUsed & "</td>")
	Response.Write("<td align=""right"">" & intPercent & "</td>")
	Response.Write("</tr>")
End Sub
	
Function GetDriveType(intDriveType)
	Dim strReturn
	
	Select Case CInt(intDriveType)
		Case 1
			strReturn = "removable"
		Case 2
			strReturn = "fixed"
		Case 3
			strReturn = "network"
		Case 4
			strReturn = "CD-ROM"
		Case 5
			strReturn = "RAM disk"
		Case Else
			strReturn = "unknown" '0	
	End Select

	GetDriveType = strReturn
End Function

Function ConvertBytes(intBytes,strTo)	
	Select Case lcase(strTo)
		Case "kb"
			intBytes = FormatNumber(intBytes/1024, 2)
		Case "mb"
			intBytes = FormatNumber((intBytes/1024)/1024, 2)
		Case "gb"
			intBytes = FormatNumber(((intBytes/1024)/1024)/1024, 2)
		Case "tb"
			intBytes = FormatNumber((((intBytes/1024)/1024)/1024)/1024, 2)
		Case Else
			intBytes = intBytes
	End Select
	
	ConvertBytes = CStr(intBytes)
End Function

'.txt file looks like this (without ASP comments):
'#Enter all drives here using UNC path, e.g. \\server\share\
'#Syntax is Name|Path
'MyServer C:\|\\myserver\c$\
'MyServer D:\|\\myserver\d$\
Displays drive information for a specified list of drives.

Updated: Saturday 9th October 2010, 02:50pm

There are 0 comments

Leave a comment of your own

Comments are currently closed.