Recursively delete files and skip files that are in use?

791 Views Asked by At

I'm using Windows API to recursively delete many files and folders. I'm using it without a UI and suppressing errors. The problem is, it completely fails if one of those files is in use. I expect that possibility, and want this to continue anyway, skipping any such cases. The one file which fails is actually the same EXE which is calling this delete command (which will be deleted after it's all done anyway.

Here's what I'm doing now:

procedure DeleteDirectory(const DirName: string);
var
  FileOp: TSHFileOpStruct;
begin
  FillChar(FileOp, SizeOf(FileOp), 0);
  FileOp.wFunc := FO_DELETE;
  FileOp.pFrom := PChar(DirName+#0);//double zero-terminated
  FileOp.fFlags := FOF_SILENT or FOF_NOERRORUI or FOF_NOCONFIRMATION;
  SHFileOperation(FileOp);
end;

How can I make this skip any event of a file being in use? I looked at the documentation but can't find anything that can do this.

2

There are 2 best solutions below

5
R.P Silveira On

Here is just an idea you can implement in your function to validate if there is any file in use:

function IsFileInUse(FileName: TFileName): Boolean;
var
  HFileRes: HFILE;
begin
  Result := False;
  if not FileExists(FileName) then
    Exit;

  HFileRes := CreateFile(PChar(FileName),
                         GENERIC_READ or GENERIC_WRITE,
                         0,
                         nil,
                         OPEN_EXISTING,
                         FILE_ATTRIBUTE_NORMAL,
                         0);
  Result := (HFileRes = INVALID_HANDLE_VALUE);

  if not Result then
    CloseHandle(HFileRes);
end;

Maybe it can help you.

2
R.P Silveira On

Why don't you try this:

procedure DeleteFiles(const DirName: String);
var
  SR: TSearchRec;
  i: Integer;
begin
  //get all files in directory
  i := FindFirst(DirName +'\*.*', faAnyFile, SR);

  while i = 0 do
  begin
    if (SR.Attr and faDirectory) <> faDirectory then
      DeleteFile(DirName +'\'+ SR.Name);

    i := FindNext(SR);
  end;

  FindClose(SR);
end;

It's another way to do it.