deleteing a file (itself)

Is it possible to delete a file that is running - ie stop it running and delete it - without using an external program?

Is there an api call or something that can be called to do this?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

There is a API call DeleteFile("MyFile.exe") try this.
You'll need to terminate the process first
wormboy__6Author Commented:
But if i terminate the process then i cannot call the delete my application is no longer running.

So basically is it possible?
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Do you need to do it immediately or can the delete happen the next time the computer reboots?
wormboy__6Author Commented:
Immediatly...or a few seconds later.

Itd be easy to do it when the computer rebooted. But what i want is deletion basically straight away.
Good Morning Wormboy!

Try something like this:

Private Sub cmdKill_Click()
    Dim RetVal
    Open "Test.bat" For Output As #1
    Print #1, ":start"
    Print #1, "@del Test.exe"
    Print #1, "@dir"
    Print #1, "@if exist Test.EXE goto :start"
    Print #1, "@del test.bat"
    Close #1

    RetVal = Shell("test.bat", 6)
End Sub

Basically, a batch file gets created that tries to delete the program, and it loops until the program terminates. When the program does terminate, the batch file erases itself.

Obviously, the program is called test.exe.


Daniel       :-)
wormboy__6Author Commented:
sorry but then it leaves that file there and i would like a way to remove a file without leaving any other files behind.
Did you test the example? On my machine, it doesn't leave anything...

I tested it on Windows 98 and NT4.0

Daniel      :-)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wormboy__6Author Commented:
oops sorry - i did it wrong

ok, thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.