Script help, vbscript to get directory sizes and output to csv

Posted on 2005-03-04
Medium Priority
Last Modified: 2008-01-09
I have found  scripts that are similar to what I want to do, but none work exactly as needed.  I just want the script to get a list of the of directories within a specified directory and there respective size then print that data to a csv/excel file

Script 1

' Title: Folder Size Checking Script
' Filename: fldrsize.vbs
' Author: Ariston Collander, Cox Computer Service
' Date: 03/2/2004

'###BEGIN Main####
On Error Resume Next

'==DIM Needed Variables==
Dim CurrentDate
Dim Company
Dim resultsfile
Dim res
Dim varFldr, varFldrName, varFldrSize

'==Configure User defined Variables==
CurrentDate = Now
Company = "Insert Company Name Here"
resultsfile = "C:\Documents and Settings\rsreese\Desktop\tmp\" & FormatDateTime(CurrentDate, vbLongDate) & ".htm "

'==System variables - Do Not Change==
const HKEY_LOCAL_MACHINE = &H80000002
ForWriting = 2
forReading = 1
forWriting = 2
forAppending = 8
CutOff = Date() - 1

'==Create the Folder List Arrays==
arrServer = Array("C:\Documents and Settings\rsreese\Desktop\tmp")

'==Create the file system object==
Set FSO = CreateObject("Scripting.FileSystemObject")

'==Create the basic htm file==
Set res = FSO.CreateTextFile(resultsfile, True)
res.Writeline ("<html>")
res.Writeline ("<h1>Folder Size Verification results on: <font color='#000080'>" & date() & "</font></h1>")
res.Writeline ("<table BORDER=0 width=50% cellspacing=0 cellpadding=0>")

'==Add header for Server==
insertHTML("Insert Server Name Here")

'==Run the chkfldrspace() subroutine on Server Array==

'==Script complete==
res.Writeline ("</html>")
'####END Main####

'####Check Folder Space Subroutine####
Sub chkfldrspace (arrObject)

'==Cycle through the array to run the subroutine==
Dim varFolder
For each varFldr in arrObject
Set varFolder = FSO.GetFolder(varFldr)
varFldrSize = Round(varFolder.size/1048576, 2)

'==Write the results to the HTM file==
res.Writeline ("<tr>")
res.Writeline ("<td><b><font face=Verdana color=#000080 size=2> " & UCase(varFolder.Name) & "</font></b></TD>")
res.Writeline ("<td><b><font face=Verdana color=#000080 size=2> " & varFldrSize & " megabytes used</font></b></TD>")
res.Writeline ("</tr>")

End Sub
'####End Check Folder Space Subroutine####

'####Insert HTML Header Subroutine####
Sub insertHTML(varSrvName)

res.Writeline ("<tr>")
res.Writeline ("<th colspan=2 bgcolor=#000080")
res.Writeline ("<p align=left>")
res.Writeline ("<b><font face=Verdana size=4 color=#FFFFFF>Folder size information on " & varSrvName & " </font></b></p>")
res.Writeline ("</th>")
res.Writeline ("</tr>")
res.Writeline ("<tr>")
res.Writeline ("<td bgcolor=#C0C0C0><b><font face=Verdana color=#FFFFFF size=3><u>Folder Name</u></font></b></TD>")
res.Writeline ("<td bgcolor=#C0C0C0><b><font face=Verdana color=#FFFFFF size=3><u>Folder Size</u></font></b></TD>")
res.Writeline ("</tr>")

End Sub
'####End Insert HTML Header Subroutine####
Question by:adamshields
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment

Accepted Solution

harnal earned 1600 total points
ID: 13462889
Here you go.  Just change the paths that I've noted to the locations you want.

Option Explicit
  'Edit your folder path here.  The
  'script will enumerate all sub directories
  'under this folder.
  Dim fPath: fPath="c:\development\"  
  'This is the path where the CSV file will be created.
  'Change this to suit your needs.
  dim fileOut: fileOut="c:\Folders.csv"
  function getFolderMetrics()
    Dim fso
    Set fso=CreateObject("scripting.filesystemobject")
    Dim fldrObj, oFolders, f
    Set fldrObj = fso.GetFolder(fPath)
    Set oFolders=fldrObj.SubFolders
    dim fStream
    set fStream=fso.createTextFile(fileOut, true)
      For Each f In oFolders      
        fStream.writeline f.name & "," & f.size            
    set fso=nothing
  end function

'----------------------END CUT-----------------------------

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question