WString Array of File or Folder List

Started by Theo Gottwald, November 26, 2023, 05:51:58 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Theo Gottwald

Ever needed to find all files matching a specific pattern in your root folder AND subfolders? 📂🔍 It can be a bit tricky, but here's how you can do it! ⚙️

1️⃣ First, you'll need to use a few procedures to get the job done.
2️⃣ Remember, the resulting list won't be sorted. 📋
3️⃣ This method matches one mask at a time - if you need multiple masks, you'll have to use a temporary array. 🎭➕

🚀 Ready to try it out? Make sure to set up the parameters in the Test/Sample app according to your hard drive's specifics - there are too many variations to have a one-size-fits-all. 🛠�💾

#TechTips #FileManagement #Coding #SoftwareDevelopment #Programming #TechHacks #ComputerScience #DevCommunity #CodeNewbie #TechTwitter 🖥�💻👨�💻👩�💻🔧🎯📊🗂�✨



Part I: Include File
'FileFolder.inc
'Public domain, use at own risk. SDurhan

#If Not %Def(%FileFolder231101)
    %FileFolder231101 = 1
    'get list of folders in root folder
    'get list of files in root folder matching mask
    'get list of all folders in root folder and sub-folders, including root folder
    'get all files in root folder, and subfolders, matching mask
    '---------------------------------------------------------------------------------
    Sub FileFolder_Folders(list() As WString, ByVal rootfolder As WString)
        'get list of folders in root folder

        Local folder, foldermask, rootpath As WString
        Local DrD As DirData

        Erase list()
        If IsFalse IsFolder(rootfolder) Then Exit Sub

        rootpath = RTrim$(rootfolder, "\") + "\"
        foldermask = rootpath
        folder = Dir$(foldermask, Only %SubDir To DrD)
        While Len(folder)
            FileFolder_Push list(), rootpath + folder
            folder = Dir$
        Wend
    End Sub
    '---------------------------------------------------------------------------------
    Sub FileFolder_Files(list() As WString, ByVal rootfolder As WString, ByVal mask As WString)
        'get list of files in root folder matching mask

        Local folder, file, filemask As WString

        Erase list()
        If IsFalse IsFolder(rootfolder) Then Exit Sub

        folder = RTrim$(rootfolder, "\") + "\"
        filemask = folder + mask
        file = Dir$(filemask)
        While Len(file)
            FileFolder_Push list(), folder + file
            file = Dir$
        Wend
    End Sub
    '---------------------------------------------------------------------------------
    Sub FileFolder_AllFolders(list() As WString, ByVal rootfolder As WString)
        'get list of all folders in root folder and sub-folders, including root folder

        Local folders(), subfolders(), currentfolder As WString

        Erase list()
        If IsFalse IsFolder(rootfolder) Then Exit Sub

        'push root folder on stack
        FileFolder_Push folders(), rootfolder
        While UBound(folders) > 0
            currentfolder = FileFolder_Pop(folders())
            'add current folder to list
            FileFolder_Push list(), currentfolder
            'get folders in current folder
            FileFolder_Folders subfolders(), currentfolder
            'push folders on stack
            While UBound(subfolders) > 0
                FileFolder_Push folders(), FileFolder_Pop(subfolders())
            Wend
        Wend
    End Sub
    '---------------------------------------------------------------------------------
    Sub FileFolder_AllFiles(list() As WString, ByVal rootfolder As WString, ByVal mask As WString)
        'get all files in root folder, and subfolders, matching mask

        Local folders(), files() As WString

        Erase list()
        If IsFalse IsFolder(rootfolder) Then Exit Sub

        FileFolder_AllFolders folders(), rootfolder
        While UBound(folders) > 0
            'get files for each folder
            FileFolder_Files files(), FileFolder_Pop(folders()), mask
            While UBound(files) > 0
                'add each file to list
                FileFolder_Push list(),  FileFolder_Pop(files())
            Wend
        Wend
    End Sub
    '---------------------------------------------------------------------------------
    '   PRIVATE
    '---------------------------------------------------------------------------------
    Sub FileFolder_Push(a() As WString, ByRef value As WString) Private
        'add value to end of array

        Local items As Long

        items = UBound(a) + 1
        If items < 1 Then items = 1
        ReDim Preserve a(1 To items)
        a(items) = value
    End Sub
    '---------------------------------------------------------------------------------
    Function FileFolder_Pop(a() As WString) Private As WString
        'get and remove last value in array

        Local items As Long

        items = UBound(a)
        If items > 0 Then
            Function = a(items)
            Decr items
            If items > 0 Then
                ReDim Preserve a(1 To items)
            Else
                Erase a()
            End If
        End If
    End Function
    '---------------------------------------------------------------------------------
#EndIf '%FileFolder231101


Part II: Test Code

'FileFolder.bas
#Option LargeMem32
#Dim All
#Compile Exe "FileFolder.ext"
#Include Once "WIN32API.INC"

#Include Once "FileFolder.inc"

%TextBox = 101
%BtnID = 102
Global gDlg As Long

Sub SS(ByVal value As WString)
    'appends without the overhead of getting the text
    Local characterCount As Long
    Local hWin As Long : hWin = GetDlgItem(gDlg, %TextBox)
    value += $CrLf
    characterCount =  SendMessageW(hWin, %WM_GETTEXTLENGTH, 0, 0)
    SendMessageW(hWin, %EM_SETSEL, characterCount, characterCount)
    SendMessageW(hWin, %EM_REPLACESEL, 1, StrPtr(value))
End Sub

Sub SampleCode()
    Local i As Long
    Local list() As WString

    SS ""
    SS "---------------------------------------------------------"
    SS "Sub FileFolder_Folders(list() As WString, ByVal rootfolder As WString)"
    SS "    get list of folders in root folder"
    FileFolder_Folders list(), ""
    SS "---------------------------------------------------------"
    For i = 1 To UBound(list)
        SS list(i)
    Next i
    SS "---------------------------------------------------------"

    SS ""
    SS ""
    SS "---------------------------------------------------------"
    SS "Sub FileFolder_Files(list() As WString, ByVal rootfolder As WString, ByVal mask As WString)"
    SS "    get list of files in root folder matching mask"
    FileFolder_Files list(), "", ""
    SS "---------------------------------------------------------"
    For i = 1 To UBound(list)
        SS list(i)
    Next i
    SS "---------------------------------------------------------"

    SS ""
    SS ""
    SS "---------------------------------------------------------"
    SS "Sub FileFolder_AllFolders(list() As WString, ByVal rootfolder As WString)"
    SS "    get list of all folders in root folder and sub-folders, including root folder"
    FileFolder_AllFolders list(), ""
    SS "---------------------------------------------------------"
    For i = 1 To UBound(list)
        SS list(i)
    Next i
    SS "---------------------------------------------------------"

    SS ""
    SS ""
    SS "---------------------------------------------------------"
    SS "Sub FileFolder_AllFiles(list() As WString, ByVal rootfolder As WString, ByVal mask As WString)"
    SS "    get all files in root folder, and subfolders, matching mask"
    FileFolder_AllFiles list(), "", ""
    For i = 1 To UBound(list)
        SS Format$(i) + "    " + list(i)
    Next i
    SS "---------------------------------------------------------"

    SS ""
    SS ""

End Sub

Function PBMain()
    Dialog Default Font "consolas", 12, 0, 0
    Dialog New 0, Exe.Name$, 0, 0, 0, 0, %WS_Caption Or %WS_ClipSiblings Or %WS_MinimizeBox Or %WS_SysMenu Or %WS_ThickFrame, %WS_Ex_AppWindow To gDlg
    Control Add TextBox, gDlg, %TextBox, "", 0, 0, 0, 0, %ES_AutoHScroll Or %ES_AutoVScroll Or %ES_MultiLine Or %ES_NoHideSel Or %ES_WantReturn Or %WS_HScroll Or %WS_VScroll Or %WS_Border, 0
    Control Add Button,  gDlg, %BtnID, "Run", 275, 220, 60, 15, %BS_Flat, 0
    Dialog Show Modeless gDlg, Call DlgCB
    Do
        Dialog DoEvents
    Loop While IsWin(gDlg)
End Function

CallBack Function DlgCB()
    Select Case As Long Cb.Msg
        Case %WM_InitDialog
            WM_InitDialog()
        Case %WM_Size
            WM_Size()
        Case %WM_Command
            Select Case As Long Cb.Ctl
                Case %BtnID : If Cb.CtlMsg = %BN_Clicked Then SampleCode()
            End Select
    End Select
End Function

Sub WM_InitDialog()
    Local clientW, clientH As Long
    Desktop Get Client To clientW, clientH
    clientW /= 7
    clientH /= 3
    Dialog Set Loc gDlg, clientW, clientH / 6
    Dialog Set Size gDlg, clientW, clientH
    SendMessageW(GetDlgItem(gDlg, %TextBox), %EM_SETLIMITTEXT, 4000000, 0)
End Sub

Sub WM_Size()
    Local clientW, clientH As Long
    Local marg As Long
    Local buttonW, buttonH As Long
    Local txtWidth, txtHeight As Long
    Local fromLeft, fromBottom As Long
    Dialog Get Client gDlg To clientW, clientH
    marg = 3 : buttonW = 25 : buttonH = 10
    fromLeft = clientW - marg - buttonW
    fromBottom = clientH - marg - buttonH
    Control Set Size gDlg, %BtnID, buttonW, buttonH
    Control Set Loc gDlg, %BtnID, fromLeft, fromBottom
    txtWidth = clientW - marg - marg
    txtHeight = clientH - marg - buttonH - marg - marg
    Control Set Size gDlg, %TextBox, txtWidth, txtHeight
    Control Set Loc gDlg, %TextBox, marg, marg
End Sub

Source: [url=https://forum.powerbasic.com/forum/user-to-user-discussions/source-code/828076-wstring-array-of-file-or-folder-list]Powerbasic Forum[/url]