Описание функции


GetFileList

Поиск файлов в папке включая подкаталоги.

GetFileList(StartDirectory.s, List Lfiles.file(), Pattern.s="*.*", Recursive=1)

Параметры

StartDirectory.s Путь к папке
Lfiles.file() Список, куда будет помещён список файлов
Pattern.s Маска/фильтр файлов в формате "*.*" или "*.txt|*.ini"
Recursive Флаг рекурсивного поиска, если =1 то поиск включая подпапки, а если =0, то поиск только в корне текущей папки.

Возвращаемое значение

Возвращает список файлов List().

Пример


; idle
; https://www.purebasic.fr/english/viewtopic.php?t=51878

EnableExplicit

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
    #Cdir = "\"
CompilerElse
    #Cdir = "/"
CompilerEndIf

Structure FileDate
    Created.i
    Modified.i
    Accessed.i
EndStructure

Structure File
    Name.s
    Attributes.i
    Date.FileDate
    Size.q
EndStructure

Procedure GetFileList(StartDirectory.s, List Lfiles.file(), Pattern.s = "*.*", Recursive = 1)
    Protected PatternCount, Depth, a, CurrentDirectoryID, Directory.s, TempDirectory.s
    Protected FileAttributes.i, FileSize.i, FileDate.FileDate, FileName.s, FullFileName.s

    Static NewList Lpattern.s()
    Static PatternSet, FileCount

    If Not PatternSet
        Pattern = RemoveString(Pattern, "*.")
        PatternCount = CountString(Pattern, "|") + 1
        ClearList(lpattern())
        For a = 1 To PatternCount
            AddElement(Lpattern())
            Lpattern() = UCase(StringField(Pattern, a, "|"))
        Next
        PatternSet = 1
    ElseIf depth = 0
        PatternSet = 0
    EndIf

    CurrentDirectoryID = ExamineDirectory(#PB_Any, StartDirectory, "*.*")
    If CurrentDirectoryID
        While NextDirectoryEntry(CurrentDirectoryID)
            If DirectoryEntryType(CurrentDirectoryID) = #PB_DirectoryEntry_File
                Directory = StartDirectory
                FileName = DirectoryEntryName(CurrentDirectoryID)
                FileDate\Created = DirectoryEntryDate(CurrentDirectoryID, #PB_Date_Created)
                FileDate\Modified = DirectoryEntryDate(CurrentDirectoryID, #PB_Date_Modified)
                FileDate\Accessed = DirectoryEntryDate(CurrentDirectoryID, #PB_Date_Accessed)
                FileAttributes = DirectoryEntryAttributes(CurrentDirectoryID)
                FileSize = DirectoryEntrySize(CurrentDirectoryID)

                ForEach Lpattern()
                    If lpattern() = "*" Or GetExtensionPart(UCase(FileName)) = lpattern()
                        FullFileName.s = StartDirectory + FileName
                        AddElement(LFiles())
                        Lfiles()\Name = FullFileName
                        Lfiles()\Date = FileDate
                        Lfiles()\Size = FileSize
                        Lfiles()\Attributes = FileAttributes
                        FileCount + 1
                    EndIf
                Next

            Else
                TempDirectory = DirectoryEntryName(CurrentDirectoryID)
                If TempDirectory <> "." And TempDirectory <> ".."
                    If Recursive = 1
                        Depth + 1
                        GetFileList(StartDirectory + TempDirectory + #Cdir, LFiles(), Pattern, Recursive)
                    EndIf
                EndIf
            EndIf
        Wend
        FinishDirectory(CurrentDirectoryID)
    EndIf

    ProcedureReturn FileCount

EndProcedure

Procedure SortFileListByDate(List InputFiles.file(), List OutPutFiles.file(), Order = #PB_Sort_Ascending, DateOption = #PB_Date_Modified, StartDate = 0, EndDate = $7FFFFFFF)

    Select DateOption
        Case #PB_Date_Modified
            SortStructuredList(InputFiles(), Order, (OffsetOf(File\Date) + OffsetOf(FileDate\Modified)), #PB_Integer)
        Case #PB_Date_Accessed
            SortStructuredList(InputFiles(), Order, (OffsetOf(File\Date) + OffsetOf(FileDate\Accessed)), #PB_Integer)
        Case #PB_Date_Created
            SortStructuredList(InputFiles(), Order, (OffsetOf(File\Date) + OffsetOf(FileDate\Created)), #PB_Integer)
    EndSelect

    If StartDate
        ForEach InputFiles()
            Select DateOption
                Case #PB_Date_Modified
                    If (InputFiles()\Date\Modified >= StartDate And InputFiles()\Date\Modified <= EndDate)
                        AddElement(OutPutFiles())
                        CopyStructure(@InputFiles(), @OutPutFiles(), File)
                    EndIf
                Case #PB_Date_Accessed
                    If (InputFiles()\Date\Modified >= StartDate And InputFiles()\Date\Accessed <= EndDate)
                        AddElement(OutPutFiles())
                        CopyStructure(@InputFiles(), @OutPutFiles(), File)
                    EndIf
                Case #PB_Date_Created
                    If (InputFiles()\Date\Modified >= StartDate And InputFiles()\Date\Created <= EndDate)
                        AddElement(OutPutFiles())
                        CopyStructure(@InputFiles(), @OutPutFiles(), File)
                    EndIf
            EndSelect
        Next
    EndIf

    ProcedureReturn ListSize(OutPutFiles())

EndProcedure

Procedure SortFileListBySize(List InputFiles.file(), List OutPutFiles.file(), Order = #PB_Sort_Ascending, MinimumSize = 0, MaximumSize.q = $7FFFFFFFFFFFFFFF)

    SortStructuredList(InputFiles(), Order, OffsetOf(File\Size), #PB_Integer)
    If MinimumSize
        ForEach InputFiles()
            If (InputFiles()\Size >= MinimumSize And InputFiles()\Size <= MaximumSize)
                AddElement(OutPutFiles())
                CopyStructure(@InputFiles(), @OutPutFiles(), File)
            EndIf
        Next
    EndIf

    ProcedureReturn ListSize(OutPutFiles())

EndProcedure

Global NewList AllFiles.File()
Global NewList FilteredFiles.File()
Global NewList ReFilteredFiles.File()
Global StartDate = Date(2014, 1, 1, 0, 0, 0)
Global EndDate = Date(2022, 12, 31, 23, 59, 59)
Global path.s = #PB_Compiler_Home


If GetFileList(path, AllFiles(), "*.pb|*.pbi") ;get all pb files in the directory recursively

    If SortFileListByDate(Allfiles(), FilteredFiles(), #PB_Sort_Ascending, #PB_Date_Modified, StartDate, EndDate) ;sort and filter by date between dates
        ForEach FilteredFiles()
            Debug FilteredFiles()\Name
            Debug FormatDate("%dd/%mm/%yyyy", FilteredFiles()\Date\Modified)
        Next
    EndIf

    Debug "++++++++++++++++++++++++++++++++++++++++++++++++++"

    If SortFileListBySize(FilteredFiles(), ReFilteredFiles(), #PB_Sort_Descending, 10000, 100000) ;re-sort and filter by size between sizes
        ForEach ReFilteredFiles()
            Debug ReFilteredFiles()\Name
            Debug Str(ReFilteredFiles()\Size / 1024) + " KB"
        Next
    EndIf

EndIf