Delphi does not provide any "ready made" RTL function to test if a given directory (folder) is empty, i.e. contains no files or sub-folders.
Is Directory Empty
Here's a custom Delphi function which returns true if a given directory (folder) is empty.//returns true if a given directory is empty, false otherwiseUsage:
function IsDirectoryEmpty(const directory : string) : boolean;
var
searchRec :TSearchRec;
begin
try
result := (FindFirst(directory+'\*.*', faAnyFile, searchRec) = 0) AND
(FindNext(searchRec) = 0) AND
(FindNext(searchRec) <> 0) ;
finally
FindClose(searchRec) ;
end;
end;
varNote: the IsDirectoryEmpty does not check if a directory exists. Use the DirecotryExists to determine whether a given directory exists.
directory : string;
isEmpty : boolean;
begin
//directory := 'c:\Program Files';
directory := 'c:\New Folder';
isEmpty := IsDirectoryEmpty(directory) ;
ShowMessage(BoolToStr(isEmpty,true)) ;
end;
The IsDirectoryEmpty uses FindFirst, FindNext and FindClose to look "inside" the directory provided. Those three functions are explined in details in the "Searching For Files" article.
Delphi tips navigator:
» Convert RGB to TColor or How to Get More TColor values for Delphi
« How to Override Delphi Form's Restore Operation?

