Sign in to follow this  
Followers 0
ioliver

DirectoryFindFirstDirectory()

5 posts in this topic

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.

Ian


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

Share this post


Link to post
Share on other sites



If StringInStr(FileGetAttrib($filename), "D") Then

It's a directory.


Who else would I be?

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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')
    $aFolders[0][0]=1
    $aFolders[1][0]=$sPath
    $iSize=__GetTreeSize($sPath, 0, 100, $aFolders)
    $aFolders[1][1]=$iSize
    ReDim $aFolders[$aFolders[0][0] + 1][2]
    _ProgressDelete()
    Return $aFolders
EndFunc

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
                EndIf
            Else
                $iTotalSize = $iTotalSize + FileGetSize($sPath & '\' & $sFileName)
            EndIf
            $sFileName = FileFindNextFile($hSearch_All)
        WEnd
    EndIf
    FileClose($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
    Next
    _ProgressUpdate($fCurPer + $fMaxPer, __FileTruncPath($sPath, 60))
    Return $iTotalSize
EndFunc

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)
    Else
        $sFile = $sPath
        $sPath = ''
    EndIf
;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)
    Else
        Return StringLeft($sPath, $iSize - 3 - $iFile) & '...'  & $sFile
    EndIf
EndFunc
Edited by blindwig

Share this post


Link to post
Share on other sites

Thanks for all the suggestions. I'll try them out on tomorrow.

I really appricate all the help.

Ian


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

Share this post


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
Sign in to follow this  
Followers 0