Folder Size: Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
No edit summary
 
m (BrokenLinks: Samples, ...)
 
Line 1: Line 1:
Back to [[Samples]]
[[Category:Samples]]
Back to [[:Category:Samples|Samples]]<!-- look at possible separate samples page -->


'''Purpose:'''
'''Purpose:'''
Line 18: Line 19:




<syntaxhighlight lang="autoit">
  ;--------------------------------------------------------------------------------
  ;--------------------------------------------------------------------------------
  ;Example of call to UDF _FolderSize()
  ;Example of call to UDF _FolderSize()
Line 66: Line 68:
      
      
  EndFunc
  EndFunc
 
</syntaxhighlight>
 
[[Category:Samples]]

Latest revision as of 13:34, 17 November 2012

Back to Samples

Purpose:

Find the space used by a folder, including subfolders and their files too.

Illustrates:

  • Automation of DOS commands to accomplish a recursive folder scan.
  • Basic use of StringFormat

Notes:

  • Compare this method with Quick Folder Size, which uses a similar method but processes a different DOS command ;o)
  • Compare this method with the builtin Autoit command "DirGetSize"


 ;--------------------------------------------------------------------------------
 ;Example of call to UDF _FolderSize()
 ;--------------------------------------------------------------------------------
     $sFolder = @TempDir
     $nFolderSize = _FolderSize($sFolder)
     If @error then
         ;failed
     Else
         Msgbox (4096 + 32,@ScriptName, StringFormat("Bytes in %s = %d", $sFolder, $nFolderSize))
     Endif
 
 
 Func _FolderSize($psFolder)
 ;--------------------------------------------------------------------------------
 ;Uses the DOS "dir" command to recursively collect all filenames
 ;in a folder and its children; then adds up the filesize for each one
 ;and returns the total.
 ;--------------------------------------------------------------------------------
 Local $sFileList, $nFileList, $nTotalSize
 
     $sFileList = "C:\_FolderSize.LST"
     RunWait(@Comspec & " /C DIR """ & $psFolder & "\*.*"" /s/b /A:-D > " & $sFileList,"", @SW_HIDE)
     
     $nFileList = FileOpen($sFileList, 0)  ;0=open
     If $nFileList = -1 then
         ;Couldn't open list of files in designated folder
         SetError(1)
         Return 0
     Endif
 
 ;Read the listfile, and determine file sizes,
 ;writing name and size to CSV file
     $nTotalSize = 0
     While 1
         $sFilePathName = FileReadLine($nFileList)
         if @error = -1 then;EOF
             ExitLoop
         endif
         $nTotalSize = $nTotalSize + FileGetSize($sFilePathName)
     Wend
 
 ;Cleanup
     FileClose($nFileList)
     FileDelete($sFileList)
     
     Return $nTotalSize
     
 EndFunc