Jump to content


Recommended Posts

Is there a way to find Directories, like there's a way to find Files using FileFindFirstFile()?

What I need to do is find the size of all the directories in the 'Documents and Settings' folder.

Thanks for any help.


"Blessed be the name of the Lord" - Job 1:21Check out Search IMF

Link to post
Share on other sites

Do a normal search using the FileFind*-functions, and then use FileGetAttrib()

and check if the returned value contains a "D". If it does, it is a directory.

Edit : Typo..plus..doh !

Edited by Helge
Link to post
Share on other sites

You could use my _GetTreeSize function as a reference. It takes a path as a parameter, and returns a 2d array list of directories and their total size.

Func _GetTreeSize($sPath)
    Dim $aFolders[100][2], $iSize
    _ProgressCreate(__FileTruncPath($sPath, 45), '', 'Directory Size')
    $iSize=__GetTreeSize($sPath, 0, 100, $aFolders)
    ReDim $aFolders[$aFolders[0][0] + 1][2]
    Return $aFolders

Func __GetTreeSize($sPath, $fCurPer, $fMaxPer, ByRef $aFolders)
;Get a list of all the folders, and a count of total file size
    Local $aFolder[100], $hSearch_All=FileFindFirstFile($sPath & '\*'), $iTotalSize=0
    If $hSearch_All <> -1 Then
        $sFileName = FileFindNextFile($hSearch_All)
        While Not @error
            If _FileIsDir($sPath & '\' & $sFileName) Then
                If $sFileName <> '.' And $sFileName <> '..' Then
                    $aFolder[0] = $aFolder[0] + 1
                    If $aFolder[0] >= UBound($aFolder) Then ReDim $aFolder[$aFolder[0] + 100]
                    $aFolder[$aFolder[0]] = $sFileName
                $iTotalSize = $iTotalSize + FileGetSize($sPath & '\' & $sFileName)
            $sFileName = FileFindNextFile($hSearch_All)
    ReDim $aFolder[$aFolder[0] + 1]
;recurse into each folder
    Local $i, $fSlice = $fMaxPer / $aFolder[0], $fNewPer, $iDirSize, $iParentIndex
    For $i = 1 To $aFolder[0]
        $fNewPer = $fCurPer + $fSlice * ($i - 1)
        $aFolders[0][0] = $aFolders[0][0] + 1
        If $aFolders[0][0] >= UBound($aFolders) Then ReDim $aFolders[$aFolders[0][0] * 2][2]
        $iNodePtr = $aFolders[0][0]
        $iDirSize = __GetTreeSize($sPath & '\' & $aFolder[$i], $fNewPer, $fSlice, $aFolders)
        $aFolders[$iNodePtr][0] = $sPath & '\' & $aFolder[$i]
        $aFolders[$iNodePtr][1] = $iDirSize
        $iTotalSize = $iTotalSize + $iDirSize
    _ProgressUpdate($fCurPer + $fMaxPer, __FileTruncPath($sPath, 60))
    Return $iTotalSize

It uses my progress bars, get them here or just remove those lines.

It also uses my _FileIsDir() and _FileTruncPath() functions:

; Function Name:    _FileIsDir
; Description:    Returns true or false weather given file is a directory or not
; Parameter(s):  $Path
; Requirement(s):
; Return Value(s):  0 = not a directory or does not exist, 1 = file exists and is a directory
; Author(s):        Mike Ratzlaff <mike@ratzlaff.org>
; Revision:      20050623A
Func _FileIsDir($Path)
;This function checkes to see if $FileName exists and if it is a Directory
    If StringInStr(FileGetAttrib($Path), 'D') Then Return 1
    Return 0
EndFunc ;==>_FileIsDir

;Truncates $sPath so that its length is <= $iSize
Func _FileTruncPath($sPath, $iSize)
;split file from path
    Local $iSplit = StringInStr($sPath, '\', 0, -1)
    If $iSplit Then
        $sFile = StringMid($sPath, $iSplit)
        $sPath = StringLeft($sPath, $iSplit - 1)
        $sFile = $sPath
        $sPath = ''
;truncate and return new path
    Local $iPath = StringLen($sPath), $iFile = StringLen($sFile)
    If $iPath + $iFile <= $iSize Then
        Return $sPath & $sFile
    ElseIf $iFile > $iSize - 3 Then
        Return '...' & StringRight($sFile, $iSize - 3)
        Return StringLeft($sPath, $iSize - 3 - $iFile) & '...'  & $sFile
Edited by blindwig
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...