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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display number of files/subfolders under a root folder 1

Status
Not open for further replies.

alepore

MIS
Jun 25, 2001
27
US
I am new at scripting and would like to know how I can get the number of files and subfolders under a root folder. I guess the equivalant of right clicking on a folder --> Properties and the information listed next to "Contains:". I am also displaying the folder size. Here is what I have so far.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objCHARFolder = objFSO.GetFolder("c:\winnt")
SizeGB = objCHARFolder.Size / 1073741824
WScript.echo SizeGB & " GB"

Thanks in advance
 
Give this a try. It list the folder\subfolder and its file count.

Hope this is what you are looking for, if not let, me know:

Dim goFso, gcsFolder, gcsSubFolder, lcsSubFolder
Set goFso = CreateObject("Scripting.FileSystemObject")
set gcsFolder = goFso.GetFolder("e:\programming\")
Call CountFiles(gcsFolder)
Set gcsSubFolder = gcsFolder.SubFolders
For Each SubFolder In gcsSubFolder
Call countFiles(SubFolder)
next


Sub CountFiles(folder)
Dim lcsFolder, lcsSubFolder
set lcsFolder = goFso.GetFolder(folder)
WScript.Echo(Folder & " : " &lcsFolder.files.count)
set lcsSubFolder = lcsFolder.SubFolders
For Each sSubFolder In lcsSubFolder
Call CountFiles(sSubFolder)
Next
Set lcsSubFolder = Nothing
Set lcsFolder = nothing
End Sub

Set gcsSubFolder = nothing
set gcsFolder = nothing
Set goFso = nothing
 
Actually I was looking for an output that would tell me something like "C:\Winnt has 600 folders and 12,000 files". I appreciate the tip though. I will try and tweak what you have given me and attempt to solve it on my own.....unless you have a few more minutes to help me out. Thanks.
 
what you can do then is instead of outputing each folder name and file count, just add it up and output the numbers only. If you need help, let me know.
 
Here you go. I got it! Let me know if this works out better for you

Dim goFso, gcsFolder, gcsSubFolder, lcsSubFolder
Set goFso = CreateObject("Scripting.FileSystemObject")
set gcsFolder = goFso.GetFolder("E:\programming\vbs")
Call CountFiles(gcsFolder)
Set gcsSubFolder = gcsFolder.SubFolders
Call countFiles(SubFolder)

Dim folCnt, filCnt
Sub CountFiles(folder)
Dim lcsFolder, lcsSubFolder
set lcsFolder = goFso.GetFolder(folder)
filCnt = filCnt + lcsFolder.files.count
set lcsSubFolder = lcsFolder.SubFolders
folCnt = folCnt + lcsSubFolder.count
For Each sSubFolder In lcsSubFolder
Call CountFiles(sSubFolder)
Next
Set lcsSubFolder = Nothing
Set lcsFolder = nothing
End Sub
WScript.Echo("Folder Count: " & folCnt)
WScript.Echo("Filer Count: " & filCnt)

Set gcsSubFolder = nothing
set gcsFolder = nothing
Set goFso = nothing

 
That works great, thank you. I only have one problem. The folder I am checking is around 30 GB so I am receiving an "Out of memory" error with code 800A0007. I will search the web for some answers. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top