Cześć kodu usefulfunctions

Cześć kodu unit usefulfunctions, na potrzeby function IsDirectoryEmpty
 avatar
user_6919294
pascal
10 months ago
1.7 kB
7
Indexable
// Ireneusz A.
// Moje przydatne funkcje, przydaja sie do innych projektów
// Pisane pod Delphi

unit usefulfunctions;

interface

uses
  System.SysUtils, System.IOUtils;

// funkcja sprawdza czy katolog nie jest pusty, brak jakichkolwiek plikow
function IsDirectoryEmpty(const Directory: string): Boolean; overload;
// to samo ale dodajemy maske plikow
function IsDirectoryEmpty(const Directory, FileMask: string): Boolean; overload;

implementation

function IsDirectoryEmpty(const Directory: string): Boolean;
var
  SearchRec: TSearchRec;
  Found: Integer;
begin
  Result := True;
  if not DirectoryExists(Directory) then
    Exit;

  Found := FindFirst(IncludeTrailingPathDelimiter(Directory) + '*', faAnyFile, SearchRec);
  try
    while Found = 0 do
    begin
      // Ignore '.' and '..'
      if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
      begin
        Result := False;
        Break;
      end;
      Found := FindNext(SearchRec);
    end;
  finally
    FindClose(SearchRec);
  end;
end;

function IsDirectoryEmpty(const Directory, FileMask: string): Boolean;
var
  SearchRec: TSearchRec;
  Found: Integer;
begin
  Result := True;
  if not DirectoryExists(Directory) then
    Exit;

  Found := FindFirst(IncludeTrailingPathDelimiter(Directory) + FileMask, faAnyFile, SearchRec);
  try
    while Found = 0 do
    begin
      // Ignore '.' and '..'
      if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
      begin
        Result := False;
        Break;
      end;
      Found := FindNext(SearchRec);
    end;
  finally
    FindClose(SearchRec);
  end;

end;

end.
Editor is loading...