Jump to content

Folders and subfolders list


Recommended Posts

I found this code on this forum and modified it but it is not working for me.

In MsgBox i want to show the folder name and path but its showing only path not name.

_RecFileFinder(@ScriptDir & "\", "_Found", "*", "", 1)

Func _Found($Path, $IsFolder)
    Select
        Case $IsFolder
            $FolderPath = StringSplit($Path,@CRLF,1)
            For $i = 1 To $FolderPath[0]
            $FolderName = StringSplit($FolderPath[$i],"\",1)
            MsgBox(0,"","Folder name: "&@CRLF&$FolderName[0]&@CRLF&"Folder path"&@CRLF&$FolderPath[$i])
            Next
;
    Case Else
    ConsoleWrite($Path & @CRLF)
    EndSelect
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _RecFileFinder
; Description ...: Finds files in a specified path with optional recursion and runs a function.
; Syntax.........: _RecFileFinder($sPath, $sFunction[, $sInclude_List = "*"[, $sExclude_List = ""[, $fRecur = 0]]])
; Parameters ....: $sPath - Initial path
; $sFunction - Function to call when file found
; $sInclude_List - Optional: the filter for included results (default is "*"). Multiple filters must be separated by ";"
; $sExclude_List - Optional: the filter for excluded results (default is ""). Multiple filters must be separated by ";"
; $fRecur - Optional: specifies whether to search in subfolders
; |$fRecur= 0 (Default) Do not search in subfolders
; |$fRecur= 1 Search in subfolders
; Requirement(s).: v3.3.1.1 or higher
; Return values .: Success: 1
; Failure: 0 and @error = 1 with @extended set as follows:
; |1 = Path not found or invalid
; |2 = Invalid $sInclude_List
; |3 = Invalid $sExclude_List
; |4 = Invalid $fRecur
; Author ........: Melba23 using SRE code from forums
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......; Yes
; ===============================================================================================================================
Func _RecFileFinder($sPath, $sFunction, $sInclude_List = "*", $sExclude_List = "", $fRecur = 0)

    Local $asFolderList[3] = [1], $sInclude_List_Mask, $sExclude_List_Mask
    Local $sCurrentPath, $hSearch, $sReturnPath = "", $sName, $fFolder

    ; Check valid path
    If Not FileExists($sPath) Then Return SetError(1, 1, "")
    ; Ensure trailing \
    If StringRight($sPath, 1) <> "\" Then $sPath = $sPath & "\"
    ; Add path to folder list
    $asFolderList[1] = $sPath

    ; Determine Filter mask for SRE check
    If StringRegExp($sInclude_List, "\\|/|:|\<|\>|\|") Then Return SetError(1, 2, "") ; Check for invalid characters
 $sInclude_List = StringReplace(StringStripWS(StringRegExpReplace($sInclude_List, "\s*;\s*", ";"), 3), ";", "|") ; Strip WS and swap :/|
 $sInclude_List_Mask = "(?i)^" & StringReplace(StringReplace(StringReplace($sInclude_List, ".", "\."), "*", ".*"), "?", ".") & "\z" ; Convert to SRE pattern

    ; Determine Exclude mask for SRE check
    If $sExclude_List = "" Then
    $sExclude_List_Mask = ":" ; Set unmatchable mask
    Else
    If StringRegExp($sExclude_List, "\\|/|:|\<|\>|\|") Then Return SetError(1, 3, "") ; Check for invalid characters
 $sExclude_List = StringReplace(StringStripWS(StringRegExpReplace($sExclude_List, "\s*;\s*", ";"), 3), ";", "|") ; Strip WS and swap ;/|
 $sExclude_List_Mask = "(?i)^" & StringReplace(StringReplace(StringReplace($sInclude_List, ".", "\."), "*", ".*"), "?", ".") & "\z" ; Convert to SRE pattern
    EndIf

    ; Verify other parameter values
    If Not ($fRecur = 0 Or $fRecur = 1) Then Return SetError(1, 4, "")

    ; Search in listed Folders
    While $asFolderList[0] > 0

    ; Set path to search
    $sCurrentPath = $asFolderList[$asFolderList[0]]
    ; Reduce folder array count
    $asFolderList[0] -= 1
    ; Get search handle
    $hSearch = FileFindFirstFile($sCurrentPath & "*")
    ; If folder empty move to next in list
    If $hSearch = -1 Then ContinueLoop

    ; Search folder
    While 1
    $sName = FileFindNextFile($hSearch)
    ; Check for end of folder
    If @error Then ExitLoop
    ; Check for subfolder - @extended set in 3.3.1.1 +
    $fFolder = @extended
    ;$fFolder = StringInStr(FileGetAttrib($sCurrentPath & $sName), "D") ; pre 3.3.1.1

    ; If recursive search, add subfolder to folder list
    If $fRecur And $fFolder Then
    ; Increase folder array count
    $asFolderList[0] += 1
    ; Double folder array size if too small (fewer ReDim needed)
    If UBound($asFolderList) <= $asFolderList[0] + 1 Then ReDim $asFolderList[UBound($asFolderList) * 2]
    ; Add subfolder to list
    $asFolderList[$asFolderList[0]] = $sCurrentPath & $sName & "\"
    Call($sFunction, $sCurrentPath & $sName, 1)
    EndIf

    ; Check file/folder type against required return value and file/folder name against Include/Exclude masks
    If Not $fFolder And StringRegExp($sName, $sInclude_List_Mask) And Not StringRegExp($sName, $sExclude_List_Mask) Then

    ;This is where you can do what you want with the found files
    Call($sFunction, $sCurrentPath & $sName, 0)

    EndIf
    WEnd

    ; Close current search
    FileClose($hSearch)

    WEnd

EndFunc ;==>_RecFileFinder

Any help will be appericiated.

Edited by NicePerson
Link to comment
Share on other sites

Any other way to do what? The function/code that you posted is not long at all. If you mean the suggestions that I made are too long then I think you may have more issues than I can address. Maybe copy/paste?

Link to comment
Share on other sites

You could use one of the more generic recursive FLTA routines floating around, that will split out just the folders and also return the full name for you. It ought to execute faster, may seem simpler in that the function does more of the work for you, it's not particularily smaller:

#include <Array.au3>

$folderarray = _FileListToArray_Recursive(@ScriptDir, "*", 2, 2, True)
_ArrayDisplay($folderarray)

;===============================================================================
; $iRetItemType: 0 = Files and folders, 1 = Files only, 2 = Folders only
; $iRetPathType: 0 = Filename only, 1 = Path relative to $sPath, 2 = Full path/filename
Func _FileListToArray_Recursive($sPath, $sFilter= "*", $iRetItemType = 0, $iRetPathType = 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 $iRetPathType
            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 ($iRetItemType + @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

typo

Edited by Spiff59
Link to comment
Share on other sites

  • Moderators

NicePerson,

this code in my first post is so long

If you think that function is long, do not even think of opening many of the include files or some the other UDFs posted around here! :)

Remember, the whole point of UDFs and functions like that is that you can use them without having to worry too much about how they do what they do. Although I would encourage you to learn if you can - it will help when you need to amend the results to fit your particular needs as was the case here. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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