I'm busy translating a few programs to PureBasic and would like to post some tiny bits of them for testing
and in case you can use any of it.
These three procedures are meant to be 'file type aware', REM/UNREM in the event handler to check out
each of them. The 'docs' are to be found in the remarks in the procedures themselves.

Code: Select all
;FILESYSTEM PROCEDURES
;DECLARATIONS
Declare GetDirEntries(Path.s,Ext.s,Dirs,Excl)
Declare GetAllDirEntries(Path.s,Ext.s,I,N,Dirs,Excl)
Declare FindFileTypesInDir(Path.s,Ext.s,Excl)
Declare GetDirsWithFileTypes(Path.s,Ext.s,Excl)
Declare.s GetAppDir()
#MaxDirEntries=128000
Dim DirEntries.s(#MAxDirEntries)
Procedure GetDirEntries(Path.s,Ext.s,Dirs,Excl)
;Fills in the array DirEntries with directory entries in path.
;Lists all(Ext.s="") or some(Ext.s=".rtf;.htm;" etc.) filetypes.
;Includes directories if Dirs=1.
;Excludes hidden and system entries if Excl=1.
If ExamineDirectory(0,Path.s,"*.*")
Repeat
WindowEvent()
FileType=NextDirectoryEntry(1)
If FileType
Name.s=DirectoryEntryName(1)
Attr=DirectoryEntryAttributes(1)
Incl=1
If Excl And (Attr&#PB_FileSystem_Hidden)|(Attr&#PB_FileSystem_System)
;Hidden or system file, do not include
Incl=0
EndIf
If Incl
;Entry attributes ok, entry eligible for inclusion
If FileType=2
;The entry is a directory
If Dirs And Name.s<>"." And Name.s<>".."
;To be included and is not a dot entry
Shared DirEntries()
DirEntries.s(I)=Path.s+Name.s+"\"
I+1
DirEntries.s(I)=""
EndIf
Else
;The entry is a file
If Ext.s
;List files with some extensions
FileExt.s="."+GetExtensionPart(Name.s)+";"
If Len(FileExt.s) And FindString(LCase(Ext.s),LCase(FileExt),1)
DirEntries.s(I)=Path.s+Name.s
I+1
EndIf
Else
;List files with all extensions
DirEntries.s(I)=Path.s+Name.s
I+1
DirEntries.s(I)=""
EndIf
EndIf
EndIf
EndIf
Until FileType=0
EndIf
ProcedureReturn I
EndProcedure
Procedure GetAllDirEntries(Path.s,Ext.s,I,N,Dirs,Excl)
;Fills in the array DirEntries with directory entries in path
;and in subdirectories of path.
;Lists all(Ext.s="") or some(Ext.s=".rtf;.htm;" etc.) filetypes.
;Includes directories if Dirs=1.
;Excludes hidden and system entries if Excl=1.
If ExamineDirectory(N,Path.s,"*.*")
Repeat
WindowEvent()
FileType=NextDirectoryEntry(N)
If FileType
Name.s=DirectoryEntryName(N)
Attr=DirectoryEntryAttributes(N)
Incl=1
If Excl And (Attr&#PB_FileSystem_Hidden)|(Attr&#PB_FileSystem_System)
;Hidden or system file, do not include
Incl=0
EndIf
If Incl
;Entry attributes ok, entry eligible for inclusion
If FileType=2
;The entry is a directory
If Name.s<>"." And Name.s<>".."
;Open it
I=GetAllDirEntries(Path.s+Name.s+"\",Ext.s,I,N+1,Dirs,Excl)
;Returned, use the current directory
; UseDirectory(N)
If Dirs
;Include directory entries
Shared DirEntries()
DirEntries.s(I)=Path.s+Name.s+"\"
I+1
DirEntries.s(I)=""
EndIf
EndIf
Else
;The entry is a file
If Ext.s
;List files with some extensions
FileExt.s="."+GetExtensionPart(Name.s)+";"
If Len(FileExt.s) And FindString(LCase(Ext.s),LCase(FileExt),1)
DirEntries.s(I)=Path.s+Name.s
I+1
DirEntries.s(I)=""
EndIf
Else
;List files with all extensions
DirEntries.s(I)=Path.s+Name.s
I+1
DirEntries.s(I)=""
EndIf
EndIf
EndIf
EndIf
Until FileType=0
EndIf
ProcedureReturn I
EndProcedure
Procedure GetDirsWithFileTypes(Path.s,Ext.s,Excl)
;Fills in the array DirEntries with directory entries in path
;that contains filetypes listed in Ext.s(Ext.s=".rtf;.htm;" etc.).
;Excludes hidden and system entries if Excl=1.
If ExamineDirectory(N,Path.s,"*.*")
Repeat
FileType=NextDirectoryEntry(n)
If FileType
Name.s=DirectoryEntryName(n)
Attr=DirectoryEntryAttributes(n)
Incl=1
If Excl And (Attr&#PB_FileSystem_Hidden)|(Attr&#PB_FileSystem_System)
;Hidden or system file, do not include
Incl=0
EndIf
If Incl
;Entry attributes ok, entry eligible for inclusion
If FileType=2
;The entry is a directory
If Name.s<>"." And Name.s<>".."
;Open it and look for some filetypes
If FindFileTypesInDir(Path.s+Name.s+"\",Ext.s,N+1)
;Found a filetype in or under it
Shared DirEntries()
DirEntries(I)=Path.s+Name.s+"\"
I+1
DirEntries.s(I)=""
EndIf
EndIf
Else
;The entry is a file
;Store it ????
EndIf
EndIf
EndIf
Until FileType=0
EndIf
ProcedureReturn I
EndProcedure
Procedure FindFileTypesInDir(Path.s,Ext.s,N)
;Searches Path.s recursively and returns 1 if it contains
;filetypes listed in Ext.s(Ext.s=".rtf;.htm;" etc.).
;Called by GetDirsWithFileTypes() to probe for filetypes.
If ExamineDirectory(N,Path.s,"")
Repeat
FileType=NextDirectoryEntry(n)
If FileType
Name.s=DirectoryEntryName(n)
If FileType=2
;The entry is a directory
If Name.s<>"." And Name.s<>".."
;Open it and look for some filetypes
If FindFileTypesInDir(Path.s+Name.s+"\",Ext.s,N+1)
;Found, unwind
ProcedureReturn 1
EndIf
EndIf
Else
;The entry is a file
FileExt.s="."+GetExtensionPart(Name.s)+";"
If Len(FileExt.s) And FindString(LCase(Ext.s),LCase(FileExt),1)
;Found, unwind
ProcedureReturn 1
EndIf
EndIf
EndIf
Until FileType=0
EndIf
ProcedureReturn 0
EndProcedure
Procedure.s GetAppDir()
;Returns the current application directory
AppDir.s=Space(255)
GetCurrentDirectory_(255,@AppDir.s)
If Right(AppDir.s,1)<>"\"
AppDir.s+"\"
EndIf
ProcedureReturn AppDir.s
EndProcedure
;FILESYSTEM PROCEDURES END
;FILESYSTEM PROCEDURES TESTPROGRAM
;CONTROL ID'S
Enumeration
#IDWinMain
#IDEdtDir
#IDBtnBrowse
#IDLstDir
EndEnumeration
;MAIN WINDOW
Flags=#PB_Window_SystemMenu|#PB_Window_ScreenCentered
OpenWindow(#IDWinMain,0,0,800,400,"File System Procedures",Flags)
;MAIN WINDOW CONTROLS
StringGadget(#IDEdtDir,10,10,710,25,Path.s)
ButtonGadget(#IDBtnBrowse,730,10,60,25,"Browse")
ListViewGadget(#IDLstDir,10,45,780,345)
Path.s=GetAppDir()
;EVENT LOOP
Repeat
EventID=WaitWindowEvent()
Select EventID
Case #PB_Event_CloseWindow
Exit=1
Case #PB_Event_Gadget
Select EventGadget()
Case #IDBtnBrowse
Path.s=PathRequester("",Path.s)
If Path.s
SetGadgetText(#IDEdtDir,Path.s)
I=0:N=0
; Unrem to test procedure #1
; GetDirEntries(Path.s,"",0,1)
; Unrem to test procedure #2
GetAllDirEntries(Path.s,".txt;.rtf;.htm;.pb;",I,N,0,1)
; Unrem to test procedure #3
; GetDirsWithFileTypes(Path.s,".lnk;.url;",1)
I=0
While DirEntries.s(I)
WindowEvent()
AddGadgetItem(#IDLstDir,-1,DirEntries.s(I))
I+1
Wend
AddGadgetItem(#IDLstDir,-1,Str(I)+" entries found.")
EndIf
EndSelect
EndSelect
Until Exit
End