Hi all,
I've been trying to get my head round Functions and decided to try to modify some code to do some recursive folder searching for a particular file.
The code takes a file to search for and a root directory and searches for the file and returns the full path.
Here's the code:
Dim $SearchFile = "bod_r.TTF"
Dim $StartDir = @ProgramFilesDir
Dim $SearchResult = 0
$FilePath = FileSearch($SearchFile, $StartDir)
MsgBox(0,"FilePath",$FilePath)
Exit
Func FileSearch($SearchFile, $StartDir, $Depth = 0)
If $Depth = 0 Then Global $RFString = ""
$Search = FileFindFirstFile($StartDir & "\*.*")
If @error Then Return
;Search through all files and folders in directory
While $SearchResult = 0
$Next = FileFindNextFile($Search)
If @error Then ExitLoop
;If folder, recurse
If StringInStr(FileGetAttrib($StartDir & "\" & $Next), "D") Then
FileSearch($SearchFile, $StartDir & "\" & $Next, $Depth + 1)
Else
;Append filename to master string
$RFString = $StartDir & "\" & $Next
EndIf
If $Next = $SearchFile Then
$SearchResult = 1
EndIf
WEnd
FileClose($Search)
If $SearchResult = 1 Then
Return $RFString
Else
Return "File Not Found"
EndIf
EndFunc ;==>FileSearch
The problem is that I can't seem to work out a way to use the function without declaring the $SearchResult = 0 first. This is annoying as it means the Function is not self contained. The $SearchResult is used to keep the While loop alive and also to determine what is returned. If I Dim it inside the fuction then it breaks it (Returns File Not Found). Any more experinced people got any idea how I can make this work so it's self contained so i can start to build my own #include file.
Thanks