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 2 SearchForFiles("C:\Windows", "Notepad.exe") SUB SearchForFiles(startPath$, FileToFind$) DIM RAW fPath$, wildpath$, fName$, fPathName$ DIM RAW hfind AS HWND DIM RAW WFD AS WIN32_FIND_DATA DIM RAW found AS BOOLean DIM RAW filename$ ' Prepare filename$ for comparison with found file names filename$ = LCASE$(FileToFind$) ' Make a local copy of the search path fPath$ = startPath$ ' Make sure search path ends with a backslash IF RIGHT$(fPath$, 1) <> "\" THEN fPath$ = fPath$ + "\" ' We want to find all files and folders so we'll ' add a wildcard asterisk to the search path wildpath$ = fPath$ + "*" ' Ask Windows API for first file or folder ' in our search path hfind = FindFirstFile(wildpath$, &WFD) ' Did we get one? IF hfind != INVALID_HANDLE_VALUE THEN found = TRUE ELSE found = FALSE END IF DO WHILE found ' Get the name of the file or folder ' from the WIN32_FIND_DATA structure fName$ = WFD.cFileName ' Add the file or folder name to the search path fPathName$ = fPath$ + fName$ IF fName$ != "." THEN ' ignore "current folder" IF fName$ != ".." THEN ' ignore "parent folder" ' Did we find a file or a folder? IF(WFD.dwFileAttributes BAND FILE_ATTRIBUTE_DIRECTORY) THEN ' Found a folder so look for our target ' file in that folder SearchForFiles(fPathName$, filename$) ' Found a file so does it match our target? ELSEIF LCASE$(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 PRINT "Found ", fPathName$ ELSE ' File searching can be a lengthy process ' so give other processes a chance to do ' whatever they need to do DOEVENTS END IF END IF END IF ' Ask Windows API for the next file or ' folder in our search path found = FindNextFile(hfind, &WFD) LOOP ' Tell Windows API we've finished searching FindClose(hfind) END SUB
Result:
Found C:\Windows\notepad.exe Found C:\Windows\System32\notepad.exe Found C:\Windows\SysWOW64\notepad.exe Found C:\Windows\WinSxS\amd64_microsoft-windows-notepad_31bf3856ad364e35_10.0.19041.117_none_4d353cf1ceb5d6d2\f\notepad.exe Found C:\Windows\WinSxS\amd64_microsoft-windows-notepad_31bf3856ad364e35_10.0.19041.117_none_4d353cf1ceb5d6d2\notepad.exe Found C:\Windows\WinSxS\amd64_microsoft-windows-notepad_31bf3856ad364e35_10.0.19041.117_none_4d353cf1ceb5d6d2\r\notepad.exe Found C:\Windows\WinSxS\amd64_microsoft-windows-notepad_31bf3856ad364e35_10.0.19041.1_none_250b9aff0f5d41ee\notepad.exe Found C:\Windows\WinSxS\wow64_microsoft-windows-notepad_31bf3856ad364e35_10.0.19041.1_none_2f60455143be03e9\notepad.exe