2007. április 12., csütörtök
Retrieve if a given folder is empty
Problem/Question/Abstract:
Ever needed to check if a folder is empty or not? That's my way of doing. I think it's really fast, but not bench marked yet.
Answer:
uses
FileCtrl, SysUtils;
function IsEmptyFolder(fld: string): boolean;
var
sr: tsearchrec;
r: integer;
begin
fld := IncludeTrailingBackSlash(fld);
result := false;
if (DirectoryExists(fld)) then
begin
result := true;
r := findfirst((fld + '*.*'), faAnyFile, sr);
while ((r = 0) and (result)) do
begin
// Revision 2:
// checks for system folders "." and ".." that always exists
// inside an empty folder.
if ((SR.Attr and faDirectory) <> 0) then
begin
if ((sr.name <> '.') and (sr.name <> '..')) then
result := false;
end
else
result := false;
r := findnext(sr);
end;
// Revision 1:
// this prevents compiler by using the API defined in windows unit,
// that will raise a compiler error like this:
// [Error]:Incompatible types: 'Cardinal' and 'TSearchRec'
sysutils.findclose(sr);
end;
end;
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése