Jump to content

Autoit_Tree_FileFind /function


wolf9228
 Share

Recommended Posts

http://blogs.msdn.com/gstemp/archive/2004/08/11/213028.aspx

Optional: specifies whether to return files folders or both

$iFlag=0(Default) Return both files and folders

$iFlag=1 Return files only

$iFlag=2 Return Folders only

Autoit_Tree_FileFind.AU3

#Include <Array.au3>
Global $FileFind_Array[2]

;TTree_FileFind($szPath = "",$iFlag = 0 ,$extension = "")
;Optional: specifies whether to return files folders or both
;$iFlag=0(Default) Return both files and folders
;$iFlag=1 Return files only
;$iFlag=2 Return Folders only

$TIMINT = TimerInit()
$TREE_FileFind = Tree_FileFind( "C:\" , 2 , "") ; $iFlag = 0 In C:\ Searching for folders and files takes a long time
$milliseconds = TimerDiff($TIMINT)
_ArrayDisplay($TREE_FileFind, "Tree_FileFind Return Folders only")
MsgBox(0,"Tree_FileFind","AUTOIT ==> milliseconds ==>  " & $milliseconds )

$TIMINT = TimerInit()
$TREE_FileFind = Tree_FileFind( @ProgramFilesDir , 1 , "exe")
$milliseconds = TimerDiff($TIMINT)
_ArrayDisplay($TREE_FileFind, "Tree_FileFind Return exe files only")
MsgBox(0,"Tree_FileFind","AUTOIT ==> milliseconds ==>  " & $milliseconds )

$TIMINT = TimerInit()
$TREE_FileFind = Tree_FileFind(@MyDocumentsDir , 0 , "")
$milliseconds = TimerDiff($TIMINT)
_ArrayDisplay($TREE_FileFind, "Tree_FileFind Return both files and folders")
MsgBox(0,"Tree_FileFind","AUTOIT ==> milliseconds ==>  " & $milliseconds )

;Tree_FileFind($szPath = "",$iFlag = 0 ,$extension = "")
;Optional: specifies whether to return files folders or both
;$iFlag=0(Default) Return both files and folders
;$iFlag=1 Return files only
;$iFlag=2 Return Folders only

Func Tree_FileFind( $szPath = "" , $iFlag = 0 , $extension = "" )

Local $TREE_Array[1] , $Dirs_Array[1]

$Dirs_Array[0] = 0
$TREE_Array[0] = 0

$FileFind_Array[0] = $Dirs_Array
$FileFind_Array[1] = $TREE_Array

If FileExists($szPath) = 0 Then Return $FileFind_Array[1]

$extension  = StringUpper($extension)
$extensionLen = StringLen($extension)

$szPath = StringReplace($szPath , "\\" , "\" )
If StringRight($szPath,1) == "\" Then $szPath = StringTrimRight($szPath,1)
$IsRoot = StringLen($szPath) < 3

$Return = FileFind($szPath,$iFlag,$extension,$extensionLen,$IsRoot)
IF $Return Then

$Dirs_Array = $FileFind_Array[0]
$TREE_Array = $FileFind_Array[1]

For $i = 1 To $Dirs_Array[0]
LoopTree_FileFind($Dirs_Array[$i],$iFlag,$extension,$extensionLen)
Next

EndIf

Return $FileFind_Array[1]

EndFunc

Func FileFind($szPath,$iFlag,$extension,$extensionLen,$IsRoot = 0)

$Dirs_Array = $FileFind_Array[0]
$TREE_Array = $FileFind_Array[1]

$Dirs_Array[0] = 0

$sFilter = "*.*"
if $iFlag = 2 Then $sFilter = "*."

if $IsRoot Then
FileChangeDir($szPath & "\")
Else
FileChangeDir($szPath)
EndIf

$search = FileFindFirstFile($sFilter)
If $search = -1 Then Return False

While 1

    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    
    $file = $szPath & "\" & $file

    Select
    Case StringInStr(FileGetAttrib($file),"D")

    IF $iFlag <> 1 Then
    ReDim $TREE_Array[$TREE_Array[0] + 2]
    $TREE_Array[$TREE_Array[0] + 1] = $file
    $TREE_Array[0] += 1
    EndIf

    If StringInStr(FileGetAttrib($file), "S") Then ContinueLoop
    ;System folders when searching in them, errors and freezes occur, so
    ;they are excluded from the search. This happens on some Windows systems

    if (UBound($Dirs_Array) < $Dirs_Array[0] + 2) Then _
    ReDim $Dirs_Array[$Dirs_Array[0] + 2]

    $Dirs_Array[$Dirs_Array[0] + 1] = $file
    $Dirs_Array[0] += 1

    Case StringInStr(FileGetAttrib($file),"D") = 0

    if $iFlag = 2 Then ContinueLoop

    Select
    Case $extensionLen
    If (StringUpper(StringRight($file,$extensionLen)) == $extension) = 0 Then ContinueLoop
    ReDim $TREE_Array[$TREE_Array[0] + 2]
    $TREE_Array[$TREE_Array[0] + 1] = $file
    $TREE_Array[0] += 1
    Case $extensionLen = 0
    ReDim $TREE_Array[$TREE_Array[0] + 2]
    $TREE_Array[$TREE_Array[0] + 1] = $file
    $TREE_Array[0] += 1
    EndSelect

    EndSelect

WEnd

FileClose($search)
FileChangeDir(@SCriptDir)

$FileFind_Array[0] = $Dirs_Array
$FileFind_Array[1] = $TREE_Array

Return True

EndFunc

Func LoopTree_FileFind($szPath,$iFlag,$extension,$extensionLen)

$Return = FileFind($szPath,$iFlag,$extension,$extensionLen)
IF $Return Then

$Dirs_Array = $FileFind_Array[0]

For $i = 1 To $Dirs_Array[0]
LoopTree_FileFind($Dirs_Array[$i],$iFlag,$extension,$extensionLen)
Next

EndIf

Return $FileFind_Array[1]

EndFunc

 

Edited by wolf9228

صرح السماء كان هنا

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...