File Search User-Defined Function (UDF) Example
ID: Q117216
2.00 2.50 2.50a 2.50b 2.60 | 2.50 2.50a 2.50b 2.60 3.00
MS-DOS | WINDOWS
The information in this article applies to:
- Microsoft Visual FoxPro for Windows, version 3.0
- Microsoft FoxPro for MS-DOS, versions 2.0, 2.5, 2.5a, 2.5b, 2.6
- Microsoft FoxPro for Windows, versions 2.5, 2.5a, 2.5b, 2.6
SUMMARY
The FileFind() user-defined function (UDF), shown below, uses the ADIR()
function to locate a file starting from a particular location on the hard
disk drive.
MORE INFORMATION
PROCEDURE FILEFIND
* WARNING: If the user has a directory structure 32+ levels deep,
* this program will crash.
* This procedure will recursively search a specified directory and
* its subdirectories for a particular file and if found return the
* location from the root directory. If the file is not found, the
* message "FILE NOT FOUND!" will be displayed. Wildcard characters
* are not supported.
* Example of how to call this function is :
* TempVariable=FileFild("Customer.dbf","C:")
PARAMETER FILE, CUR_DIR
filefound = " "
IF RIGHT((cur_dir),1) = "\"
cur_dir=substr(cur_dir,1,len(cur_dir)-1)
ENDIF
=filesrch(file,cur_dir)
IF EMPTY(filefound)
filefound = "FILE NOT FOUND!"
ENDIF
RETURN UPPER(filefound)
FUNCTION FILESRCH
*
* This is the recursive part of the program. This function will
* search the specified directory and call itself again if needed to
* search a subdirectory.
PARAMETER FILENAME, CURDIR
PRIVATE filename, curdir, temp_dir, i, j && Private variables needed
* curdir is the current directory. && for recursion to work.
* filename is the file being searched for.
DIMENSION temp_dir(1,1)
temp_dir(1,1)=" "
=ADIR(temp_dir,curdir+"\"+FILENAME)
IF ASCAN(temp_dir,UPPER(filename)) != 0 && Search current directory
filefound=curdir + "\"+ UPPER(filename)
RETURN TO filefind && IF file found, end program
ENDIF
=ADIR(temp_dir,curdir+"\*.","D") && Get subdirectories
IF temp_dir(1,1) != " " AND filefound =" "&& Search subdirectories
IF temp_dir(1,1)="." && Look at first subdirectory
IF ALEN(temp_dir,1)=2 && Possibly no subdirectories
RETURN
ENDIF
i=3
ELSE
i=1
ENDIF
FOR j = i TO ALEN(temp_dir,1) && Start searching subdirectories
curdir=curdir+"\"+temp_dir(j,1)
=filesrch(filename,curdir) && Recursive call
curdir=SUBSTR(curdir,1,RAT("\",curdir)-1)
ENDFOR
ENDIF
RETURN
Additional reference words: VFoxWin 3.00 FoxDos FoxWin 2.00 2.50 2.50a
2.50b 2.60 search
find
locate
KBCategory:
KBSubcategory: FxprgGeneral
Keywords : kbcode FxprgGeneral
Version : 2.00 2.50 2.50a 2.50b 2.60 | 2.5
Platform : MS-DOS WINDOWS
|