Author: Christian Cristofori
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:
1 uses2 FileCtrl, SysUtils;
3 4 function IsEmptyFolder(fld: string): boolean;
5 var6 sr: tsearchrec;
7 r: integer;
8 begin9 fld := IncludeTrailingBackSlash(fld);
10 result := false;
11 if (DirectoryExists(fld)) then12 begin13 result := true;
14 r := findfirst((fld + '*.*'), faAnyFile, sr);
15 while ((r = 0) and (result)) do16 begin17 // Revision 2:18 // checks for system folders "." and ".." that always exists19 // inside an empty folder.20 if ((SR.Attr and faDirectory) <> 0) then21 begin22 if ((sr.name <> '.') and (sr.name <> '..')) then23 result := false;
24 end25 else26 result := false;
27 r := findnext(sr);
28 end;
29 // Revision 1:30 // this prevents compiler by using the API defined in windows unit,31 // that will raise a compiler error like this:32 // [Error]:Incompatible types: 'Cardinal' and 'TSearchRec'33 sysutils.findclose(sr);
34 end;
35 end;