+ Post New Thread
Results 1 to 4 of 4
Coding Thread, VBS to list dirves, space, used etc in Coding and Web Development; I am trying to get the following script to list all physical drives and provide drive size, space used, space ...
  1. #1
    Guest

    Join Date
    Jun 2009
    Posts
    3,754
    Thank Post
    1,458
    Thanked 490 Times in 375 Posts
    Rep Power
    0

    VBS to list dirves, space, used etc

    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?

    Code:
    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:

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

  2. #2
    alunmjones's Avatar
    Join Date
    Jul 2009
    Location
    North Wales
    Posts
    178
    Thank Post
    8
    Thanked 10 Times in 10 Posts
    Rep Power
    12
    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

  3. Thanks to alunmjones from:

    Rawns (26th February 2010)

  4. #3

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,157
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    124
    change the line which builds your text string to this:

    Code:
    	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.

  5. Thanks to srochford from:

    Rawns (26th February 2010)

  6. #4
    Guest

    Join Date
    Jun 2009
    Posts
    3,754
    Thank Post
    1,458
    Thanked 490 Times in 375 Posts
    Rep Power
    0
    Thanks for your help guys. Working fine now.

SHARE:
+ Post New Thread

Similar Threads

  1. List computers in Domain in a text file with vbs
    By FatBoy in forum How do you do....it?
    Replies: 5
    Last Post: 15th February 2010, 12:29 PM
  2. Not Enough Space!!
    By silver in forum Educational Software
    Replies: 2
    Last Post: 12th February 2010, 01:57 PM
  3. Replies: 4
    Last Post: 17th November 2009, 08:58 AM
  4. AD Group in vbs
    By Arcath in forum Scripts
    Replies: 2
    Last Post: 23rd June 2009, 02:42 PM
  5. A little .vbs help please
    By ozzy in forum Scripts
    Replies: 9
    Last Post: 27th February 2009, 08:07 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •