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?
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With