Jump to content

Selecting Folders


Recommended Posts

I'm looking for a UDF or some code that initiates a "Browse for Folder" dialog box and lets you select multiple folders.

It would return the names of the selected folders. In this respect it would be similar to the "FileSelectFolder" function which returns the name of a single selected folder and to the "FileOpenDialog" function which returns the names of multiple selected files (but not folders).

Better yet, for my purposes, would be a UDF that returns the names of all folders within a selected folder.

The list of folders can then be presented for selection in a group of checkboxes.

For me the usage would be as follows...

I am currently writing some scripts that generate monthly activity reports for our clients and it varies from month to month who is going to get a report. So there needs to be a GUI that allows us to choose which clients are to get reports on any given month. I can hardcode the client names into a group of checkboxes, however new clients are always coming along and it would be nice to leave things dynamic. Since each client has it's own folder in our main Clients folder, this can serve as the basis for such a GUI, either a chechbox or a dialog box that works something like FileOpenDialog.

I searched Forums for "selected folders" and for "select folders" and found that some people have asked for such a UDF in the past, but as far as I can tell nothing like this was available at the time.

Do you happen to know if such a UDF exists today? .

If not, perhaps you have some ideas on how this may be accomplished.

Any suggestions would be greatly appreciated. Sample code would be even better.

Link to comment
Share on other sites

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>

Local $LIST


Guicreate("", 500, 400)
$LIST = GUICtrlCreateListView("", 10, 10, 480, 330, "", BitOR($LVS_EX_CHECKBOXES, $LVS_EX_GRIDLINES))
_GUICtrlListView_InsertColumn($LIST , 0, "List of Folders", 450)
GUICtrlCreateLabel("Listing Folders and Subfolders from @ScriptDir", 75, 360, 380)
Guictrlsetfont(-1, 12)
_RecFileFinder(@ScriptDir & "\", "_Found", "*", "", 1)
SortLV()    
Guisetstate()

While 1
    $msg = GUIGetMsg()
        
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
    EndSelect
Wend
            
            
Func _Found($Path, $IsFolder)
    Select
        Case $IsFolder
            ConsoleWrite($Path & "\" & @CRLF)
            GUICtrlCreateListViewItem($Path,  $LIST)
        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


    Func SortLV()
Dim $B_DESCENDING[_GUICtrlListView_GetColumnCount ($LIST) ]
    _GUICtrlListView_SimpleSort($LIST , $B_DESCENDING, 0); GUICtrlGetState(@GUI_CtrlId))
EndFunc

This should help you.

It uses UDF by Melba to list files and folders.

Ajit

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