Recursive Functions
Recursive User-Defined Functions
Can BCX handle recursive functions?
No problem! Here's a popular example.
' Example function call usage 1'SearchForFiles("C:\", "win.ini")' Example function call usage 2SearchForFiles("C:\Windows","Notepad.exe")SUBSearchForFiles(startPath$, FileToFind$)DIMRAWfPath$, wildpath$, fName$, fPathName$DIMRAWhfindASHANDLEDIMRAWWFDASWIN32_FIND_DATADIMRAWfoundASBOOLeanDIMRAWfilename$' Prepare filename$ for comparison with found file namesfilename$=LCASE$(FileToFind$)' Make a local copy of the search pathfPath$=startPath$' Make sure search path ends with a backslashIFRIGHT$(fPath$,1)<>"\"THENfPath$=fPath$+"\"' We want to find all files and folders so we'll' add a wildcard asterisk to the search pathwildpath$=fPath$+"*"' Ask Windows API for first file or folder' in our search pathhfind=FindFirstFile(wildpath$,&WFD)' Did we get one?IFhfind !=INVALID_HANDLE_VALUETHENfound=TRUEELSEfound=FALSEENDIFDOWHILEfound' Get the name of the file or folder' from the WIN32_FIND_DATA structurefName$=WFD.cFileName' Add the file or folder name to the search pathfPathName$=fPath$+fName$IFfName$ !="."THEN' ignore "current folder"IFfName$ !=".."THEN' ignore "parent folder"' Did we find a file or a folder?IF(WFD.dwFileAttributesBANDFILE_ATTRIBUTE_DIRECTORY)THEN' Found a folder so look for our target' file in that folderSearchForFiles(fPathName$, filename$)' Found a file so does it match our target?ELSEIFLCASE$(fName$)=filename$THEN' Code here could call another routine,' add the found file to a listbox,' or whatever. We'll just display it's' full path and name in a message box"Found ", fPathName$ELSE' File searching can be a lengthy process' so give other processes a chance to do' whatever they need to doDOEVENTSENDIFENDIFENDIF' Ask Windows API for the next file or' folder in our search pathfound=FindNextFile(hfind,&WFD)LOOP' Tell Windows API we've finished searchingFindClose(hfind)ENDSUB