Learning2Code
asked on
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?
Does anyone have an example?
And a good way to find that in code is this:
Set objShell = CreateObject("WScript.Shel l")
Wscript.Echo "My Documents = " & objShell.SpecialFolders("M yDocuments ")
~bp
Set objShell = CreateObject("WScript.Shel
Wscript.Echo "My Documents = " & objShell.SpecialFolders("M
~bp
ASKER
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
I suspect that's because there are folders under neath that folder that aren't real, like "My Pictures", "My Music", etc.
~bp
~bp
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
~bp
ASKER
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
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
that works perfectly
Great, thanks for the feedback.
~bp
~bp
Hi Bill
can we include mypictures / mymusic and myvideos ?
Is it possible to exclude one folder in my documents " No Backup".
thank you
can we include mypictures / mymusic and myvideos ?
Is it possible to exclude one folder in my documents " No Backup".
thank you
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/
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
~bp
C:\Users\myuser\Documents
~bp