Folder Size
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