zoukankan      html  css  js  c++  java
  • TDirectory.GetFiles获取指定目录下的文件

    使用函数:

    System.IOUtils.TDirectory.GetFiles

    所有重载:

    class function GetFiles(const Path: string): TStringDynArray;
    class function GetFiles(const Path: string;  const Predicate: TFilterPredicate): TStringDynArray;
    class function GetFiles(const Path, SearchPattern: string): TStringDynArray;
    class function GetFiles(const Path, SearchPattern: string;  const Predicate: TFilterPredicate): TStringDynArray;
    class function GetFiles(const Path, SearchPattern: string;  const SearchOption: TSearchOption): TStringDynArray; overload; static;
    class function GetFiles(const Path, SearchPattern: string;  const SearchOption: TSearchOption; const Predicate: TFilterPredicate): TStringDynArray; overload; static;
    class function GetFiles(const Path: string;  const SearchOption: TSearchOption; const Predicate: TFilterPredicate): TStringDynArray; overload; static;

    参数说明和TDirectory.GetDirectories一样,只是目录换成文件

    异常处理: 目录无效或目录不存在

    示例:获取指定目录下,文件名包含'a'且不为隐藏属性的所有txt文本文件。

      代码部分:

    var
        sFile: string;
        fp: TDirectory.TFilterPredicate;
    begin
        //
        fp := function(const Path: string; const SearchRec: TSearchRec): Boolean
            begin
                Result := (Pos('a', SearchRec.Name) > 0) and
                  (SearchRec.Attr and faHidden <> faHidden); //文件名包含'a'且不为隐藏文件
            end;
        try
            for sFile in TDirectory.GetFiles(
                    'H:	mp1', //'H:	mp1'
                    '*.txt', //后缀为.txt
                    TSearchOption.soAllDirectories,// 'H:	mp1'目录下的所有文件
                    fp) do
                ListBox1.Items.Add(sFile);
        except
            on e: Exception do
            begin
                MessageDlg(e.Message, mtError, [mbok], 0);
                Exit;
            end;
        end;
    end;
  • 相关阅读:
    【现在还没补的比赛及题解】
    【刷题中】
    【寒假的待填的坑】
    【python】爬虫实践
    【python】vscode python环境配置
    【spring学习笔记二】Bean
    【spring学习笔记一】Ioc控制反转
    【2018CCPC秦皇岛】
    【2018ICPC沈阳】
    【2018ICPC青岛】
  • 原文地址:https://www.cnblogs.com/cause/p/3500100.html
Copyright © 2011-2022 走看看