Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete files matching pattern within a directory

Tags:

delphi

That is, delete all files matching pattern within a given directory

Example, Delete all *.jpg files within DirectoryName

like image 931
volvox Avatar asked Jun 12 '09 19:06

volvox


People also ask

How do you delete a file in Linux that matches a pattern?

The rm command can remove multiple files at once either by passing it more than one file or by using a pattern.

How do I remove a specific pattern from a Unix file?

N command reads the next line in the pattern space. d deletes the entire pattern space which contains the current and the next line. Using the substitution command s, we delete from the newline character till the end, which effective deletes the next line after the line containing the pattern Unix.

How do you remove a directory which contains files?

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.


3 Answers

procedure TForm1.Button1Click(Sender: TObject);
begin
  DeleteFiles(ExtractFilePath(ParamStr(0)),'*.jpg');
end;

procedure DeleteFiles(APath, AFileSpec: string);
var
  lSearchRec:TSearchRec;
  lPath:string;
begin
  lPath := IncludeTrailingPathDelimiter(APath);

  if FindFirst(lPath+AFileSpec,faAnyFile,lSearchRec) = 0 then
  begin
    try
      repeat
        SysUtils.DeleteFile(lPath+lSearchRec.Name);    
      until SysUtils.FindNext(lSearchRec) <> 0;
    finally
      SysUtils.FindClose(lSearchRec);  // Free resources on successful find
    end;
  end;
end;
like image 196
Jeff Cuscutis Avatar answered Oct 18 '22 13:10

Jeff Cuscutis


In more recent versions of Delphi, you would probably use the classes in System.IOUtils, which are essentially wrapping FindFirst, FindNext etc:

procedure DeleteFilesMatchingPattern(const Directory, Pattern: string);
  var FileName: string;
begin
  for FileName in TDirectory.GetFiles(Directory, Pattern) do TFile.Delete(FileName);
end;
like image 25
DNR Avatar answered Oct 18 '22 15:10

DNR


You can use the SHFileOperation function. The nice thing about using SHFileOperation is you have the option of deleting the files to the recycle bin and you get the normal API animations so the user will know what is going on. The downside is the delete will take a little longer than Jeff's code.

There are several wrappers out there. I use this free wrapper from BP Software. The entire wrapper file is only 220 lines and is easy to read and use. I don't install this as a component. I have found it easier to add this unit to my project and just Create and free the object as needed.

Update: The download link for the BP Software site is no longer valid. There is an older version on the Embarcadero website.

TSHFileOp (1.3.5.1) (3 KB)
May 31, 2006
TComponent that is a wrapper for the SHFileOperation API to copy, move, rename, or delete (with recycle-bin support) a file system object.

The file name parameter for SHFileOperation supports MS DOS style wildcards. So you can use the component like this:


      FileOps := TSHFileOp.Create(self);

      FileOps.FileList.Add(DirectoryName + '\*.jpg');

      FileOps.HWNDHandle := self.Handle;
      FileOps.Action := faDelete;
      FileOps.SHOptions :=
          [ofAllowUndo, ofNoConfirmation, ofFilesOnly, ofSimpleProgress];
      FileOps.Execute;

I usually show the "Are you sure" message myself so I always pass the ofNoConfirmation flag so Windows does not ask again.

If you don't want to delete every jpg file or you need to delete from multiple directories you can add full file names or different paths with wild cards to the FileList string list before calling execute.

Here is the MSDN Page for SHFileOperation
Note that SHFileOperation has been replaced by IFileOperation starting with Windows Vista. I have continued to use SHFileOperation on Windows Vista without any problems.

like image 27
Mark Elder Avatar answered Oct 18 '22 14:10

Mark Elder