Implement subdirectory level file query with Delphi

zhaozj2021-02-11  205

Implement subdirectory level file query with Delphi

In application practices, we often use file query. With the lookup feature provided in Win95, we can easily find files in any subdirectory on disk, why this lookup feature can traverse files in all subdirectory in the specified directory. From a programming perspective, it implements the file query of the subdirectory level. In fact, this function is not difficult to implement, the key is to understand and master this program design idea. I implemented this function with Delphi (any sub-directory level), because of the use of 懙莨 阍, the program is clear, the amount of code is small.

Implementation method: 1. Get all the next level subdirectory in the current directory. 2. Store in a string list (TSTRINGS). Among them, several API functions are used. FindFirst is to find the first file or directory in the specified directory. Findnext is generally used with FindFirst to find the next file or directory. FindClose is used to close the query. (There is a detailed explanation of the above function Delphi online help, and details will not be described herein); 3. Use the FileExists function to find the current directory, 4. Looking for the existence of files that meet the conditions, 5. In turn, make each child directory a current directory, 6. Recurns call this function, 7. Release resource, 8. Returns the results of the query. The code is as follows: 1. Judging from the search record is a subdirectory. Function isvaliddir (SearchRec: tsearchrec): boolean; begin if (searchrec.attr = 16) and (searchrec.name <> '.') and (searchRec.name <> '..'). '.') THEN Result: = true else = FALSE; END; 2. This is the query body function. Parameter introduction:

Mainpath: Specify the query directory. FileName: Document to query. FoundResult: Returns the matching file containing the full path (there may be multiple).

If there is a match file, the function returns true, otherwise, return false;

function SearchFile (mainpath: string; filename: string; var foundresult: TStrings): Boolean; var i: integer; Found: Boolean; subdir1: TStrings; searchRec: TsearchRec; begin found: = false; if Trim (filename) <> ' 'The begin subdir1: = tstringlist.create; // String list must be dynamically generated // to find all sub-subdirectory. if (FindFirst (mainpath '* *.', faDirectory, SearchRec) = 0) then begin if IsValidDir (SearchRec) then subdir1.Add (SearchRec.Name); while (FindNext (SearchRec) = 0) do begin if IsValidDir (SearchRec ) The subdir1.add (searchRec.name); end; end; findclose (searchRec); // lookup current directory. If FileExists (MainPath FileName) The begin found: = true; FoundResult.add (MainPath filename); END; // This is a recursive part and finds each subdirectory. For i: = 0 to subdir1.count-1 dofact: = searchfile (Mainpa Subdir1.strings [i] '/', filename, FoundResult) or found; // resource release and return the result. Subdir1.free; End; Result: = Found; End; In short, as long as you master your thinking, use which programming language can be implemented. Now you can easily hang a very useful feature to your system. "Site: delphi enthusiasts | e-mail: delphifan@9990.net | WebMaster: Liang Ming"

-------------------------------------------------- -------------------------

转载请注明原文地址:https://www.9cbs.com/read-3987.html

New Post(0)