gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 15:24:17 32 4
gpt4 key购买 nike

下面显示了两个使用 IOUtils 来计算文件夹和文件数量的函数。如何添加开关 (CountHidden) 来使用 faHidden TFileAttribute 切换计数文件或文件夹?

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;

关于delphi - 如何将 TFileAttribute 开关添加到 IOUtils 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8312017/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com