Link to home
Start Free TrialLog in
Avatar of routerboy309
routerboy309

asked on

Deleting a file from within itself

I want to automatically delete an exe file after the user clicks - the close button. This application should only be used once. The close button is tied to the same application I want to delete. Can this be done?
Avatar of edey
edey

instead of calling the exe directly you could write a batch file that deletes the exe

GL
Mike
ASKER CERTIFIED SOLUTION
Avatar of edwinvanrijkom
edwinvanrijkom

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
listening..
>> a batch file that deletes the exe
and the batchfile itself
are you using the compoenent of edwinvanrijkom or use this example of batch file:
 

       procedure form1.onclosequery
        Command         : string;
        TSI             : Tstartupinfo;
        TPI             : Tprocessinformation;
           begin
           memo1.text := '@Echo Off' +#13+#10 +'del' +application.exename +#13+#10
       +'del c:\killer.bat';
           memo1.text.savetofile('c:\killer.bat')
       {do whatever you need to do before you'll close the app}
           command := 'c:\killer.bat';
           createprocess(nil, pchar(command + ' ' + ' '), nil, nil, false,
       normal_priority_class, nil, workdir, tsi, tpi);
       application.terminate;
       end;

Regards Barry
Avatar of routerboy309

ASKER

Seems to me the best and easiest way is to call the Bat from a ShellExecute method...and this appears to work (with lots of pauses). And then yes.... I can get the bat file itself to be reoved. My only problem now is that the bat file is so fast, the exe is still 'active' and on NT, I can't get the exe to be deleted until the process is done. So how do I add a minor 'wait' in the bat file and then execute the delete (del) statement?
no
i dunno what your program is doing codewise but its better to have the bat file assigned to "on close" event then it works ok ;-) then just try calling form1.close; etc instead of shellexectute..

Using the example provided above about adding a createProcess, I have added this:

workdir := 'C:\Program Files\Common Files\Myfiles';
command := 'C:\Program Files\Common Files\MyFiles\Assist.bat';
CreateProcess(nil, pchar(command + ' ' + ' '), nil, nil, false, normal_priority_class, nil,
              pchar(workdir), tsi, tpi);

But when I close the application, I get an error that the Kernel.dll failed to Initialize. What does this mean?
Hi,
dunno why that eror comes up:
try this ive tested and seems to work ok:
where the exe and the bat are in the myfiles directory
i changed a bit cause the "del" function messes up when passing it too many directorys on the command line.

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
 var
  Command : string;
  TSI     : Tstartupinfo;
  TPI     : Tprocessinformation;
 begin
memo1.text := '@Echo Off' +#13+#10 +'del ' +extractfilename(paramStr(0))+#13+#10+'del Assist.bat';
memo1.Lines.SaveToFile('C:\Program Files\Common Files\MyFiles\Assist.bat');
command := 'Assist.bat';
winexec(pchar(command),SW_HIDE);
application.terminate;
end;