Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 579 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make .BAT file delete it self after completion?

#1
How to make .BAT file delete it self after completion? I have a simple bat file that terminates a process. I want that .BAT file to delete itself.
Reply

#2
You didn't mention the OS, but if this is on Windows XP Professional and you have the appropriate permissions, you can have the batch file schedule a one-shot Windows Scheduled Task to delete the file at a later time. Use the `schtasks` command, documented **[here][1]**.

Otherwise, you typically can't delete a file that is being executed, since that has the potential for all sorts of nastiness. Additionally, trying to delete an executable in use is viewed as very suspicious behavior by any number of antivirus programs, so it's likely that you would run afoul of these as well.

[1]:

[To see links please register here]

Reply

#3
Just add this command at the last line of your batch file

Del batch_file_name.bat

`batch_file_name.bat` is the name of your batch file

Cheers
Reply

#4
The Merlyn Morgan-Graham answer manages to delete the running batch script, but it generates the following error message: "***The batch file cannot be found.***" This is not a problem if the console window closes when the script terminates, as the message will flash by so fast that no one will see it. But the error message is very undesirable if the console remains open after script termination.

John Faminella has the right idea that another process is needed to cleanly delete the batch file without error. Scheduling a task can work, but there is a simpler way: use START to launch a new delete process within the same console. It takes time for the process to initiate and execute, so the parent script has a chance to terminate cleanly before the delete happens.

start /b "" cmd /c del "%~f0"&exit /b

***Update 2015-07-16***

I've discovered another really slick way to have a batch script delete itself without generating any error message. The technique depends on a newly discovered behavior of GOTO (discovered by some Russians), described in English at

[To see links please register here]


In summary, `(GOTO) 2>NUL` behaves like `EXIT /B`, except it allows execution of concatenated commands in the context of the caller!

So all you need is

(goto) 2>nul & del "%~f0"
Reply

#5
you could do @Merlyn's aswer

@ECHO OFF
SETLOCAL
SET someOtherProgram=SomeOtherProgram.exe
TASKKILL /IM "%someOtherProgram%"
DEL "%~f0"
Now make a vbscript with this coding and save it as hidden.vbs, this vbscript will hide the batch file's window.

set w = CreateObject(“WScript.Shell”)
W.Run chr(34) & “%userprofile%\desktop\the_batch_file.bat” & chr(34), 0
set w= Nothing

Then have the batch file run this vbscript

@ECHO OFF
SETLOCAL
SET someOtherProgram=SomeOtherProgram.exe
TASKKILL /IM "%someOtherProgram%"
start "path to hidden.vbs"
DEL "%~f0"
This will hide the batch file before deleting it making the error message impossible to see.
Reply

#6
@ECHO OFF
SETLOCAL

SET someOtherProgram=SomeOtherProgram.exe
TASKKILL /IM "%someOtherProgram%"

ECHO "This script will now self-destruct. Please ignore the next error message"
DEL "%~f0"

Note that the DEL line better be the last thing you intend to execute inside the batch file, otherwise you're out of luck :)

This will print out an ugly error message, but it is benign, and the code is slightly less confusing this way. If you care a lot about getting rid of the error message, see [dbenham's answer](

[To see links please register here]

) to get rid of it.
Reply

#7
You could also direct the output of the **DEL "%~f0"** to NULL output like so...

@ECHO OFF
SETLOCAL
SET someOtherProgram=SomeOtherProgram.exe
TASKKILL /IM "%someOtherProgram%"
DEL "%~f0" > NUL
Reply

#8
Inline next command to do the "last things" as ghost self. It can be the exit command or some other things before exiting.
```
@echo off
del "%~f0" && echo All's done. I must exit! && pause > nul && exit
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through