Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Delete EXE

3 views
Skip to first unread message

manjunath

unread,
Jan 9, 2004, 6:12:20 AM1/9/04
to
i want to develop a EXE which will delete itself when it
complets it's assigned task, is it possible, do i need one
more exe to delete my EXE.

i want to delete file by itself, no other exe or program
is required to to the job.

please help me.

thanks in advance
manjunath

Phill. W

unread,
Jan 9, 2004, 11:36:41 AM1/9/04
to
"manjunath" <matti...@yahoo.com> wrote in message
news:0b9201c3d6a1$7325c640$a101...@phx.gbl...
. . .

> i want to develop a EXE which will delete itself
. . .

> i want to delete file by itself, no other exe or program
> is required to to the job.

Can't be done, unless you want to leap into .Net. Windows
"locks" the running Executable on disk and it cannot be removed
until the program /stops/ running (so it can't delete itself).

You'll need /some/ external entity, even if it's a batch script that
your program writes then Shell()s out as it shuts down.

HTH,
Phill W.


Andy

unread,
Jan 9, 2004, 12:49:54 PM1/9/04
to
I don't think you can make your EXE commit suicide :-)

You will get something like "File is in use, can not
delete it."

You may want to create another exe that will be
the 'executor' of your original EXE

--- Andy

>.
>

Gary Nelson

unread,
Jan 9, 2004, 12:55:43 PM1/9/04
to
manjunath,

> i want to develop a EXE which will delete itself when it
> complets it's assigned task, is it possible, do i need one
> more exe to delete my EXE.
>
> i want to delete file by itself, no other exe or program
> is required to to the job.
>
> please help me.

Try this (where the exe file's name is Myexe.exe):

ChDir App.Path
Close
Open "DelUs.bat" For Output As 1
Print #1, "@Echo off"
Print #1, ":Repeat"
Print #1, "del Myexe.exe"
Print #1, "if exist Myexe.exe goto Repeat"
Print #1, "del DelUS.bat"
Close #1
retval = Shell("DelUs.bat", 0)
End


Rick Rothstein

unread,
Jan 9, 2004, 1:56:35 PM1/9/04
to
> i want to develop a EXE which will delete itself when it
> complets it's assigned task, is it possible, do i need one
> more exe to delete my EXE.
>
> i want to delete file by itself, no other exe or program
> is required to to the job.
>
> please help me.

I think putting this code in the Form's Terminate event will work

Shell Environ("comspec") & " /c del """ & _
App.Path & IIf(Right$(App.Path, 1) = "\", "", "\") & _
App.EXEName & ".exe""", vbHide

Rick - MVP


Jonathan Wood

unread,
Jan 9, 2004, 2:03:46 PM1/9/04
to
manjunath,

> i want to develop a EXE which will delete itself when it
> complets it's assigned task, is it possible, do i need one
> more exe to delete my EXE.
>
> i want to delete file by itself, no other exe or program
> is required to to the job.

I once wrote some code that ran an EXE and then deleted it when it was
finished. So, for starters, I would say that you definitely need a second
EXE (or maybe a batch file).

However, I also found some operating systems would not let me delete the
file as soon as it finished. The approach I ended up taking was as follows:

First, I launched the application using CreateProcess and then called
WaitForSingleObject on the process handle. When WaitForSingleObject
returned, I then called Sleep(1000) before deleting the file. I found that
some systems would produce an 'Access Denied' error without the call to
Sleep.

It seems to work okay for me.

--
Jonathan Wood
SoftCircuits
http://www.softcircuits.com
Available for consulting: http://www.softcircuits.com/jwood/resumes.htm


Andrew Faust

unread,
Jan 9, 2004, 3:21:03 PM1/9/04
to
On Fri, 9 Jan 2004 03:12:20 -0800 "manjunath" <matti...@yahoo.com> blurted out:

I love when people bother to check google groups for an answer first. This
exact question was asked and answered only 2 days ago.

Andrew Faust

Where are we going and why am I in this handbasket?

Jonathan Wood

unread,
Jan 9, 2004, 3:27:22 PM1/9/04
to
Andrew,

> I love when people bother to check google groups for an answer first. This
> exact question was asked and answered only 2 days ago.

Personally, I don't see any problem. If they can find it on Google, fine.
Otherwise, why not post a question? Isn't that what the newsgroups are for?

Rick Rothstein

unread,
Jan 9, 2004, 3:32:23 PM1/9/04
to
> I love when people bother to check google groups for an answer
> first. This exact question was asked and answered only 2 days ago.

Yes, but my answer wasn't there<g> (assuming that it even works).

Actually, I missed the question on the first go around.

Rick - MVP


Bob Butler

unread,
Jan 9, 2004, 3:41:08 PM1/9/04
to
"Rick Rothstein" <rickNOS...@NOSPAMcomcast.net> wrote in message
news:uckpLJu1...@TK2MSFTNGP12.phx.gbl

It looks right but I've seen that sort of thing fail because the DOS process
actually runs before the lock on the EXE has been released. You need
something that will wait for a short time and/or retry.

--
Reply to the group so all can participate
VB.Net... just say "No"

Rick Rothstein

unread,
Jan 9, 2004, 4:23:57 PM1/9/04
to
> >> i want to develop a EXE which will delete itself when it
> >> complets it's assigned task, is it possible, do i need one
> >> more exe to delete my EXE.
> >>
> >> i want to delete file by itself, no other exe or program
> >> is required to to the job.
> >>
> >> please help me.
> >
> > I think putting this code in the Form's Terminate event will work
> >
> > Shell Environ("comspec") & " /c del """ & _
> > App.Path & IIf(Right$(App.Path, 1) = "\", "", "\") & _
> > App.EXEName & ".exe""", vbHide
>
> It looks right but I've seen that sort of thing fail because the DOS
process
> actually runs before the lock on the EXE has been released. You need
> something that will wait for a short time and/or retry.

Would putting a DoEvents before the Shell command do anything for the
problem (or is your thread completely out of the picture by then)?

Rick - MVP


Bob Butler

unread,
Jan 9, 2004, 5:23:01 PM1/9/04
to
"Rick Rothstein" <rickNOS...@NOSPAMcomcast.net> wrote in message
news:%23UGaibv...@tk2msftngp13.phx.gbl

Ummm... if you added DoEvents you'd delay your app before it does the shell
and then exits... it's the "and then exits" part that can take some time to
complete so I would not expect it to make a difference. Cleaning up all
forms/files/etc might help but I think thread/process termination is
probably lower priority than process creation so there's no good way I know
of to ensure that the DOS command execution is delayed until after the EXE
is completely gone.

Bonj

unread,
Jan 9, 2004, 9:26:10 PM1/9/04
to
There could be at least several different thread switchings during the
process of the DOS process starting, and in the cleanup of the VB
application. DoEvents would just get lost wouldn't it? seeing as there's
still the same amount of code to run after it.

"Rick Rothstein" <rickNOS...@NOSPAMcomcast.net> wrote in message
news:%23UGaibv...@tk2msftngp13.phx.gbl...

Bonj

unread,
Jan 9, 2004, 9:27:24 PM1/9/04
to

> Personally, I don't see any problem. If they can find it on Google, fine.
> Otherwise, why not post a question? Isn't that what the newsgroups are
for?

Yes.

Bonj

unread,
Jan 9, 2004, 9:27:50 PM1/9/04
to
Try the batch file Gary Nelson posted.

"Bob Butler" <tire...@nospam.com> wrote in message
news:uPSNm8v1...@TK2MSFTNGP11.phx.gbl...

Bob O`Bob

unread,
Jan 9, 2004, 11:45:52 PM1/9/04
to


I had a couple of minor quibbles with this the last time I saw it posted.
I think this fixes those, so I'll contribute it to the newsgroup's
collective codebase. Thanks for the head start.


Public Sub KillMe()
Dim ff As Integer, batname As String, ext As String, batnum As Long
ChDir App.Path
ext = ".bat"
Do
batname = "DelMe" & Format(batnum) & ext
batnum = batnum + 1
Loop While Len(Dir(batname))
ext = ".exe"
ff = FreeFile
Open batname For Output As ff


Print #1, "@Echo off"
Print #1, ":Repeat"

Print #1, "del """ & App.EXEName & ext & """"
Print #1, "if exist """ & App.EXEName & ext & """ goto Repeat"
Print #1, "del " & batname
Close #1
batnum = Shell(batname, 0)
End
End Sub

Bob
--
VB expert looking for work <http://obob.com/bob/resume/>

Bob Butler

unread,
Jan 10, 2004, 10:16:19 AM1/10/04
to
"Bob O`Bob" <filt...@yahoogroups.com> wrote in message
news:3FFF83...@yahoogroups.com
<cut>
> End

End?
END??

Rick Rothstein

unread,
Jan 10, 2004, 10:36:57 AM1/10/04
to
> <cut>
> > End
>
> End?
> END??

Well.... that would take care of the "it's the 'and then exits' part that
can take some time to complete" which you posted against one of my messages,
wouldn't it?<g>

Rick - MVP


Bob Butler

unread,
Jan 10, 2004, 11:28:56 AM1/10/04
to
"Rick Rothstein" <rickNOS...@NOSPAMcomcast.net> wrote in message
news:O7GCS%2341DH...@TK2MSFTNGP11.phx.gbl

LOL
It'd help since you'd circumvent some of the cleanup but you still have to
wait for the OS to tear down the process and that takes time as well. I
just could not believe my eyes when I saw that.

Bob O`Bob

unread,
Jan 10, 2004, 11:38:13 AM1/10/04
to
Bob Butler wrote:

> >> END??
> >
> > Well.... that would take care of the "it's the 'and then exits' part
> > that can take some time to complete" which you posted against one of
> > my messages, wouldn't it?<g>
>
> LOL
> It'd help since you'd circumvent some of the cleanup but you still have to
> wait for the OS to tear down the process and that takes time as well. I
> just could not believe my eyes when I saw that.

Hey, nobody said this was a good idea or *should* be done...

just that if someone's dead-set on doing it, it might as well be done as effectively
as can be. If someone uses it without doing a proper cleanup first, that's a
different issue from deleting the file without leaving other files.

Andrew Faust

unread,
Jan 12, 2004, 11:25:45 AM1/12/04
to
On Fri, 9 Jan 2004 13:27:22 -0700 "Jonathan Wood" <jw...@softcircuits.com> blurted out:

>Andrew,
>
>> I love when people bother to check google groups for an answer first. This
>> exact question was asked and answered only 2 days ago.
>
>Personally, I don't see any problem. If they can find it on Google, fine.
>Otherwise, why not post a question? Isn't that what the newsgroups are for?

Yes, but often we see the same questions re-asked many times. It can be a bit
annoying to answer a question then have that same question asked again
just a few days later.

Andrew Faust

unread,
Jan 12, 2004, 11:27:22 AM1/12/04
to
On Fri, 9 Jan 2004 15:32:23 -0500 "Rick Rothstein" <rickNOS...@NOSPAMcomcast.net> blurted out:

Really, the only reason I jumped on him was because my answer was there,
and I got a bit annoyed that I spend the time to type in an answer to the
question only to have someone else ask the exact same question 2 days
later.

I'm sure most of the other regulars here have felt the same way at one
time or another.

Bob O`Bob

unread,
Jan 12, 2004, 11:45:05 AM1/12/04
to
Andrew Faust wrote:
> On Fri, 9 Jan 2004 15:32:23 -0500 "Rick Rothstein" <rickNOS...@NOSPAMcomcast.net> blurted out:
>
> >> I love when people bother to check google groups for an answer
> >> first. This exact question was asked and answered only 2 days ago.

> Really, the only reason I jumped on him was because my answer was there,


> and I got a bit annoyed that I spend the time to type in an answer to the
> question only to have someone else ask the exact same question 2 days
> later.
>
> I'm sure most of the other regulars here have felt the same way at one
> time or another.


I know I have ...
but to a significant extent don't you think the burden for that really should be
on _us_ to gather and somehow publish a real FAQ list for the group?
In the absence of any group FAQ list, it's hardly surprising the questions
keep coming back.


I've been thinking about how it might be done for a long time, but the fact is that I
haven't stepped up to the plate either. One of my ideas, though, was to gather the
questions by community effort (i.e., have a running thread or threads about it) and then
to have someone periodically post the actual message, whether manually or automatically
(that much is bog-standard FAQ procedure) but one detail I thought I'd suggest was to
include google links to past postings as the answers, rather than incorporate all the
text directly. I was thinking those links may be easier to update (when variations get
posted, and are considered good, just add them - each question may have multiple links
pointing to answering posts), and also a better way to give credit where it's due
to those who've contributed the FAQ-worthy material.

Bob
but heck, what do I know? I haven't even had my coffee this morning...

0 new messages