下面显示的是使用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。
答案 0 :(得分:7)
您可以使用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;