Easy to find a file - a universal file lookup function that supports the callback function

zhaozj2021-02-08  208

Easy to find a file in the usual programming, often encounter a problem with a certain type of file or all files in a directory, in order to adapt to different needs, we often have to write a large number of similar code, is there possible Write a generic lookup file program that is processed after finding a file? This way we can write some part of the processing file, do not need to write some of the findings of the file! The answer is yes. The following programs can implement this feature! The efficiency of this algorithm is to be improved, mainly the directory processing section (specifically pointed out). File: // Description: file: // tfindcallback is called the callback function, and the FindFile function will call this function after finding a matching file. File: // TfindCallback's first parameter found file name, you can operate according to the file name in the callback function. File: // TfindCallback's second parameter is a TSEARCHREC structure for the relevant record information found by the file. File: // TfindCallback's third, four parameters respectively determine whether termination of files, temporarily decide whether to find a subdirectory! File: // Findfile Parameters: File: // The first decision is to exit the lookup, should initialize to false; file: // second is to find the path; file: // The third is the file name, can contain Windows supported by any wildcards; default all file file: // fourth is a callback function, the default is empty File: // Fifth decision to find the subdirectory, default is the lookup subdirectory file: // Six decisions to handle other messages when they find files, by default to handle other messages, this parameter can speed up the processing speed if false, but will not respond to any message of the program. File: // If you have any comments and suggestions, e_mail: kingron@163.nettype tfindcallback = procedure (const filename: string; constin: tsearchrec; var bquit, bsub: boolean);

Procedure Findfile (var quit: string; const filename: string = '*. *'; proc: tfindcallback = nil; bsub: boolean = true; const bmsg: boolean = true; var fpath: string; info : Tsearchrec;

Procedure processafile; begin if (Info.name <> '.') and (info.name <> '..') and (info.attr and fadirectory <> fadirectory) Then Proc (Proc) Then Proc (PROC) FPATH Info.finddata.cfileName, Info, Quit, BSUB; End; End;

Procedure processadirectory; begin if (Info.name <> '.') and (Info.name <> '..') and (info.attr and fadirectory = fadirectory) THEN FINDFILE (Quit, Fpath Info.name, FileName, Proc, BSUB, BMSG); End; Beginif Path [Length] <> '/' Then fpath: = path '/' else fpath: = path; try if 0 = findfirst (fpath filename, faanyfile and (not fadirectory), info) then begin ProcessAFile; while 0 = findnext (info) do begin ProcessAFile; if bmsg then application.ProcessMessages; if quit then begin findclose (info); exit; end; end; end; finally findclose (info ); END; TRY IF BSUB AND (0 = FindFirst (FPATH * ', FAANYFILE, INFO)) THEN FILE: // This has been improved here because it will find // all files, not just a directory Begin Processadirectory; while Findnext (info) = 0 do processadirectory; finally FindClose (INFO); END; END; example: // Toning function: Procedure AAA (const filename: string; constin: tsearchrec; var quit, bsub: boolean; begin fo Rm1.ListBox1.items.add (filename); quit: = form1.qqq; bsub: = form1.checkbox1.checked;

procedure TForm1.Button1Click (Sender: TObject); beginlistbox1.Clear; file: // Initialization qqq: = false; button1.Enabled: = false; findfile (qqq, edit1.text, edit2.text, aaa, checkbox1.checked, checkbox2 .CHECKED; ShowMessage (INTSTOX1.COUNT); button1.enabled: = true;

Procedure tForm1.Button2Click (Sender: Tobject); beginqqq: = true; file: // Terminate lookup END;

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

New Post(0)