The following function demonstrates a simple method to search a disk drive and all subdirectories using a recursive method. If the function is passed an invalid directory in which to begin the file search it exits and returns the boolean value 'false' to the calling routine. The code can be easily modified for whatever you might need it for. As one example you might be searching for a specific file, in which case you can convert the Function and exit with the pathname to the file assigned to Result, etc. instead of returning the boolean true or false value. In this case it would be the calling routine that must check that the directory is valid before passing the filespec to the function.
A related page scans subdirectories and puts them onto a Delphi TreeView.
The TSearchRec type in Pascal returns only filenames in the 'name' variable of the record (TSearchRec.Name) and also returns directories without a trailing slash ('\'). The function assumes that 'FileSpec' (the variable passed to the function below) is already fully qualified, including the File Filter or file name. For example to begin a recursive search (including all subdirectories) starting at the directory c:\somewhere\thisfolder if you were looking for 'all files' you should pass the filespec to the routine as it is written in the form c:\somehwere\thisfolder\*.* ... If you were looking for a specific file you would of course dispense with the wild cards and pass c:\somehwere\thisfolder\filename.ext ... If you search for TSearchRec in Delphi help you will find a list of constants used in the attribute field of the record. If you wish to exclude certain file types from the search you can modify the code by 'subtracting' the constant from the fanyfile constant in the FindFirst function call. For example the constant which represents 'hidden files' is 'faHidden' and to exclude hidden files you can modify the source as follows
FindFirst(FileSpec, faAnyFile-faHidden, SearchRec); Note that you should not exclude directory files in this way (faAnyfile-faDirectory) if you want to recursively scan subdirectories which might be included within the starting directory. You can download the uncommented source code and the commented source code as a text file by right clicking on the link to the following text file and choosing 'save link as' or 'save target as' (depending on your browser_ to save the text file to your computer ---->disk_search_recursive.txt
Recursively search a disk and subdirectories for files
Delphi Pascal Source Code
procedure TForm1.FileLook(Filespec:string);
{filespec must contain the full path ie. c:\folder\*.*
or c:\folder\this.txt, etc.}
var
validres:integer; {Findfirst returns '0' only if there was no error}
SearchRec : TSearchRec; {required by 'FindFirst and FindNext' functions }
DirPath, FullName, Flname : string;
{SearchRec does not return the full path in the name variable in the record, so 'DirPath' is used to store the full path name, and 'FullName' holds the Full Path Name plus the file name or filter spec while 'Flname' holds the filter or filename}
begin
DirPath:=ExtractFilePath(FileSpec); {keep track of the path ie: c:\folder\}
Result:= DirectoryExists(DirPath);{Check for valid directory - include FileCtrl in the uses statement}
If not Result then exit;{Invalid directory then exit}
Flname:=ExtractFileName(FileSpec); {keep track of the name or filter}
validres := FindFirst(FileSpec, faAnyFile, SearchRec); {find first file}
while validres=0 do begin {if a matching file exists loop}
If (SearchRec.Name[1] <> '.') then begin {ignore . and .. dirs}
FullName:=DirPath + LowerCase(SearchRec.Name);
{The above line assumes that the user wants the full path and file name ... Note that at this point you can use the variable returned
for example writeln(fs, fullname); or x:=TreeInsert(FullName, head); or you can do a compare to see if this is the file you were looking for, etc. etc.}
If (SearchRec.Attr and faDirectory > 0) then {it is a directory, not a file}
FileLook(FullName+'\'+ Flname);
{the above line is the recursive call to search the subdirectory. The trailing slash is added to the path as well as the filter filter or filename}
end; {end if statement}
validres:=FindNext(SearchRec); {get next record before continuing conditional while loop}
end; {end while loop}
end; {end procedure}
A Unified Field Theory
![]()
The Unified Field Theory
is also available as a zip file -> unified.zip
Introduction :The Pioneer Effect and the New Physics. A brief description of the new physics required to explain the 'Pioneer Effect', which is the constant deceleration of space craft as they fly through space.

