Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a program delete himself?

Status
Not open for further replies.

cyberwolf14

Programmer
Aug 27, 2001
65
BE
Hello,

Does somebody now how I can a make a program that deletes himself??
 
You find the EXE file and do a Kill(Filename)

Though i am not sure you can delete the EXE while it runs. Craig, mailto:sander@cogeco.ca

Remember not to name the Lambs...
It only makes the chops harder to swallow
 
No, it doesn't work:
run-time error '75'
Path/file access error
 
Hey, that's cool. I didn't know something as simple as Kill() is located in VB. I think I know what your problem is cyberwolf14. When I did it the first time, I just used another vb project that I didn't want and it said the same thing. I think you can only use exe files under kill(), as craigsander implied. Maybe if you make an exe out of this program itself and use that filename?? All I know is there is some way to assign files to variables and then use the .Delete function, but I'm not positive. There should be another thread out there right now with that info.
 
What you can do, is have the program that wants to delete itself, shell out to another program, sending its own pathname as a parameter. Then terminate yourself. The second program can then Kill the original
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
That's a good idea, because I ran an exe with that code to delete itself and it said that same error 75.
 
It's very difficult if I have to work with 2 programs.
Isn't there an easier way??
 
You cannot delete a file that is in use, so as long as your program is running, it cannot be deleted. You'll get the file in use error that we already know about.

Without using a second program - you could create a DOS Batch file - from within the program as a text output file, and write the necessary commands to the batch file which will delete your exe. Shell to the bat file, and immediately terminate.

You will have a timing issue in that the batch file will not be able to execute the delete until your program has completely terminatated, and Windows has release all of the handles to your exe. The Bat file will probably have to do some sort of file checking and/or looping to get around this timing issue.
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
It's the only way I know to do it, but I used a BAT file instead of creating a second EXE.

Put this in the event that exits your program. Do not put it in the form terminate or deactivate event, as it will not work there.

' write the bat file to erase the .exe
Dim Killpath As String
Dim fso, MyFile

Killpath = App.Path & "\Yourprogramname.exe"

Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("C:\Killit.bat", True)
MyFile.WriteLine (":START")
MyFile.WriteLine ("del " & Killpath)
MyFile.WriteLine ("IF EXIST C:\Killit.exe GOTO START")
MyFile.WriteLine ("del C:\killit.bat")
MyFile.WriteLine ("EXIT")
MyFile.Close

SH = Shell("C:\killit.bat", vbHide)

End

This writes a simple BAT file and then shells it out just before the program ends. The BAT file will try to delete the EXE file. If it is succesful, it then deletes itself and then exits. If it is not succesful at deleting the EXE ( because the EXE has not finished closing yet, and can't be deleted ), it will loop around and try again and again until it does finally delete the file. This only takes a second or two, and it is hidden from the end user.

If your application is always going to reside in the same place ( the app path is always the same ), you can simplify the code a bit.

HTH

Robert

 
Oopps. The line

MyFile.WriteLine ("IF EXIST C:\Killit.exe GOTO START")


Should read:
MyFile.WriteLine ("IF EXIST " & Killpath & " GOTO START")

( Killit.exe was the test program I wrote to test this... )

My bad,

Robert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top