Recent

Author Topic: DeleteDirectory doesn't always work - I need to delete even read-only files  (Read 535 times)

AMJF

  • Jr. Member
  • **
  • Posts: 53
I have a program that generates a lot of temporary data in a specified folder and need a means of deleting it all, even if some of it is read-only, but the FileUtils DeleteDirectory doesn't touch the folder if any of the files or folders have the read-only attribute set. Is there a more robust way of deleting this TEMP folder?

jamie

  • Hero Member
  • *****
  • Posts: 6964
Are all the files closed before you do this?

jamie
The only true wisdom is knowing you know nothing

AMJF

  • Jr. Member
  • **
  • Posts: 53
They are files intended for an emulator, and the emulator is usually closed before I close the program.

I did a test, setting all of the files and folders in the folder to read-only, and it wouldn't touch them.

Maybe a "RMDIR" command executed straight from the program? Or any CMD command, for that matter?

jamie

  • Hero Member
  • *****
  • Posts: 6964
I just looked at the MS doc, it seems you are on windows and it clearly states Read only files need the Read only bit cleared first.

 So you need to collect the files first and change the ATT

 In any case, why not make a collection of the READ only files while you processing them and when done you can then change the ATT on them or even delete them while you are there?

The only true wisdom is knowing you know nothing

AMJF

  • Jr. Member
  • **
  • Posts: 53
Can't I just run batch file (DOS) commands via Lazarus IDE, which would avoid all of that fiddly stuff?

The only alternative I have is to run an actual batch file with the RMDIR command to delete the files, outside of the program, but I'd like to build it into the program itself.

Bart

  • Hero Member
  • *****
  • Posts: 5573
    • Bart en Mariska's Webstek
You can use FindAllFiles to create a list of files to be deleted.
Then for each entry in the list set attribute it's attribute to faArchive, then delete the file.

Bart

ASerge

  • Hero Member
  • *****
  • Posts: 2423
Code: Pascal  [Select][+][-]
  1. uses ShellApi;
  2.  
  3. function DeleteDir(const Dir: string): Boolean;
  4. var
  5.   R: TSHFileOpStructW;
  6.   U: UnicodeString;
  7. begin
  8.   FillChar(R, SizeOf(R), 0);
  9.   R.wFunc := FO_DELETE;
  10.   U := UTF8Decode(Dir) + #0;
  11.   R.pFrom := PUnicodeChar(U);
  12.   R.fFlags := FOF_NOCONFIRMATION or FOF_SILENT or FOF_NOERRORUI;
  13.   Result := SHFileOperationW(@R) = 0;
  14. end;

 

TinyPortal © 2005-2018