windows 如何使批处理文件自行删除?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20329355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:08:29  来源:igfitidea点击:

How to make a batch file delete itself?

windowsbatch-filewindows-7delete-fileelevated-privileges

提问by 09stephenb

Is it possible to make a batch file delete its self?

是否可以使批处理文件自行删除?

I have tried to make it execute another file to delete it but this did not work does any one know how I could do it. The batch file I am using is elevated. My OS is windows 7 32 bit.

我试图让它执行另一个文件来删除它,但这不起作用有人知道我该怎么做。我正在使用的批处理文件已提升。我的操作系统是 Windows 7 32 位。

回答by dbenham

npocmaka's answer works, but it generates the following error message: "The batch file cannot be found." This isn't a problem if the console window closes when the script terminates, as the message will flash by so fast, no one will see it. But it is very undesirable if the console remains open after the script terminates.

npocmaka 的答案有效,但它会生成以下错误消息:“找不到批处理文件。”如果在脚本终止时控制台窗口关闭,这不是问题,因为消息会闪烁得如此之快,没人会看到它。但是如果在脚本终止后控制台仍然打开,这是非常不可取的。

The trick to deleting the file without an error message is to get another hidden process to delete the file after the script terminates. This can easily be done using START /B to launch a delete process. It takes time for the delete process to initiate and execute, so the parent script has a chance to terminate cleanly before the delete happens.

在没有错误消息的情况下删除文件的技巧是让另一个隐藏进程在脚本终止后删除文件。这可以很容易地使用 START /B 启动删除过程来完成。删除过程的启动和执行需要时间,因此父脚本有机会在删除发生之前彻底终止。

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

You can simply use a CALLed subroutine if you are worried about SHIFT trashing the %0value.

如果您担心 SHIFT 破坏%0值,您可以简单地使用 CALLed 子例程。

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

Update 2015-07-16

更新 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

我发现了另一种非常巧妙的方法,可以让批处理脚本自行删除而不生成任何错误消息。该技术依赖于新发现的 GOTO 行为(由一些俄罗斯人发现),英文描述在http://www.dostips.com/forum/viewtopic.php?f=3&t=6491

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

总之,(GOTO) 2>NUL行为类似于EXIT /B,除了它允许在调用者的上下文中执行串联命令!

So all you need is

所以你只需要

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

The returned ERRORLEVEL will be 0 because DEL is the last command and it always clears the ERRORLEVEL.

返回的 ERRORLEVEL 将为 0,因为 DEL 是最后一个命令,它总是清除 ERRORLEVEL。

If you need to have control of the ERRORLEVEL, then something like

如果您需要控制 ERRORLEVEL,则类似于

(goto) 2>nul & del "%~f0" & cmd /c exit /b 10

回答by npocmaka

( del /q /f "%~f0" >nul 2>&1 & exit /b 0  )

Set this at the end of the script. (might not work if SHIFTcommand is used)

在脚本的末尾设置它。(如果使用SHIFT命令可能不起作用)

回答by JeffRSon

del "%~f0"

will work, but there's an error message if you call it from a previously open console (can be ignored however).

会起作用,但是如果您从以前打开的控制台调用它,则会出现错误消息(但是可以忽略)。

回答by archer

del %0

As the last line of the file is the easiest way I've found. %0returns the name of the currently executing .cmdfile.

由于文件的最后一行是我找到的最简单的方法。 %0返回当前正在执行的.cmd文件的名称。

When I first tried it, I thought I would get a "file in use" error, but that hasn't happened so far.

当我第一次尝试时,我以为我会收到“文件正在使用”错误,但到目前为止还没有发生。

回答by archer

As an answer to this question, (which is put on hold) this batch will selfdestruct after three runs. The code to achieve this is condensed in the last 3 lines. It incorporates the above tip from dbenham

作为这个问题的答案,(被搁置)该批次将在三轮运行后自毁。实现此目的的代码浓缩在最后 3 行中。它包含了来自 dbenham上述提示

@Echo off
Rem batch main purpose
Rem
Rem
Set Tok=###&For /f %%C in ('find /C "%Tok%" ^<"%~f0"') Do Set /A Cnt=%%C
Echo Run-%Cnt%&If %Cnt% Geq 3 (goto) 2>nul & del "%~f0"
Echo %Tok%>>"%~f0"& Goto :Eof

The batch modifies itself by appending a token after each run and counting the occurences of the token. If the number is reached it deletes itself.

批处理通过在每次运行后附加一个令牌并计算令牌出现的次数来修改自身。如果达到该数字,它会自行删除。

> Dir /B Kill*
KillMySelf.cmd

> KillMySelf.cmd
Run-1

> KillMySelf.cmd
Run-2

> KillMySelf.cmd
Run-3

> Dir /B Kill*
File Not Found

回答by Massimo

you can set the delay to avoid race conditions, here is 15 seconds :

您可以设置延迟以避免竞争条件,这里是 15 秒:

START /B CMD.EXE /D /C "PING.EXE -n 15 127.0.0.1 && DEL prova.bat"

NOTES

笔记

  • CMD.EXE /D is mandatory to avoid execution of auto-run crap-ware
  • PING and DEL must be executed syncronous, so surrounded by quotes
  • I let to the users how to pass the batch name using "%~f0"
  • CMD.EXE /D 是强制性的,以避免执行自动运行的垃圾软件
  • PING 和 DEL 必须同步执行,所以用引号括起来
  • 我让用户如何使用“%~f0”传递批次名称

回答by Ace

In case someone else was trying to use user6811411's 3 run and then self destruct script and it was not working. I figured out the &'s were not being interpreted right. My solution was just to separate the commands like below.

如果其他人试图使用 user6811411 的 3 运行然后自毁脚本并且它不起作用。我发现 & 的解释不正确。我的解决方案只是将如下命令分开。

@Echo off
Rem batch main purpose
Rem
Rem
Set Tok=###
For /f %%C in ('find /C "%Tok%" ^<"%~f0"') Do Set /A Cnt=%%C
Echo Run-%Cnt%
If %Cnt% Geq 3 (goto) 2>nul & del "%~f0"
Echo %Tok%>>"%~f0" & Goto :Eof

回答by ThE WAtChEr

You can use del (name)if it on the desktop otherwise use del (path to file ex. del C:\WINDOWS.

del (name)如果它在桌面上,则可以使用,否则使用del (path to file ex. del C:\WINDOWS.

P.S Does not make error msg

PS 不报错 msg

回答by Sam

del %0 as the last line of the batch file

del %0 作为批处理文件的最后一行