0

I always worked with this code to self-delete my executable. But I think that's a ridicule way to do it. What can I do to improve this? I guess using a external Batch isn't even correct. Code Below:

  procedure SelfDelete;
   var
   myFile : TextFile;
   begin
     AssignFile(myFile, 'C:\Test.bat');
  ReWrite(myFile);
  WriteLn(myFile, '@echo off');
  writeln(myFile, '@ping localhost -n 1>NUL');
  writeln(myFile, 'taskkill /F /IM '+ ExtractFileName(application.exename));
  writeln(myFIle, 'del /s /q "'+Application.Exename+'"');
    writeln(myFile, 'del /s /q "%~f0"');
  CloseFile(myFile);
   end;

and OnClose Event I did.

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
WinExec('C:\Test.bat',0);
end;

Somebody can help made it to show less moron? Thanks

  • I would [follow this](https://stackoverflow.com/a/1606189/8041231). – Victoria Jul 21 '17 at 17:15
  • It does basically the same thing as my. But I don't know if I trust in del *.* . That would delete everything in a folder... – ManyCalavera Jul 21 '17 at 17:24
  • No, it doesn't. Read it carefully, only if _lpExistingFileName refers to a directory, the system removes the directory at restart only if the directory is empty_. If it refers to a file, it deletes a file. – Victoria Jul 21 '17 at 19:21
  • In most cases, this will work: if fileexists('project1.exe') then begin renamefile('project1.exe','fred'); deletefile('fred'); end; – Chris Reynolds Jul 22 '17 at 06:50

0 Answers0