Marcel
Marcel

Reputation: 906

Delete Files Without Popup's Using Delphi

I am using the below code to delete the contents of a folder, the only issue I have now is that if 1 of the files is in use, it pops a confirmation dialog, and then starts showing the progress to the user of the software.

Since these are temp files user's are completely baffled by the popup and keep asking about it.

Is there some way to tweak it, so that if a file is in use or cannot be deleted, that the code will just move on to the next file and ignore this one until the next attempt.

Code

procedure DelFilesFromDir(Directory, FileMask: string; DelSubDirs: Boolean);
var
  SourceLst: string;
  FOS: TSHFileOpStruct;
begin
  FillChar(FOS, SizeOf(FOS), 0);
  FOS.Wnd := Application.MainForm.Handle;
  FOS.wFunc := FO_DELETE;
  SourceLst := Directory + '\' + FileMask + #0;
  FOS.pFrom := PChar(SourceLst);
  if not DelSubDirs then
    FOS.fFlags := FOS.fFlags OR FOF_FILESONLY;
  FOS.fFlags := FOS.fFlags OR FOF_NOCONFIRMATION;
  SHFileOperation(FOS);
end;

Upvotes: 1

Views: 1045

Answers (1)

SilverWarior
SilverWarior

Reputation: 8341

In order to avoid showing any dialog when an error is encountered, you should add FOF_NOERRORUI as an additional flag.

Also, if you want to hide information about the file operation progress entirely, you can add the FOF_NO_UI flag instead.

You can read more about the possible flags in the official SHFileOperation() documentation.

Upvotes: 5

Related Questions