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
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.
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
>.
>
> 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
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
> 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
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?
> 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 my answer wasn't there<g> (assuming that it even works).
Actually, I missed the question on the first go around.
Rick - MVP
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"
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
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.
Yes.
"Bob Butler" <tire...@nospam.com> wrote in message
news:uPSNm8v1...@TK2MSFTNGP11.phx.gbl...
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/>
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
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.
> >> 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,
>
>> 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.
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.
> 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...