开发者

How to make .BAT file delete it self after completion?

开发者 https://www.devze.com 2022-12-31 21:40 出处:网络
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.The Merlyn Morgan-Graham answer manag

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.


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 http://www.dostips.com/forum/viewtopic.php?f=3&t=6491

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"


@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 get rid of it.


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.

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.


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


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.


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消