Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

report folder size, # files and # folders 6

Status
Not open for further replies.

thebook

MIS
Jun 13, 2001
146
CA
I have a drive mapped to a home directory server. I want something in VBscript that will scan..show me the size of each folder...# of files per folder and # folders..per home directory.

Basically the same ino you get when you get properties of a directory in Windows Explorer.

Any help would be cool.

Thanks
 
Take a look at the Scripting.FileSystemObject ActiveX object that is designed for just this situation.

Code:
dim oFS, oFolder
set oFS = WScript.CreateObject("Scripting.FileSystemObject")
set oFolder = oFS.GetFolder("YourPathName")

ShowFolderDetails oFolder

sub ShowFolderDetails(oF)
dim F
    wscript.echo oF.Name & ":Size=" & oF.Size
    wscript.echo oF.Name & ":#Files=" & oF.Files.Count
    wscript.echo oF.Name & ":#Folders=" & oF.Subfolders.count
    wscript.echo oF.Name & ":Size=" & oF.Size
    for each F in oF.Subfolders
        ShowFolderDetails(F)
    next
end sub
This iterates over all the files and subfolders in a given starting folder. ShowFolderDetails is a recursive subroutine (one that calls itself) in order to keep the code size down.

Make sure that you call this from a command line using cscript not wscript as it's using wscript.echo!

Adapt the code to suit your needs.

Note that it may be pretty slow on a network drive however.



Bob Boffin
 
Looks good but I may need JUST a little more hand-holding. So my mapped drive is Z: and I also want to pipe it out to an excel file...or txt. The other thing is how do I change to the cscript from CMD line.?

sorry..thanks
 
More...

Within my Z: drive are all of the user directories...all I need are each user directory size (can it be in M instead of bytes?) and how many folders and files within as a numeric value total...instead of listing out all of the folders seperately with sizes.

Not sure if that makes any sense al all.
 
Open a copy of NotePad and copy and paste my example code into it.

Change "YourPathName" to "Z:\". Save this as GetZFolders.vbs. .vbs is one of the two common file extensions for VBScript files. The other is .wsf.

To run the script using cscript type:

cscript GetZFolders.vbs

at a command line prompt. You should see the output go shooting up the screen.

To get the output from this into a text file type:

cscript GetZFolders.vbs > ZFolderSizes.txt

This will redirect the screen output from the wscript.echo commands to the text file ZFolderSizes.txt. If you want to modify the format of the information just change the wscript.echo commands to output what you want.

Try it out first and then post some example output and tell me what you want changed.



Bob Boffin
 
Try this modification of the script on for size.

Code:
'==========================================================================
'
' NAME: <filename>
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 11/21/2004
'
' COMMENT: <comment>
'
'==========================================================================
Dim oFS, oFolder
Dim objexcel, r, lnameArray, lname, nameLength
set oFS = WScript.CreateObject("Scripting.FileSystemObject")
set oFolder = oFS.GetFolder("C:\Documents and Settings")

Set objExcel = createobject("Excel.application")   
objexcel.Workbooks.add
objexcel.Cells(1, 1).Value = "Folder Name"
objexcel.Cells(1, 2).Value = "Size (MB)"
objexcel.Cells(1, 3).Value = "# Files"
objexcel.Cells(1, 4).Value = "# Sub Folders"
objexcel.Visible = True
Wscript.Sleep 300
r=2


ShowFolderDetails oFolder, r

	
	
objexcel.ActiveWorkbook.SaveAs("FolderReport.xls")
objexcel.Quit
MsgBox "File Saved as FolderReport.xls"	
	
Function ShowFolderDetails(oF,r)
	Dim F
    objexcel.Cells(r, 1).Value = oF.Name
    objexcel.Cells(r, 2).Value = oF.Size /1024\1024
    objexcel.Cells(r, 3).Value =  oF.Files.Count
    objexcel.Cells(r, 4).Value =  oF.Subfolders.count
    r = r+1
    for each F in oF.Subfolders
        ShowFolderDetails F, r
    next
End Function

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Give this version of the above script a try.

This will save it all to Excel for you for analysis.

Code:
'==========================================================================
'
' NAME: getFolderSizes2XL.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 11/21/2004
'
' COMMENT: <comment>
'
'==========================================================================
Dim oFS, oFolder
Dim objexcel, r, lnameArray, lname, nameLength
set oFS = WScript.CreateObject("Scripting.FileSystemObject")
set oFolder = oFS.GetFolder("C:\Documents and Settings")

Set objExcel = createobject("Excel.application")   
objexcel.Workbooks.add
objexcel.Cells(1, 1).Value = "Folder Name"
objexcel.Cells(1, 2).Value = "Size (MB)"
objexcel.Cells(1, 3).Value = "# Files"
objexcel.Cells(1, 4).Value = "# Sub Folders"
objexcel.Visible = True
Wscript.Sleep 300
r=2


ShowFolderDetails oFolder, r

	
	
'	objexcel.ActiveWorkbook.SaveAs("LogonReport.xls")
'	objexcel.Quit
MsgBox "Done"	
	
Function ShowFolderDetails(oF,r)
	Dim F
    objexcel.Cells(r, 1).Value = oF.Name
    objexcel.Cells(r, 2).Value = oF.Size /1024\1024
    objexcel.Cells(r, 3).Value =  oF.Files.Count
    objexcel.Cells(r, 4).Value =  oF.Subfolders.count
    r = r+1
    for each F in oF.Subfolders
        ShowFolderDetails F, r
    next
End Function


I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top