{Recursively search a disk and subdirectories for files} { Delphi Pascal Source Code uncommented} {commented code follows below} {http://www.awitness.org/delphi_pascal_tutorial/source/disk_search_recursive.html} Function TForm1.FileLook(Filespec:string):boolean; var validres:integer; SearchRec : TSearchRec; DirPath, FullName, Flname : string; begin DirPath:=ExtractFilePath(FileSpec); Result:= DirectoryExists(DirPath); If not Result then exit; Flname:=ExtractFileName(FileSpec); validres := FindFirst(FileSpec, faAnyFile, SearchRec); while validres=0 do begin If (SearchRec.Name[1] <> '.') then begin {not a directory} FullName:=DirPath + LowerCase(SearchRec.Name); {Use variable here} If (SearchRec.Attr and faDirectory>0) then FileLook(FullName+'\'+Flname); end; validres:=FindNext(SearchRec); end; end; { Recursively search a disk and subdirectories for files Delphi Pascal Source Code with Comments} 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} 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}