WSH question, file sizes

I'm trying to get the sizes of a few file types in a particular folder (My Documents). So far I've been successful in obtaining the My Documents size, and even wrote a script to parse the results of all of our computers on the network and get the average/total for the whole company.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H5&)  // <--My Documents namespace is &H5&
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path
Set objFolder = objFSO.GetFolder(strPath)
Wscript.Echo objFolder.length
Dim MyDocSiz
MyDocSiz = (objFolder.Size / 1000000)
MyDocSiz = Round(MyDocSiz, 0)
Wscript.Echo MyDocSiz

The only way to get this through the UI that I know of is to do a search, select all of the results, and right click and hit properties. I think that this is embedded somewhere in the shell because when you do this, it says "All files of type PST". But it may not be. Help.

The file types I'm trying to get right now, not that it really matters, are PST, OST, GHO, GHS, VMDK, BKF, ISO, MP3.
LVL 3
cmsJustinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

amit_gCommented:
Try this ...

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H5&)  ' My Documents namespace is &H5&
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path
Set objFolder = objFSO.GetFolder(strPath)
Wscript.Echo objFolder.Files.count
Dim MyDocSiz
MyDocSiz = (objFolder.Size / 1000000)
MyDocSiz = Round(MyDocSiz, 0)
Wscript.Echo MyDocSiz
for each file in objFolder.Files
    if (InStr(file.Name, ".pdf") > 0) then
        Wscript.Echo file.Name & "|" & file.Size
    end if
next
cmsJustinAuthor Commented:
Thanks! That method works great, but it doesn't recursivly go through the folders. Heres what I have so far:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H5&)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path
Set objFolder = objFSO.GetFolder(strPath)
Dim ThisSize
for each file in objFolder.Files
    if (InStr(file.Name, ".pdf") > 0) then
        ThisSize = ThisSize + file.Size
    end if
next
ThisSize = (ThisSize / 1000000)
ThisSize = Round(ThisSize, 0)
Wscript.Echo ThisSize

It will output the size rounded down to MB. Any idea how to go through subfolders though?

Thanks again
-Justin
amit_gCommented:
This should do it ...

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H5&)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path
Set objFolder = objFSO.GetFolder(strPath)
Dim ThisSize
ThisSize = GetFileSizeInFolder(strPath)

ThisSize = (ThisSize / 1000000)
ThisSize = Round(ThisSize, 0)
Wscript.Echo ThisSize

Function GetFileSizeInFolder(strPath)
      Dim objFolder, objSubFolder, file
      Dim ThisSize

      ThisSize = 0

      Set objFolder = objFSO.GetFolder(strPath)

      for each file in objFolder.Files
          if (InStr(file.Name, ".pdf") > 0) then
              ThisSize = ThisSize + file.Size
          end if
      next

      for each objSubFolder in objFolder.SubFolders
              ThisSize = ThisSize + GetFileSizeInFolder(objSubFolder.Path)
      next

      GetFileSizeInFolder = ThisSize
End Function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cmsJustinAuthor Commented:
We rock! Just kidding. You rock. Check it out:
I modified it to take commend line arguments. Using no arguments returns the size of My Documents in megs. Passing an argument to it will return the size for that extension type:

cscript//nologo \\server\share\mydocsiz.vbs mp3

Heres the code:

Dim arg
Dim ThisSize
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H5&)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path
Set objFolder = objFSO.GetFolder(strPath)

If WScript.Arguments.Count = 0 Then
      ThisSize = (objFolder.Size / 1000000)
      ThisSize = Round(ThisSize, 0)
      Wscript.Echo ThisSize
Else
   For each arg in WScript.Arguments  
      ThisSize = GetFileSizeInFolder(strPath, arg)
      ThisSize = (ThisSize / 1000000)
      ThisSize = Round(ThisSize, 0)
      Wscript.Echo ThisSize
   Next
End If

Function GetFileSizeInFolder(strPath, arg)
     Dim objFolder, objSubFolder, file
     Dim ThisSize
     Dim ExtName
     ExtName = "." & arg
     ThisSize = 0
     Set objFolder = objFSO.GetFolder(strPath)
     for each file in objFolder.Files
         if (InStr(file.Name, ExtName) > 0) then
             ThisSize = ThisSize + file.Size
         end if
     next

     for each objSubFolder in objFolder.SubFolders
             ThisSize = ThisSize + GetFileSizeInFolder(objSubFolder.Path, arg)
     next

     GetFileSizeInFolder = ThisSize
End Function

Thanks again!!!
-Justin
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.