Delphi 列举(筛选)子目录下符合档案【附例】

Purpose:运用’回呼函数Callback’,列举(筛选)子目录下符合档案
Enviro:Delphi RAD 10.4 Win 64x
//---实作下列这段话
Start by defining a type for your function/procedure
type
TMyProc = procedure(Param1: Integer);

Then you can use your procedure type anywhere, as long as your procedure's signature matches your type. To call your callback from within, you can use something like this:

procedure DoAndCallBack(MyProc: TMyProc)
begin
MyProc(1);
end;
步骤:上段话分两步骤

  1. 定义一个类别Type,当它被呼叫时,执行某事
  2. 在Procedure内呼叫它 MyProc

Step 1-1. 定义一个Type EnumProc 参数:档名、档案属性
type
TEnumProc = procedure(fNa: string; vAttr:integer; var bCont:Boolean) of object;

Step 1-2. 创建一个该类别的实体AddRec(),写码,写入要执行的事

//--- AddRec 实作TEnumProc类别, 把档案加入 List
//--- 接受参数 fNa档名  vAttr档案属性
procedure TfrmSeek.AddRec(fNa: string; vAttr:integer; var bCont:Boolean);
begin
  memo1.Lines.Add(fNa);
  if vAttr = faDirectory then dirList.Add(fNa);
  if (vAttr and faArchive)  <>0 then fList.Add(fNa);
  bCont := True;
end;

Step 2. 如何呼叫它
写一个Procedure,参数:subYes 是否包含子目录
procedure TfrmSeek.EnumFiles(subYes:Boolean; sDir, SMASK: string; Attr: Integer; AddFile: TEnumProc);
完整代码请详见 Source Code

只找一层
//--- 找现在这一层

 //--- 找现在这一层
  Status := FindFirst(sDir+SMASK, faAnyFile, SRec);
  try
    while Status = 0 do
    begin
      //--- 下一行的意思是: ( 只要档案啦 )
      //--- SRec.Attr 和Attr 做AND运算,如果<>0,表示有此属性
      //--- 而且不是Folder ,也不是 '.' 或者'..'
      if (SRec.Attr and Attr <> 0) and (FileExists(sDir + SRec.name)) and
        not ((SRec.Attr and faDirectory <> 0) and ((SRec.name = '.') or (SRec.name = '..'))) then
      begin
        bContinue := True;
        //--- 加入 List
        AddRec( sDir+SRec.name, SRec.Attr ,bContinue );
        if not bContinue then Break;
        if flagStop then Break;

      end;
      //--- 继续找
      Status := FindNext(SRec);
      Application.ProcessMessages;
    end;
  finally
    FindClose(SRec);
  end;

再找子目录层

//--- 找子目录层
  if subYes then begin
     Status := FindFirst(sDir + '*.*', faDirectory, SRec);
     try
       while Status = 0 do
       begin
         //--- add to directory list
         if (SRec.Attr = faDirectory) and ((SRec.Name<>'.') and (SRec.Name<>'..')) then
         begin
            dirList.Add(SRec.Name);
            memo2.Lines.Add(sRec.Name);
         end;

         if (SRec.Name<>'.') and (SRec.Name<>'..') then begin
            EnumFiles(subYes, sDir + SRec.name, SMASK, Attr, AddRec);
         end;
         //--- 继续找
         Status := FindNext(SRec);
         if flagStop then Break;
         // 加这行才能即时中断执行
         Application.ProcessMessages;
       end;
     finally
       FindClose(SRec);
     end;
  end;

Step 3. 在btnGo 呼叫它
EnumFiles(subAlso,path, filter,faAnyFile, AddRec);

注:Global Variables 宣告
DirCount : integer;
dirList : TStringList;
fList : TStringList;
flagStop : Boolean; //--- 中断执行

成果: 范例 Source + Exec
https://ithelp.ithome.com.tw/upload/images/20210824/20111373NLaXOKSr76.jpg
2021/8/24


<<:  Day55 (React)

>>:  第43天~

我们的基因体时代-AI, Data和生物资讯 Day13- 最基本的生物资讯资料格式Fasta

上一篇我们的基因体时代-AI, Data和生物资讯 Day12-基因疗法中之腺病毒载体与机器学习 我...

Day 14 - 函数与物件互动 - 制作蜜蜂靠近花朵

function 函数 为什麽要用函数:函数可以把需要重复执行的行为打包,在需要使用的时候直接使用函...

[Day 1] 身为一名普通 iOS 开发者所需的程序知识 Intro

前言 Hi 我是一名普通的 iOS 开发者,兴趣使然的 UI 设计师。不小心参与了几年 iOS 开发...

Day26 LINE BOT & NBA 战况查询

本次 Github 连结 我们在这个区块要完成的功能有: 查询今日战况 查询特定日期战况 网页观察 ...

TypeScript 能手养成之旅 Day 10 物件型别-扩充型别-列举(Enum)

前言 上集我们介绍到 Enum 基础用法,今天将来讲解其它用法。 字串列举(String enum)...