Jump to content

VBS to list dirves, space, used etc


Recommended Posts

Posted

I am trying to get the following script to list all physical drives and provide drive size, space used, space free and the % of free space. It seems to be working up to a point but is only listing the E drive. Why does it not list any others?

 

OPTION EXPLICIT

CONST strComputer = "."
CONST strReport = "c:\diskspace.txt"


DIM objWMIService, objItem, colItems
DIM strDriveType, strDiskSize, txt

SET objWMIService = GETOBJECT("winmgmts:\\" & strComputer & "\root\cimv2")
SET colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk WHERE DriveType=3")
' bar = "-----------------------------------------------------------------------------------------------" & vbcrlf

FOR EACH objItem in colItems

DIM pctFreeSpace,strFreeSpace,strusedSpace

pctFreeSpace = INT((objItem.FreeSpace / objItem.Size) * 1000)/10
strDiskSize = Int(objItem.Size /1073741824) & "Gb"
strFreeSpace = Int(objItem.FreeSpace /1073741824) & "Gb"
strUsedSpace = Int((objItem.Size-objItem.FreeSpace)/1073741824) & "Gb"
txt = "Drive" & vbtab & objItem.Name & vbcrlf & "Size" & vbtab & strDiskSize & vbcrlf & "Used" & vbtab & strUsedSpace & vbcrlf & "Free" & vbtab & strFreeSpace & vbcrlf & "% Free" & vbtab & pctFreeSpace & vbcrlf

NEXT

writeTextFile txt, strReport

' Procedure to write output to a text file
PRIVATE SUB writeTextFile(BYVAL txt,BYVAL strTextFilePath)
DIM objFSO,objTextFile

SET objFSO = CREATEOBJECT("Scripting.FileSystemObject")

SET objTextFile = objFSO.CreateTextFile(strTextFilePath)

'objTextFile.Write(bar)
objTextFile.Write(txt)
'objTextFile.Write(bar)

objTextFile.Close
SET objTextFile = NOTHING
END SUB

 

The output looks like this:

 

Drive	E:
Size	298Gb
Used	298Gb
Free	0Gb
% Free	0

Posted
looks like your over writing the data each time to the file, and the E drive is the last to run so its the only one thats shows up, someone else could double check, looks like whats happening tho
  • Thanks 1
Posted

change the line which builds your text string to this:

 

txt = txt & "Drive" & vbtab & objItem.Name & vbcrlf & "Size" & vbtab & strDiskSize & vbcrlf & "Used" & vbtab & strUsedSpace & vbcrlf & "Free" & vbtab & strFreeSpace & vbcrlf & "% Free" & vbtab & pctFreeSpace & vbcrlf

 

You're only writing txt at the end (which is fine) but you're throwing away "txt" after each drive so you only get the last disc.

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...