delphi – 计数文件夹中的文件夹
发布时间:2020-12-15 04:26:45 所属栏目:大数据 来源:网络整理
导读:有谁知道一个代码,我可以用来计算指定目录中的文件夹数量? 解决方法 我知道的最简单的代码是使用IOUtils单元的TDirectory: function GetDirectoryCount(const DirName: string): Integer;begin Result := Length(TDirectory.GetDirectories(DirName));end;
有谁知道一个代码,我可以用来计算指定目录中的文件夹数量?
解决方法
我知道的最简单的代码是使用IOUtils单元的TDirectory:
function GetDirectoryCount(const DirName: string): Integer; begin Result := Length(TDirectory.GetDirectories(DirName)); end; TDirectory.GetDirectories实际上返回一个包含目录名称的动态数组,所以这有点低效.如果你想要最有效的解决方案,那么你应该使用FindFirst枚举. function GetDirectoryCount(const DirName: string): Integer; var res: Integer; SearchRec: TSearchRec; Name: string; begin Result := 0; res := FindFirst(TPath.Combine(DirName,'*'),faAnyFile,SearchRec); if res=0 then begin try while res=0 do begin if SearchRec.FindData.dwFileAttributes and faDirectory<>0 then begin Name := SearchRec.FindData.cFileName; if (Name<>'.') and (Name<>'..') then begin inc(Result); end; end; res := FindNext(SearchRec); end; finally FindClose(SearchRec); end; end; end; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |