Help in modifying search procedure to run in a Thread
Posted: Sat Jan 21, 2023 5:18 am
Hi All, Just wanted to ask for help in modifying the below procedure "SearchDirectory()" to run as a thread.
I found the code in the forum (written by collectordave) and I have been using it to search for photo types specified in the RegEx criteria. The code does a good job in finding files except when searching large (2TB etc) hard drives the GUI freezes. The code still run all the way and control is given back to the GUI at the end. I figured if I ran the search in a thread that will not happen hence the help.
I found the code in the forum (written by collectordave) and I have been using it to search for photo types specified in the RegEx criteria. The code does a good job in finding files except when searching large (2TB etc) hard drives the GUI freezes. The code still run all the way and control is given back to the GUI at the end. I figured if I ran the search in a thread that will not happen hence the help.
Code: Select all
Global NewList FilesFolders.s()
Global SearchFolder.s
Global SearchRegEX.i
#INITIAL_PATH = "c:\"
Procedure SearchDirectory(dir$, List dList.s(), level.l = 0)
Define EntryName.s
NewList FolderList.s()
Static FileCount.i
If (level = 0)
ClearList(dList())
EndIf
If Right(dir$, 1) <> #PS$
dir$ + #PS$
EndIf
If ExamineDirectory(0, dir$, "")
While NextDirectoryEntry(0)
EntryName = DirectoryEntryName(0)
Select DirectoryEntryType(0)
Case #PB_DirectoryEntry_Directory
If Left(EntryName,1) <> "."
AddElement(FolderList())
FolderList() = EntryName + #PS$
EndIf
Case #PB_DirectoryEntry_File
If Left(EntryName,1) <> "."
If MatchRegularExpression(SearchRegEx,EntryName)
AddElement(dList())
FileCount = FileCount + 1
dList() = dir$ + EntryName
EndIf
EndIf
EndSelect
Wend
EndIf
If ListSize(FolderList())
ForEach FolderList()
SearchDirectory(dir$ + FolderList(), dList(), level + 1)
Next
EndIf
EndProcedure
Define Word.s
;Search for all image files
filter.s ="jpg|rw2|cr2|nef|fpx|dcr|pcd|pef|raf|rwl|srw|arw|raw|kdc|iiq|fff|orf|eip|cib|kqp|dng|mrw|mdc|x3f|nop|exf|sr2|crw|ari|3fr|mos|lfr|r3d|mef|nrw|ndd|erf|ra2|dc2|ptx|srf|st7|sd0|sd1|st5|gray|grey|ce2|cmt|craw|st4|st6|gry|nwb|ce1|rwz|3pr|st8|mfw|kc2|olr|cap|stx|dcs|ycbcra|bay|heic|heif"
;Search for .png and .gif only
Word.s = "\.(?i)(" + filter + ")(?-i)$"
SearchRegEx = CreateRegularExpression(#PB_Any, Word)
;Define the topmost folder to start search
SearchFolder.s = PathRequester("Please choose a SOURCE path", #INITIAL_PATH)
SearchDirectory(SearchFolder, FilesFolders())
ForEach FilesFolders()
Debug FilesFolders()
Next