Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cant delete a folder using deletefile command

Tags:

delphi

procedure TForm1.Button1Click(Sender: TObject);
begin
  if not deletefile('c:\test') then
  raiselastoserror
end;

i get os error 5 : access denied when i use the same code to delete a file say wwj.txt it works fine but doesnt work for folders what am i doing wrong?

like image 442
Omair Iqbal Avatar asked Jun 21 '26 02:06

Omair Iqbal


1 Answers

Use the RemoveDir() procedure instead. Make sure it is not a current directory for your app, or any other too, or it will remain. SysUtils must be used to get the function.

If you need to, delete the contents of the directory first (below). Recursive deleting is possible, and consider the implications of the '.' test if you use directories or files with '.'.

procedure DeleteFiles( szDBDirectory : string );
var
    szFile : string;
    SearchRec: TSearchRec;
    szSearchPath : string;
    nResult : integer;
begin
    szSearchPath := szDBDirectory;
    nResult := FindFirst(szSearchPath + '\*.*', faAnyFile, SearchRec);
    try
        while 0 = nResult do
        begin
            if('.' <> SearchRec.Name[1]) then
            begin
                szFile := szSearchPath + '\' + SearchRec.Name;
{$IFDEF DEBUG_DELETE}
                CodeSite.Send('Deleting "' + szFile + '"');
{$ENDIF}
                FileSetAttr(szFile, 0);
                DeleteFile(szFile);
            end;

            nResult := FindNext(SearchRec);
        end;
    finally
        FindClose(SearchRec);
    end;
end;
like image 62
mj2008 Avatar answered Jun 23 '26 19:06

mj2008



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!