zoukankan      html  css  js  c++  java
  • Delphi 2010 新增功能之: IOUtils 单元(2): TDirectory.TFilterPredicate


    TDirectory.GetFiles 函数还有一个 TDirectory.TFilterPredicate 类型的参数, 这是个匿名函数类型, 可对搜索结果再处理.


    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TForm1 = class(TForm)
        Memo1: TMemo;
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        Button4: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure Button4Click(Sender: TObject);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    uses IOUtils, Types;
    
    const path = 'C:\Program Files\Embarcadero\RAD Studio\7.0';
    
    //准备给 TDirectory.GetFiles 调用的函数, 须符合 TDirectory.TFilterPredicate 格式
    function fp(const Path: string; const SearchRec: TSearchRec): Boolean;
    begin
      Form1.Memo1.Lines.Add(Path + '\' + SearchRec.Name);
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      files: TStringDynArray;
    begin
      Memo1.Clear;
      files := TDirectory.GetFiles(path, fp);
    end;
    
    procedure TForm1.Button2Click(Sender: TObject);
    var
      files: TStringDynArray;
    begin
      Memo1.Clear;
      files := TDirectory.GetFiles(path, '*.txt', fp);
    end;
    
    procedure TForm1.Button3Click(Sender: TObject);
    var
      files: TStringDynArray;
    begin
      Memo1.Clear;
      files := TDirectory.GetFiles(path, '*.txt', TSearchOption.soAllDirectories, fp);
    end;
    
    //可以这样方便地使用匿名函数
    procedure TForm1.Button4Click(Sender: TObject);
    var
      files: TStringDynArray;
    begin
      Memo1.Clear;
      files := TDirectory.GetFiles(path,
        function(const Path: string; const SearchRec: TSearchRec): Boolean
        begin
          Memo1.Lines.Add(Path + '\' + SearchRec.Name);
        end
      );
    end;
    
    end.
    
  • 相关阅读:
    kafka 的基本概念及使用场景
    使用RedisTemplate执行Redis脚本
    SpringBoot使用Lua脚本操作Redis
    Java不定参数Object… obj 和 Object[] 的区别
    IntelliJ IDEA添加快捷键自动输入@author信息
    使用Guava RateLimiter限流以及源码解析
    go fileserver
    记录了prometheus 告警指标
    https://mp.weixin.qq.com/s/ZBsZHQtAz_vKS8fwvHKB3g图文分析Kubernetes认证、授权和准入控制
    es不停机滚动update
  • 原文地址:https://www.cnblogs.com/del/p/1584711.html
Copyright © 2011-2022 走看看