Find+a+file

This is a small program that searches for a file on the current disk drive and returns its full path if it exists. If there are more than one file with the same name on the drive, it will find the first in its search path. If you want to use this code in your programs, keep in mind that the search may take a while. Therefore you should provide some sort of display to show that the program has not stopped. (This program displays the path currently searched 3 levels deep.) Although there is a SCAN statement in the function, the user should not be allowed to exit the recursion by this means and continue running the program as this may cause unpredictable behaviour.

code format="qbasic" nomainwin dim info$(1, 1) WindowHeight = 175 WindowWidth = 320 statictext #main, "File to find:", 10, 18, 100, 20 textbox #main.tb, 10, 40, 100, 25 button #main.go, "Go", [go], UL, 10, 70, 100, 25 statictext #main.st, "", 10, 100, 300, 45 open "Search" for window as #main wait [go] if b$ = "" then #main.st, "Please enter file to search for." else #main.go, "!disable" path$ = find$("\", b$) 'directory, file name if path$ = "" then #main.st, "File not found!" else #main.st, "Found:"; chr$(13); path$; b$   end if    #main.go, "!enable" end if wait
 * 1) main, "trapclose quit"
 * 2) main.tb, "wordpad.exe"
 * 1) main.tb, "!contents? b$"

function find$(a$, b$) scan files a$, b$, info$(   v$ = info$(0, 3) 'current directory    'this part to display directory up to 3 levels deep    a = 0    for i = 1 to 3        if instr(v$, "\", a) > 0 then a = instr(v$, "\", a)+1    next i    if mid$(v$, a) <> "" then v$ = left$(v$, a-1); ".."    #main.st, "Searching..."; chr$(13); v$

if val(info$(0, 0)) > 0 then find$ = info$(0, 3) 'if file found return path else folders = val(info$(0,1)) 'get number of folders for i = 1 to folders 'check each subfolder files a$, b$, info$( 'get folder names, needed because arrays are global           find$ = find$(info$(i, 0), b$) 'get result from subfolder            if find$ <> "" then exit for 'if found step out of loop/function        next i 'check next subfolder    end if end function

sub quit h$   close #main end end sub code