当前位置 : 主页 > 编程语言 > delphi >

delphi – 如何将TFileAttribute开关添加到IOUtils函数

来源:互联网 收集:自由互联 发布时间:2021-06-23
下面显示的是使用IOUtils计算文件夹和文件的两个函数.如何使用faHidden TFileAttribute添加一个开关(CountHidden)来切换计数文件或文件夹? function SubDirectoryCount( const Path: string; const CountHidden:
下面显示的是使用IOUtils计算文件夹和文件的两个函数.如何使用faHidden TFileAttribute添加一个开关(CountHidden)来切换计数文件或文件夹?

function SubDirectoryCount( const Path: string; const CountHidden: boolean; const Recurse: boolean ): Cardinal;
    { Counts all the sub-directories of a specified directory. If Recurse is true
      all sub-directories are counted recursively, otherwise only immediate
      sub-directories are counted. Count Hidden toggles counting hidden files (faHidden). }
    var
      StrArray: Types.TStringDynArray; // array of subdirectories
      SearchOption: IOUtils.TSearchOption; // directory search option
    begin
      if Recurse then
        SearchOption := IOUtils.TSearchOption.soAllDirectories
      else
        SearchOption := IOUtils.TSearchOption.soTopDirectoryOnly;
      StrArray := IOUtils.TDirectory.GetDirectories( Path, '*', SearchOption );
      Result := length( StrArray );
    end;

    function SubDirectoryFileCount( const Path: string; const CountHidden: boolean; const Recurse: boolean ): Cardinal;
    { Counts all the files of a specified directory. If Recurse is true
      files in all sub-directories are counted recursively, otherwise only immediate
      sub-directories are counted. Count Hidden toggles counting hidden files (faHidden).}
    var
      StrArray: Types.TStringDynArray; // array of subdirectories
      SearchOption: IOUtils.TSearchOption; // directory search option
    begin
      if Recurse then
        SearchOption := IOUtils.TSearchOption.soAllDirectories
      else
        SearchOption := IOUtils.TSearchOption.soTopDirectoryOnly;
      StrArray := IOUtils.TDirectory.GetFiles( Path, '*', SearchOption );
      Result := length( StrArray );
    end;

还有哪些可以改进这两个功能?
我在Windows 7上使用Delphi 2010.

您可以使用 TDirectory.TFilterPredicate课程.

检查这些样本

function SubDirectoryCount( const Path: string; const CountHidden: boolean; const Recurse: boolean ): Cardinal;
var
  StrArray     : Types.TStringDynArray;
  SearchOption : IOUtils.TSearchOption;
  Predicate    : TDirectory.TFilterPredicate;
begin
  if Recurse then
    SearchOption := IOUtils.TSearchOption.soAllDirectories
  else
    SearchOption := IOUtils.TSearchOption.soTopDirectoryOnly;

  Predicate := function(const Path: string; const SearchRec: TSearchRec): Boolean
                     begin
                       Result := (SearchRec.Attr and faHidden)=0;
                     end;

  if CountHidden then
    StrArray := IOUtils.TDirectory.GetDirectories( Path, '*', SearchOption)
  else
    StrArray := IOUtils.TDirectory.GetDirectories( Path, '*', SearchOption, Predicate);

  Result := length(StrArray);
end;

function SubDirectoryFileCount( const Path: string; const CountHidden: boolean; const Recurse: boolean ): Cardinal;
var
  StrArray     : Types.TStringDynArray;
  SearchOption : IOUtils.TSearchOption;
  Predicate    : TDirectory.TFilterPredicate;
begin
  if Recurse then
    SearchOption := IOUtils.TSearchOption.soAllDirectories
  else
    SearchOption := IOUtils.TSearchOption.soTopDirectoryOnly;

  Predicate := function(const Path: string; const SearchRec: TSearchRec): Boolean
               begin
                Result := (SearchRec.Attr and faHidden)=0;
               end;

  if CountHidden then
    StrArray := IOUtils.TDirectory.GetFiles( Path, '*', SearchOption )
  else
    StrArray := IOUtils.TDirectory.GetFiles( Path, '*', SearchOption, Predicate);

  Result := length(StrArray);
end;
网友评论