Announcement

Collapse
No announcement yet.

Script to Report My Documents Size

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Script to Report My Documents Size

    Hi everyone,
    We are moving all of users my documents to a network share. Before we do, I need to find out how much space I need. Is there a way to script the my documents size and report it to a text document that keeps track of size.

    Some caveats:
    1. Only report once. I can do this by setting an IF statement and copying a file down after reporting.
    2. I do not care about My Pictures or My Music. The script needs to exclude these directories.

    Any help would be appreciated.

  • #2
    Re: Script to Report My Documents Size

    I'm not really sure how this would work, just because of the excluding part kindof throws me off, but it's an idea. I haven't tested...try this:

    http://www.codingforums.com/showthread.php?t=68639

    Code:
    Set objNetwork = CreateObject("WScript.Network")
    strUser = objNetwork.UserName
    strPath = "C:\Documents and Settings\" & strUser & "\My Documents"
    strLogFile = "C:\log.txt"
    arrExclude = ("My Pictures", "My Videos")
    Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(strLogFile, 8)
    	If objFSO.FolderExists(strPath) Then
    		GetSize(strPath)
    		objFile.WriteLine "My Documents size for " & strUser & " : " & intSizeTotal
    	Else
    		objFile.WriteLine "My Documents does not exist!"
    		WScript.Quit
    	End If
    
    Function GetSize(strPath)
    	Set objFolder = objFSO.GetFolder(strPath)
    		For Each File in objFolder.Files
    			For i = lbound(arrExclude) to ubound(arrExclude)
    			   Exclude = False
    				If file.name = arrEclude(i) Then
    					Exclude = True
    				End If
    			Next
    				If Exclude = False then
    					objFolder.Size = intSize
    				End If
    		intSizeTotal = intSizeTotal + intSize
    		Next
    End Function
    
    objFile.Close
    
    WScript.Quit(0)
    Last edited by ekrengel; 21st March 2009, 22:02.

    Comment


    • #3
      Re: Script to Report My Documents Size

      Actually this might be much easier:

      Code:
      Set objNetwork = CreateObject("WScript.Network")
      strUser = objNetwork.UserName
      strDocuments = "C:\Documents and Settings\" & strUser & "\My Documents"
      strMusic = "C:\Documents and Settings\" & strUser & "\My Documents\My Music"
      strPictures = "C:\Documents and Settings\" & strUser & "\My Documents\My Pictures"
      strLogFile = "C:\log.txt"
      Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
      Set objFile = objFSO.OpenTextFile(strLogFile, 8)
      	If objFSO.FolderExists(strDocuments) Then
      		Set objDocuments = objFSO.GetFolder(strDocuments)
      		Set objMusic = objFSO.GetFolder(strMusic)
      		Set objPictures = objFSO.GetFolder(strPictures)
      			intSizeTotal = (objDocuments.Size - (objMusic.Size + objPictures.Size))
      		objFile.WriteLine "My Documents size for " & strUser & " : " & intSizeTotal
      	Else
      		objFile.WriteLine "My Documents does not exist!"
      		WScript.Quit
      	End If
      
      objFile.Close
      
      WScript.Quit(0)

      Comment


      • #4
        Re: Script to Report My Documents Size

        Yes that will do the job, getting the total size of the users's My Documents and subtract the sizes of the special folders that are found in the folder

        Here is a similar vbscript.
        Basically the main difference between this script and ekrengel's script is that it determine the actual path of the special folders first (is also OS language independed).

        Code:
        Const CSIDL_MY_DOCUMENTS = &H5&
        Const CSIDL_MY_MUSIC = &Hd&
        Const CSIDL_MY_PICTURES = &H27&
        Const CSIDL_MY_VIDEOS = &He&
        Const ForAppending = 8
        Dim objShell
        
        Set objShell = CreateObject("Shell.Application")
        Set fso = CreateObject("scripting.filesystemobject")
        Set WshNetwork = WScript.CreateObject("WScript.Network")
        
        OutputName = WshNetwork.computername & "_" & WshNetwork.userdomain & "#" & WshNetwork.username
        OutputPath = "C:\test"
        
        '# Quit the script if file exist already
        If fso.FileExists(OutputPath & "\" & OutputName & ".csv") Then wscript.quit
        
        '# Determine the path and size of the users's domuments folder
        MY_DOCUMENTS = GetSpecialFolder(CSIDL_MY_DOCUMENTS)
        With fso.getfolder(MY_DOCUMENTS)
           FolderSize = .size
        End With
        
        On Error Resume Next
        '# Subtract the sizes of the following 3 special folders
        '# If they are a subfolder of the My Domuments folder
        
          Set re = new regexp : re.IgnoreCase = true
          strRoot = Replace(MY_DOCUMENTS,"\","\\")
          re.Pattern = "^(" & strRoot & "\\)"
        
        MY_MUSIC = GetSpecialFolder(CSIDL_MY_MUSIC)
           If re.Test(MY_MUSIC) = True Then
              With fso.getfolder(MY_MUSIC)
                 FolderSize = FolderSize - .size
              End With
           End If
        
        MY_PICTURES = GetSpecialFolder(CSIDL_MY_PICTURES)
           If re.Test(MY_PICTURES) = True Then
              With fso.getfolder(MY_PICTURES)
                 FolderSize = FolderSize - .size
              End With
           End If
        
        MY_VIDEOS = GetSpecialFolder(CSIDL_MY_VIDEOS)
           If re.Test(MY_VIDEOS) = True Then
              With fso.getfolder(MY_VIDEOS)
                 FolderSize = FolderSize - .size
              End With
           End If
        On Error Goto 0
        
        rem Set objNewFile = fso.CreateTextFile(strFile)
        Set objLogFile = fso.OpenTextFile (_
           OutputPath & "\" & OutputName & ".csv", ForAppending, True)
           
        objLogFile.writeline OutputName & "," & FolderSize & ",bytes"
        objLogFile.close : Set objLogFile = Nothing
        
        rem Wscript.Echo "Done,",_
        rem      vbnewline & "My Documents FolderSize is", FolderSize, "bytes"
        wscript.quit 0
        
        Function GetSpecialFolder(sfolder)
           With objShell
              With .Namespace(sfolder)
                 GetSpecialFolder = .Self.Path
              End With
           End With
        End Function
        It will write to a computer+user unique log file. If the log file exist already the script will not rewriting or appending to the previous file, the script will just quit.

        Later on you can merge all single files to one csv file and calculate the total size.


        \Rems

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Script to Report My Documents Size



          I think I've learned more from Rems than my teachers in school.

          Comment


          • #6
            Re: Script to Report My Documents Size

            That works perfect. Thank You, Thank You!

            I always praise this site to my coworkers!

            Comment


            • #7
              Re: Script to Report My Documents Size

              I apologize for the question but will this script check my entire network or only my local machine?

              Comment


              • #8
                Re: Script to Report My Documents Size

                Originally posted by kfo4 View Post
                I apologize for the question but will this script check my entire network or only my local machine?
                Only the My documents folder of the current user on this computer.


                \Rems

                This posting is provided "AS IS" with no warranties, and confers no rights.

                __________________

                ** Remember to give credit where credit's due **
                and leave Reputation Points for meaningful posts

                Comment


                • #9
                  Re: Script to Report My Documents Size

                  Thanks for the script Rems, just what i was looking for, however on Vista Ent. i recieve an error on Line: 35 Char: 4

                  Which is here:

                  ....
                  With fso.getfolder(MY_DOCUMENTS)
                  ->FolderSize = .size
                  ....

                  Any info for me on that?

                  Edit: UAC disabled, user is Local Admin.
                  Last edited by Kelias; 19th May 2009, 11:54. Reason: More info.

                  Comment


                  • #10
                    Re: Script to Report My Documents Size

                    Are you sure that you showed the line that raised the error? You showed line 21 from the sample.

                    At line 35 there is:
                    ..........MY_MUSIC = GetSpecialFolder(CSIDL_MY_MUSIC)
                    ..........If re.Test(MY_MUSIC) = True Then
                    .............With fso.getfolder(MY_MUSIC)
                    35,4 -->....FolderSize = FolderSize - .size
                    .............End With
                    ..........End If

                    Perhaps you have made changes to the script?


                    \Rems


                    EDIT
                    After running some tests it can be concluded that the script will not run on Windows Vista and newer Windows OSs. The ".Size" Property will return an "Access Denied" on any folder. Also, when you would use WMI the FileSize property of the Win32_Directory class that does not work either.
                    Last edited by Rems; 19th May 2009, 19:23.

                    This posting is provided "AS IS" with no warranties, and confers no rights.

                    __________________

                    ** Remember to give credit where credit's due **
                    and leave Reputation Points for meaningful posts

                    Comment


                    • #11
                      Re: Script to Report My Documents Size

                      Reason for the other line number:

                      '=================
                      '
                      ' NAME: USR-Reports-MyDocuments.vbs
                      '
                      ' AUTHOR: Rems, Petri.co.il
                      ' CREATED: 2009-03-22 (11:35)
                      '
                      ' CHANGED: 2009-05-19 (10:16)
                      ' BY: Kristofer Ohman, IBX
                      '
                      ' COMMENT: USR-Reports-My Documents: internal.se
                      '
                      '=================

                      Thanks for the quick answer though, will continue to search for a solution.

                      Comment

                      Working...
                      X