Jump to content

HELP : File list with limited recursion


Recommended Posts

Okay, bowing again to the superior intellect of all you folks.

I'm trying to build a list of all file in a directory according to various criteria. One of which needs to search only the root and the FIRST level only (So in C:\1\2\3 it would search only 1 & 2, and skip 3)

I've tried a couple of different ways which don't tend to work at all, and was hoping to use _FileListToArray3 as I'm using it elsewhere, but it doesn't look like it's possible to limit the recursion. Any other UDF that may work?

Is it possible?

If not, I figured I'd try excluding files from the 'ignored' depths afterwards, but can't figure out how to do that either, since I don't know what those folders are called.

Skipped posting code, because none of it works, and generally just calls UDF.

Thanks in advance for any nudges/shoves in the right direction.

Link to comment
Share on other sites

This would list all folders and files in a given directory ($Directory) and 1 Level deep of said directory

#include<Array.au3>
$Directory = 'C:'
$MainArray = _FileListToArray_Recursive($Directory, '*', 0, 2)
_ArrayDisplay($MainArray, 'Root of ' & $Directory & ' Only')
For $a = 1 To $MainArray[0]
    If _IsDir($MainArray[$a]) = 'Directory' Then
        Local $SubArray = _FileListToArray_Recursive($MainArray[$a], '*', 0, 2)
        If @error Then ContinueLoop
        For $b = 1 To $SubArray[0]
            _ArrayAdd($MainArray, $SubArray[$b])
        Next
    EndIf
Next
$MainArray[0] = UBound($MainArray) - 1
_ArraySort($MainArray)
_ArrayDisplay($MainArray, '1 Level Deep of ' & $Directory)

Func _IsDir($Dir = '') ;Returns Directory, File, or Bad
    If StringRight($Dir, 1) = '\' Then $Dir = StringTrimRight($Dir, 1)
    If Not FileExists($Dir) Then Return 'Bad'
    If StringInStr(FileGetAttrib($Dir), 'D') <> 0 Then Return 'Directory'
    Return 'File'
EndFunc   ;==>_IsDir

;===============================================================================
; $aRetItemType: 0 = Files and folders, 1 = Files only, 2 = Folders only
; $aRetPathType: 0 = Filename only, 1 = Path relative to $sPath, 2 = Full path/filename
Func _FileListToArray_Recursive($sPath, $sFilter = "*", $aRetItemType = 0, $aRetPathType = 0, $bRecursive = False)
    Local $sRet = "", $sRetPath = ""
    $sPath = StringRegExpReplace($sPath, "[\\/]+\z", "")
    If Not FileExists($sPath) Then Return SetError(1, 1, "")
    If StringRegExp($sFilter, "[\\/ :> <\|]|(?s)\A\s*\z") Then Return SetError(2, 2, "")
    $sPath &= "\|"
    $sOrigPathLen = StringLen($sPath) - 1
    While $sPath
        $sCurrPathLen = StringInStr($sPath, "|") - 1
        $sCurrPath = StringLeft($sPath, $sCurrPathLen)
        $Search = FileFindFirstFile($sCurrPath & $sFilter)
        If @error Then
            $sPath = StringTrimLeft($sPath, $sCurrPathLen + 1)
            ContinueLoop
        EndIf
        Switch $aRetPathType
            Case 1 ; relative path
                $sRetPath = StringTrimLeft($sCurrPath, $sOrigPathLen)
            Case 2 ; full path
                $sRetPath = $sCurrPath
        EndSwitch
        While 1
            $File = FileFindNextFile($Search)
            If @error Then ExitLoop
            If ($aRetItemType + @extended = 2) Then ContinueLoop
            $sRet &= $sRetPath & $File & "|"
        WEnd
        FileClose($Search)
        If $bRecursive Then
            $hSearch = FileFindFirstFile($sCurrPath & "*")
            While 1
                $File = FileFindNextFile($hSearch)
                If @error Then ExitLoop
                If @extended Then $sPath &= $sCurrPath & $File & "\|"
            WEnd
            FileClose($hSearch)
        EndIf
        $sPath = StringTrimLeft($sPath, $sCurrPathLen + 1)
    WEnd
    If Not $sRet Then Return SetError(4, 4, "")
    Return StringSplit(StringTrimRight($sRet, 1), "|")
EndFunc   ;==>_FileListToArray_Recursive

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