フォルダ内のファイルの数を知るには?


イタリア  2004-05-11 00:37:11  No: 8856

フォルダ内のファイル数を知りたいのですが、
いい方法があればご教授ください。
よろしくお願いします。


sadoyama  URL  2004-05-11 19:23:47  No: 8857

皆さんお忙しいようなので、

FindFirst, FindNextを使うと取得できます。

function GetFilesCount(const Path: String): Integer;
var
  Res: Integer;
  SearchRec: TSearchRec;
  Attri: Integer;
  Count: Integer;
  S: String;
begin
  Result := 0;
  Count := 0;
  // Pathの最後が'\'でない場合、'\'を加える = 重要
  // IncludeTrailingPathDelimiter()がないVersionでは
  // IncludeTrailingBackslash()を使用
  // それもないversionでは、IsPathDelimiter()でチェックし、
  // S := Path + '\'
  S := IncludeTrailingPathDelimiter(Path);

  Res := FindFirst(S + '*.*', faAnyFile, SearchRec);
  try
    try
      while Res = 0 do
      begin
        Attri := SearchRec.Attr;
        if  (Attri <> faDirectory) then
          Inc(Count);
        Res := FindNext(SearchRec);
      end;

      Result := Count;
    except
      Showmessage('ファイル数の取得に失敗しました。');
    end;
  finally
    FindClose(SearchRec);
  end;
end;


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加