フォルダ内のファイル数を知りたいのですが、
いい方法があればご教授ください。
よろしくお願いします。
皆さんお忙しいようなので、
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;
ツイート | ![]() |