Jump to content

Find all files


Achilles
 Share

Recommended Posts

This function finds all the files you have (within 5 levels of the directory you choose, but you can add on more or let me know if you think more would be useful).... I'm using it for a media player I'm making...

#include <File.au3>
#include <Array.au3>
Global $allfiles ;A list of all the files
Global $end ;Ending of name
$folderDir = FileSelectFolder("Choose a folder to add to your library", @MyDocumentsDir)
If @ERROR then exit

FindFiles("jpg", $folderDir)

Func FindFiles ($end, $folderToAdd)
$xFolder = _FileListToArray($folderToAdd, "*.*",2)
For $a = 1 to $xFolder[0]
    $yFolder = _FileListToArray($folderToAdd & "\" & $xFolder[$a], "*.*",2)
    For $b = 1 to $yFolder[0]
        $zFolder = _FileListToArray($folderToAdd & "\" & $xFolder[$a] & "\" & $yFolder[$b], "*.*",2)
        If IsArray($zFolder) and $zFolder <> 0 then 
            For $c = 1 to $zFolder[0]
                $xyzFolder = _FileListToArray($folderToAdd & "\" & $xFolder[$a] & "\" & $yFolder[$b] & "\" & $zFolder[$c], "*.*",2)
                If IsArray($xyzFolder) and $xyzFolder <> 0 then 
                    For $d = 1 to $xyzFolder[0]
                        $wxyzFolder = _FileListToArray($folderToAdd & "\" & $xFolder[$a] & "\" & $yFolder[$b] & "\" & $zFolder[$c] & "\" & $xyzFolder[$d])
                        If IsArray($wxyzFolder) and $wxyzFolder <> 0 then 
                            For $e = 1 to $wxyzFolder[0]
                            $newFolder = $folderToAdd & "\" & $xFolder[$a] & "\" & $yFolder[$b] & "\" & $zFolder[$c] & "\" & $xyzFolder[$d] ;Fifth...
                            Addarray($newFolder)
                            Next
                        Endif
                    $newFolder = $folderToAdd & "\" & $xFolder[$a] & "\" & $yFolder[$b] & "\" & $zFolder[$c] ;Fourth...
                    Addarray($newFolder)
                    Next
                Endif
                $newFolder = $folderToAdd & "\" & $xFolder[$a] & "\" & $yFolder[$b] & "\" & $zFolder[$c] ;Three levels...
                Addarray($newFolder)
            Next
            $newFolder = $folderToAdd & "\" & $xFolder[$a] & "\" & $yFolder[$b] ;Two levels below...
            Addarray($newFolder)
        Endif
    Next
    $newFolder = $folderToAdd & "\" & $xFolder[$a] ;Adds all the files one level below original
    Addarray($newFolder)
Next
Addarray($folderToAdd) ;Adds all the files in the original directory

ProgressOn("Adding Music", "")
$all = Stringsplit($allfiles, "<!>")
For $a = 1 to $all[0]
    $check = Stringsplit($all[$a], ".")
    $c = $check[0]
    If $check[$c] = $end then 
        Iniwrite(@DesktopCommonDir & "\Testing.ini", "File type: " & $end, $a, $all[$a])
    Endif
    $increment = 1/$all[0] * 100
    ProgressSet($increment * $a, "")
Next
ProgressOff()
Endfunc

Func Addarray($folder)
    $temp = _FileListToArray($folder, "*." & $end & "*",1)
    If IsArray($temp) and $temp <> 0 then 
        For $k = 1 to $temp[0]
            $allFiles = $allFiles & ($folder & "\" & $temp[$k] & "<!>")
        Next
    Endif
EndFunc
My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

  • Moderators

This might be a tad faster... and I don't think it has a recursion limit:

Example:

#include <array.au3>
$file = @DesktopDir & '\SongFolder'
$array = _FileListToArrayEx($file, '*.mp3;*.wav', 0, '', False, True)
_ArrayDisplay($array, '')

Func _FileListToArrayEx($sPath, $sFilter = '*.*', $iFlag = 0, $sExclude = '', $iRecurse = False)
    If Not FileExists($sPath) Then Return SetError(1, 1, '')
    If $sFilter = -1 Or $sFilter = Default Then $sFilter = '*.*'
    If $iFlag = -1 Or $iFlag = Default Then $iFlag = 0
    If $sExclude = -1 Or $sExclude = Default Then $sExclude = ''
    Local $aBadChar[6] = ['\', '/', ':', '>', '<', '|']
    For $iCC = 0 To 5
        If StringInStr($sFilter, $aBadChar[$iCC]) Or _
            StringInStr($sExclude, $aBadChar[$iCC]) Then Return SetError(2, 2, '')
    Next
    If StringStripWS($sFilter, 8) = '' Then Return SetError(2, 2, '')
    If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 3, '')
    If Not StringInStr($sFilter, ';') Then $sFilter &= ';'
    Local $aSplit = StringSplit(StringStripWS($sFilter, 8), ';'), $sRead
    For $iCC = 1 To $aSplit[0]
        If StringStripWS($aSplit[$iCC], 8) = '' Then ContinueLoop
        If StringLeft($aSplit[$iCC], 1) = '.' And _
            UBound(StringSplit($aSplit[$iCC], '.')) - 2 = 1 Then $aSplit[$iCC] = '*' & $aSplit[$iCC]
        Local $iPid
        If Not $iRecurse Then
            $iPid = Run(@ComSpec & ' /c ' & 'dir "' & $sPath & '\' & $aSplit[$iCC] & '" /b /o-e /od', '', @SW_HIDE, 6)
        Else
            $iPid = Run(@Comspec & ' /c dir /b /s /a "' & $sPath & '\' & $aSplit[$iCC] & '"', '', @SW_HIDE, 6)
        EndIf
        While 1
            $sRead &= StdoutRead($iPid)
            If @error Then ExitLoop
        WEnd
    Next
    If StringStripWS($sRead, 8) = '' Then Return SetError(4, 4, '')
    Local $aFSplit = StringSplit(StringTrimRight(StringStripCR($sRead), 1), @LF)
    Local $sHold
    For $iCC = 1 To $aFSplit[0]
        If $sExclude And StringLeft(StringTrimLeft($aFSplit[$iCC], StringInStr($aFSplit[$iCC], '\', 0, -1)), _
            StringLen(StringReplace($sExclude, '*', ''))) = StringReplace($sExclude, '*', '') Then ContinueLoop
        Switch $iFlag
            Case 0
                $sHold &= $aFSplit[$iCC] & Chr(1)
            Case 1
                If StringInStr(FileGetAttrib($aFSplit[$iCC]), 'd') Then ContinueLoop
                $sHold &= $aFSplit[$iCC] & Chr(1)
            Case 2
                If Not StringInStr(FileGetAttrib($aFSplit[$iCC]), 'd') Then ContinueLoop
                $sHold &= $aFSplit[$iCC] & Chr(1)
        EndSwitch
    Next
    If StringTrimRight($sHold, 1) Then Return StringSplit(StringTrimRight($sHold, 1), Chr(1))
    Return SetError(4, 4, '')
EndFunc

Edit:

Changed the example

Edit:

Example was showing me missing $sPath & '\' << however when I went to edit it because it was pointed out by Danny35, it was actually there, just not showing up in the AutoIt Code Tags... so I changed the code tags.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

or try this:

#include-once

;===============================================================================
;
; Function Name:    __AddToArray()
; Description:      Internal function - Add an element to array
; Parameter(s):     None
; Requirement(s):   AutoIt3
; Return Value(s):  On Success - Returns array
; Author(s):        Alexsandro Percy
;
;===============================================================================
Func __AddToArray( byref $DirArray, $Dir )
    ReDim $DirArray[UBound( $DirArray ) + 1]
    $DirArray[UBound( $DirArray ) - 1] = $Dir
EndFunc

;===============================================================================
;
; Function Name:    __GetFileList()
; Description:      Internal function - returns array filled with filefullpath
;                   The first element enum all occurrences
; Parameter(s):     $Path       - Base path for search
;                   $SearchStr  - Pattern for search (ex: '*.bmp' )
; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
; Return Value(s):  On Success - Returns array with filenames
;                   On Failure - Returns first element 0
; Author(s):        Alexsandro Percy
;
;===============================================================================
Func __GetFileList( $Path, $SearchStr )
    Local $RetVal[1]
    Local $VmcSearch = FileFindFirstFile( $Path & "\" & $SearchStr )  
    If $VmcSearch <> -1 Then
        Local $File
        While 1
            $File = FileFindNextFile( $VmcSearch ) 
            If @error Then ExitLoop
            __AddToArray( $RetVal, $Path & "\" & $File )
        WEnd
        FileClose( $VmcSearch )
    EndIf
    $RetVal[0] = UBound( $RetVal ) - 1
    Return $RetVal
EndFunc

;===============================================================================
;
; Function Name:    __GetTreeList()
; Description:      Returns array filled with tree of directories
;                   The first element enum all occurrences
; Parameter(s):     $Path       - Base path for search
;                   $TreeList   - Return array (must be an array)
; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
; Return Value(s):  On Success - Returns array with directories
;                   On Failure - Returns first element 0
; Author(s):        Alexsandro Percy
;
;===============================================================================
Func _GetTreeList( $Path, ByRef $TreeList )
    If UBound( $TreeList ) = 0 Then
        Return
    EndIf
    
    Local $Search = FileFindFirstFile( $Path & "\*.*" )  
    If $Search <> -1 Then
        Local $File
        While 1
            $File = FileFindNextFile( $Search ) 
            If @error Then ExitLoop
            Local $FoundFile = $Path & "\" & $File
            If StringInStr( FileGetAttrib ( $FoundFile ), "D" ) Then
                __AddToArray( $TreeList, $FoundFile )
                _GetTreeList( $FoundFile, $TreeList )
            EndIf
        WEnd
        FileClose( $Search )
    EndIf
    $TreeList[0] = UBound( $TreeList ) - 1
EndFunc

;===============================================================================
;
; Function Name:    _GetFileListInVector()
; Description:      Returns array filled with all filefullpaths in tree
;                   The first element enum all occurrences
; Parameter(s):     $Path       - Base path for search
;                   $SearchStr  - Pattern for search (ex: '*.bmp' )
; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
; Return Value(s):  On Success - Returns array with filenames
;                   On Failure - Returns first element 0
; Author(s):        Alexsandro Percy
;
;===============================================================================
Func _GetFileListInVector( $Path, $SearchStr )
    Local $RetVal[1]
    Local $VmcList = __GetFileList( $Path, $SearchStr )
    Local $i = 1
    For $i = 1 to $VmcList[0]
        __AddToArray( $RetVal, $VmcList[$i] )
    Next
    
    Local $DirList[1]
    _GetTreeList( $Path, $DirList )
    Local $j = 1
    For $i = 1 to $DirList[0]
        $VmcList = __GetFileList( $DirList[$i], $SearchStr )
        For $j = 1 to $VmcList[0]
            __AddToArray( $RetVal, $VmcList[$j] )
        Next
    Next
    $RetVal[0] = UBound( $RetVal ) - 1
    Return $RetVal  
EndFunc

:whistle:

Edited by A. Percy

Só o que posso lhe dizer, bom é quando faz mal!My work:Au3Irrlicht - Irrlicht for AutoItMsAgentLib - An UDF for MSAgentAu3GlPlugin T2 - A 3D plugin for AutoIt...OpenGl Plugin - The old version of Au3GlPlugin.MAC Address Changer - Changes the MAC AddressItCopter - A dragonfly R/C helicopter simulator

VW Bug user

Pinheiral (Pinewood) city:

http://pt.wikipedia.org/wiki/Pinheiral

Link to comment
Share on other sites

Nice... still a bit slow for me personally.

If that's slow I don't know what mine is.. some sort of Neanderthal compared to a airplane... anyways, thanks both of you... I realized that my code was pretty simple and was hoping to find a more complex way of doing that which would be more efficient.

Thanks again...

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
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...