A
A
Alexander2016-10-08 11:15:15
Delphi
Alexander, 2016-10-08 11:15:15

How to delete files by mask in Delphi subdirectories?

Guys, tell me how to delete files by mask in a directory and subdirectories in Delphi?
There is a function code:

function FullRemoveDir(Dir: string; DeleteAllFilesAndFolders,
  StopIfNotAllDeleted, RemoveRoot: boolean): Boolean;
var
  i: Integer;
  SRec: TSearchRec;
  FN: string;
begin
  Result := False;
  if not DirectoryExists(Dir) then
    exit;
  Result := True;

  Dir := IncludeTrailingBackslash(Dir);
  i := FindFirst(Dir + '*.log', faAnyFile, SRec);
  try
    while i = 0 do
    begin

      FN := Dir + SRec.Name;

      if SRec.Attr = faDirectory then
      begin

        if (SRec.Name <> '') and (SRec.Name <> '.') and (SRec.Name <> '..') then
        begin
          if DeleteAllFilesAndFolders then
            FileSetAttr(FN, faArchive);
          Result := FullRemoveDir(FN, DeleteAllFilesAndFolders,
            StopIfNotAllDeleted, True);
          if not Result and StopIfNotAllDeleted then
            exit;
        end;
      end
      else
      begin
        if DeleteAllFilesAndFolders then
          FileSetAttr(FN, faArchive);
        Result := SysUtils.DeleteFile(FN);
        if not Result and StopIfNotAllDeleted then
          exit;
      end;

      i := FindNext(SRec);
    end;
  finally
    SysUtils.FindClose(SRec);
  end;
  if not Result then
    exit;
  if RemoveRoot then
    if not RemoveDir(Dir) then
      Result := false;
end;

However, if you specify a mask '*.log',, then files are deleted by mask only in the specified directory, but not in its subdirectories. How to fix? And if we change the mask to '*.*', then all files in all subdirectories are deleted. It is by mask that it does not delete in subdirectories. Where is the mistake?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2016-10-08
@Mercury13

Well, clear pepper.
Directories should be searched by *, and files should be deleted by *.log.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question