• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4674
  • Last Modified:

get total file and folder size of my documents using vbscript

Is it possible to use vbscript to get the total file and folder size of my documents in windows 7, everything I have tried so far gives access denied on my documents, but works fine on any other folder.

Does anyone have an example?
0
Learning2Code
Asked:
Learning2Code
  • 7
  • 4
1 Solution
 
Bill PrewCommented:
What folder are you looking in?  You should be looking in:

C:\Users\myuser\Documents

~bp
0
 
Bill PrewCommented:
And a good way to find that in code is this:

Set objShell = CreateObject("WScript.Shell")
Wscript.Echo "My Documents = " & objShell.SpecialFolders("MyDocuments")

~bp
0
 
Learning2CodeAuthor Commented:
see the screenshot, any way I try to grab the size I get the following error

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("Wscript.Network")

objUser = objNetwork.UserName

folderIneed = "c:\users\" & objUser & "\documents"

WScript.Echo "Size = " & objFSO.GetFolder(folderIneed).Size

Open in new window


screen capture
0
Independent Software Vendors: 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!

 
Bill PrewCommented:
I suspect that's because there are folders under neath that folder that aren't real, like "My Pictures", "My Music", etc.

~bp
0
 
Bill PrewCommented:
One way to work around this is to add up the file sizes yourself, and avoid any files of subfolders that can't be accessed.  Here's a quick example that should give you some ideas on that, and also report any of the folders / files that can't be accessed.

' Set up filesystem object for usage
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")

' Get folder name to list off the command line, make sure it's valid
strFolder = objShell.SpecialFolders("MyDocuments")

' Get access to the folder we want to list files in
Set objFolder = objFSO.GetFolder(strFolder)

' Look for files
Wscript.Echo FindFiles(objFolder)

' Recursively tally the size of all files under a folder
' Protect against folders or files that are not accessible
Function FindFiles(objFolder)
   On Error Resume Next

   ' List files
   For Each objFile In objFolder.Files
       On Error Resume Next
       If Err.Number <> 0 Then ShowError "FindFiles:01", objFolder.Path
       On Error Resume Next
       FindFiles = FindFiles + objFile.Size
       If Err.Number <> 0 Then ShowError "FindFiles:02", objFile.Path
   Next

   If Err.Number = 0 Then
       ' Recursively drill down into subfolder
       For Each objSubFolder In objFolder.SubFolders
           On Error Resume Next
           If Err.Number <> 0 Then ShowError "FindFiles:04", objFolder.Path
           FindFiles = FindFiles + FindFiles(objSubFolder)
           If Err.Number <> 0 Then ShowError "FindFiles:05", objSubFolder.Path
       Next
   Else
       ShowError "FindFiles:03", objFolder.Path
   End If
End Function

Sub ShowError(strLocation, strMessage)
   WScript.StdErr.WriteLine "==> ERROR at [" & strLocation & "]"
   WScript.StdErr.WriteLine "    Number:[" & Err.Number & "], Source:[" & Err.Source & "], Desc:[" &  Err.Description & "]"
   WScript.StdErr.WriteLine "    " & strMessage
   Err.Clear
End Sub

Open in new window

~bp
0
 
Learning2CodeAuthor Commented:
yeah, it was the my music, my videos, my pictures giving the error, so if I do this it appears to work so far

For Each objSubfolder in colSubfolders
	Select Case objSubfolder.Name
		Case "My Music"
		Case "My Pictures"
		Case "My Videos"	
		Case Else
    		Wscript.Echo objSubfolder.Name, ConvertSize(objSubfolder.Size)
    		totalsize = totalsize + objSubfolder.Size
    End Select
Next

Open in new window

0
 
Learning2CodeAuthor Commented:
Do you or anyone have any other ideas, I need to get the total size of these folders MyDocs, Favs and Desktop, same as if you were to right click on each of the folders to get the folder size
0
 
Bill PrewCommented:
How about this?

Const blnShowErrors = False

' Set up filesystem object for usage
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")

' Display desired folder sizes
Wscript.Echo "MyDocuments : " & FormatSize(FindFiles(objFSO.GetFolder(objShell.SpecialFolders("MyDocuments"))))
Wscript.Echo "Favorites   : " & FormatSize(FindFiles(objFSO.GetFolder(objShell.SpecialFolders("Favorites"))))
Wscript.Echo "Desktop     : " & FormatSize(FindFiles(objFSO.GetFolder(objShell.SpecialFolders("Desktop"))))

' Recursively tally the size of all files under a folder
' Protect against folders or files that are not accessible
Function FindFiles(objFolder)
   On Error Resume Next

   ' List files
   For Each objFile In objFolder.Files
       On Error Resume Next
       If Err.Number <> 0 Then ShowError "FindFiles:01", objFolder.Path
       On Error Resume Next
       FindFiles = FindFiles + objFile.Size
       If Err.Number <> 0 Then ShowError "FindFiles:02", objFile.Path
   Next

   If Err.Number = 0 Then
       ' Recursively drill down into subfolder
       For Each objSubFolder In objFolder.SubFolders
           On Error Resume Next
           If Err.Number <> 0 Then ShowError "FindFiles:04", objFolder.Path
           FindFiles = FindFiles + FindFiles(objSubFolder)
           If Err.Number <> 0 Then ShowError "FindFiles:05", objSubFolder.Path
       Next
   Else
       ShowError "FindFiles:03", objFolder.Path
   End If
End Function

' Function to format a number into typical size scales
Function FormatSize(iSize)
   aLabel = Array("bytes", "KB", "MB", "GB", "TB")
   For i = 0 to 4
      If iSize > 1024 Then iSize = iSize / 1024 Else Exit For End If
   Next
   FormatSize = Round(iSize, 2) & " " & aLabel(i)
End Function

Sub ShowError(strLocation, strMessage)
   If blnShowErrors Then
      WScript.StdErr.WriteLine "==> ERROR at [" & strLocation & "]"
      WScript.StdErr.WriteLine "    Number:[" & Err.Number & "], Source:[" & Err.Source & "], Desc:[" &  Err.Description & "]"
      WScript.StdErr.WriteLine "    " & strMessage
      Err.Clear
   End If
End Sub

Open in new window

~bp
0
 
Learning2CodeAuthor Commented:
that works perfectly
0
 
Bill PrewCommented:
Great, thanks for the feedback.

~bp
0
 
pkarunaCommented:
Hi Bill

can we include mypictures / mymusic and myvideos ?

Is it possible to exclude one folder in my documents " No Backup".

thank you
0
 
Bill PrewCommented:
Here's an adjusted script to add those.  I had to switch to a slightly different approach to locating the special folder paths, but this should work well.  In addition there are other folders that can be found using this same routine, you just need to locate the const value for them.  The following sites list a number of those.  I typically work with the values in decimal rather than hex, but either can be handled.

http://www.activexperts.com/admin/vbscript/category/1213/
http://www.vbsedit.com/scripts/desktop/special/
http://spablog.ontrex.ch/2011/10/11/additional-special-folders/

Const blnShowErrors = False

Const MyDocuments = 5
Const Favorites = 6
Const MyMusic = 13
Const MyVideo = 14
Const Desktop = 16
Const MyPictures = 39

' Set up filesystem object for usage
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")

' Display desired folder sizes
Wscript.Echo "MyDocuments : " & FormatSize(FindFiles(objFSO.GetFolder(GetSpecialFolder("MyDocuments"))))
Wscript.Echo "Favorites   : " & FormatSize(FindFiles(objFSO.GetFolder(GetSpecialFolder("Favorites"))))
Wscript.Echo "Desktop     : " & FormatSize(FindFiles(objFSO.GetFolder(GetSpecialFolder("Desktop"))))
Wscript.Echo "MyPictures  : " & FormatSize(FindFiles(objFSO.GetFolder(GetSpecialFolder("MyPictures"))))
Wscript.Echo "MyMusic     : " & FormatSize(FindFiles(objFSO.GetFolder(GetSpecialFolder("MyMusic"))))
Wscript.Echo "MyVideo     : " & FormatSize(FindFiles(objFSO.GetFolder(GetSpecialFolder("MyVideo"))))

' Recursively tally the size of all files under a folder
' Protect against folders or files that are not accessible
Function FindFiles(objFolder)
   On Error Resume Next

   ' List files
   For Each objFile In objFolder.Files
       On Error Resume Next
       If Err.Number <> 0 Then ShowError "FindFiles:01", objFolder.Path
       On Error Resume Next
       FindFiles = FindFiles + objFile.Size
       If Err.Number <> 0 Then ShowError "FindFiles:02", objFile.Path
   Next

   If Err.Number = 0 Then
       ' Recursively drill down into subfolder
       For Each objSubFolder In objFolder.SubFolders
           On Error Resume Next
           If Err.Number <> 0 Then ShowError "FindFiles:04", objFolder.Path
           FindFiles = FindFiles + FindFiles(objSubFolder)
           If Err.Number <> 0 Then ShowError "FindFiles:05", objSubFolder.Path
       Next
   Else
       ShowError "FindFiles:03", objFolder.Path
   End If
End Function

' Function to format a number into typical size scales
Function FormatSize(iSize)
   aLabel = Array("bytes", "KB", "MB", "GB", "TB")
   For i = 0 to 4
      If iSize > 1024 Then iSize = iSize / 1024 Else Exit For End If
   Next
   FormatSize = Round(iSize, 2) & " " & aLabel(i)
End Function

Sub ShowError(strLocation, strMessage)
   If blnShowErrors Then
      WScript.StdErr.WriteLine "==> ERROR at [" & strLocation & "]"
      WScript.StdErr.WriteLine "    Number:[" & Err.Number & "], Source:[" & Err.Source & "], Desc:[" &  Err.Description & "]"
      WScript.StdErr.WriteLine "    " & strMessage
      Err.Clear
   End If
End Sub

Function GetSpecialFolder(intFolder)
   Set objShell = CreateObject("Shell.Application")
   Set objFolder = objShell.Namespace(intFolder)
   Set objFolderItem = objFolder.Self
   GetSpecialFolder = objFolderItem.Path
End Function

Open in new window

~bp
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now