Jump to content

How to get recursive directory listing


leuce
 Share

Recommended Posts

G'day everyone

I need to write a script that performs a certain action (or actions) on a certain set of files. These files are located in a directory tree (multiple subfolders and subsubfolders etc). I can easily identify these files by their file extension. In other for the script to work, however, I need AutoIt to search a given directory and get me the full paths (or full up to the top folder) of these files, so that I can tell the script to do other stuff with these files.

I can get such a list outside of AutoIt, of course. The easiest way is to do a search for those files on the uppermost folder, then select all the results, and then use PikyBasket (or similar tool) to "copy paths to clipboard". This requires some manual work, however, and I was hoping to be able to do this directly in AutoIt.

Can anyone please tell me how to do a recursive directory search using AutoIt?

Thanks

Samuel

Link to comment
Share on other sites

  • Moderators

G'day everyone

I need to write a script that performs a certain action (or actions) on a certain set of files. These files are located in a directory tree (multiple subfolders and subsubfolders etc). I can easily identify these files by their file extension. In other for the script to work, however, I need AutoIt to search a given directory and get me the full paths (or full up to the top folder) of these files, so that I can tell the script to do other stuff with these files.

I can get such a list outside of AutoIt, of course. The easiest way is to do a search for those files on the uppermost folder, then select all the results, and then use PikyBasket (or similar tool) to "copy paths to clipboard". This requires some manual work, however, and I was hoping to be able to do this directly in AutoIt.

Can anyone please tell me how to do a recursive directory search using AutoIt?

Thanks

Samuel

Search for _FileListToArrayEx and also search Randallc's signature.

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

  • Moderators

G'day everyone

I need to write a script that performs a certain action (or actions) on a certain set of files. These files are located in a directory tree (multiple subfolders and subsubfolders etc). I can easily identify these files by their file extension. In other for the script to work, however, I need AutoIt to search a given directory and get me the full paths (or full up to the top folder) of these files, so that I can tell the script to do other stuff with these files.

I can get such a list outside of AutoIt, of course. The easiest way is to do a search for those files on the uppermost folder, then select all the results, and then use PikyBasket (or similar tool) to "copy paths to clipboard". This requires some manual work, however, and I was hoping to be able to do this directly in AutoIt.

Can anyone please tell me how to do a recursive directory search using AutoIt?

Thanks

Samuel

Search for _FileListToArrayEx and also search Randallc's signature.

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

Func recursiv_folder($folder)
    $foldere = _FileListToArray($folder, "*.*", 2)
    If Not @error Then
        For $i = 1 To $foldere[0]
            recursiv_folder($folder & "\" & $foldere[$i])
        Next
    EndIf
EndFunc  ;==>recursiv_folder

Link to comment
Share on other sites

  • Moderators

Func recursiv_folder($folder)
    $foldere = _FileListToArray($folder, "*.*", 2)
    If Not @error Then
        For $i = 1 To $foldere[0]
            recursiv_folder($folder & "\" & $foldere[$i])
        Next
    EndIf
EndFunc ;==>recursiv_folder
Did you try that on a directory that had more than 400 files?

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

Did you try that on a directory that had more than 400 files?

I did it is quite heavy. this is the reason why i modified the function to make it slightly faster.

CODE

Func DirListRecursive($sSearchPath, $sSearchPattern, $iMaxRecurseLevel, $sWorkerFunction)

Local $retVal

Local $nFilesFound = 0

Local $debug_prefix = "_FileChangeRecursive: "

;#== If the max recursion level < 0 (e.g. -1) then, we set an insane high

If $iMaxRecurseLevel < 0 Then

$iMaxRecurseLevel = 0xFFFFFFF

EndIf

Local $dirs = _FileListToArray($sSearchPath, "*", 2)

If @error > 0 And @error < 4 Then

SetError(@error, @extended)

Return -1

EndIf

_ArraySort($dirs, 0, 1)

If IsArray($dirs) Then

For $n = 1 To $dirs[0]

$retval = Call($sWorkerFunction, $sSearchPath, $dirs[$n])

$nFilesFound += 1

Next

EndIf

;#== Now recursively walk through all directories

If IsArray($dirs) Then

For $n = 1 To $dirs[0]

$__glb_FileChangeRecursive_current_recurse_level += 1

If $__glb_FileChangeRecursive_current_recurse_level <= $iMaxRecurseLevel Then

$retval = DirListRecursive($sSearchPath & "\" & $dirs[$n], $sSearchPattern, $iMaxRecurseLevel, $sWorkerFunction)

$__glb_FileChangeRecursive_current_recurse_level -= 1

Else

$__glb_FileChangeRecursive_current_recurse_level -= 1

EndIf

Next

EndIf

Return $nFilesFound ;**** return

EndFunc ;==>DirListRecursive

you can try i definetly had performence improvements
Link to comment
Share on other sites

Here's one I've been using for a long time now. Just set the starting folder ($szRoot) to the top level

Func _FldrSearch($szRoot, $nFlag = 1 )
    If StringRight($szRoot, 1) <> '\' Then $szRoot &= '\'
    Local $szReturn = '',  $szBuffer = '', $szPathlist = '*', $oRoot = $szRoot & '*'
        $Hfile = FileFindFirstFile ($szRoot &'*')
        If $Hfile >= 0 Then
            $szBuffer = FileFindNextFile ($Hfile)
            While NOT @Error
                If Not StringInStr($szBuffer, '.') Then $szPathlist &= $szRoot & $szBuffer & "\*"
                $szBuffer = FileFindNextFile ($Hfile)
            Wend
            FileClose ($Hfile)
        EndIf
        $szReturn = $szPathList

    If $nFlag = 1 Then
        $szPathList = StringTrimLeft($szPathlist, 1)
        $szRoot = StringLeft($szPathList, StringInStr($szPathlist, '*') -1)
        While 1
            $hFile = FileFindFirstFile ($szRoot & '*')
            If $hFile >= 0 Then
                $szBuffer = FileFindNextFile ($Hfile)
            While NOT @Error
                If Not StringInStr($szBuffer, '.') Then
                    $szPathlist &= $szRoot & $szBuffer & "\*"
                    $szReturn &= $szRoot & $szBuffer & "\*"
                EndIf
                $szBuffer = FileFindNextFile ($Hfile)
            Wend
            FileClose($hFile)
            $szPathList = StringReplace($szPathList, $szRoot, '')
            EndIf
            If $szPathList == '*' Then ExitLoop
            $szPathlist = StringTrimLeft ($szPathlist, 1)
            $szRoot = StringLeft ($szPathlist, StringInStr ($szPathlist, "*") - 1) & "\"
            $szPathlist = StringTrimLeft ($szPathlist, StringInStr ($szPathlist, "*") - 1)
        ;$szPathlist = StringReplace($szPathlist, $szRoot, '',1)
        Wend
    EndIf
    If StringLeft($szReturn, 1) = '*' Then $szReturn = StringTrimLeft($szReturn, 1)
    $szReturn = StringReplace($szReturn, '\\', '\')
    If StringRight($szReturn, 1) = '*' Then $szReturn = StringTrimRight($szReturn,1)
    $szReturn = StringSplit($oRoot & $szReturn,'*')
    If $szReturn = '*' or $szReturn = '' Then Return 0
    _ArraySort($szReturn)
    Return $szReturn
EndFunc  ;<==> _FldrSearch($szRoot)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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...