下面显示的是使用IOUtils计算文件夹和文件的两个函数.如何使用faHidden TFileAttribute添加一个开关(CountHidden)来切换计数文件或文件夹? function SubDirectoryCount( const Path: string; const 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.
检查这些样本
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;