windows bat文件是否知道它的名字,它可以删除自己吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3849095/
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
Does a bat file know its name and can it delete itself
提问by Berming
At some point in my script, I'd like the bat script to delete itself. This requires that the script know its name and then to use that name to delete itself. Is this possible?
在我的脚本中的某个时候,我希望 bat 脚本删除自己。这要求脚本知道其名称,然后使用该名称删除自身。这可能吗?
回答by dbenham
None of the existing answers provide a clean way for a batch file to delete itself and exit without any visible error message.
现有的答案都没有为批处理文件提供一种干净的方法来删除自身并退出而没有任何可见的错误消息。
Simply including del "%~f0"
does delete the script, but it also results in an ugly "The batch file cannot be found."error message.
简单地包含del "%~f0"
确实删除了脚本,但它也会导致一个丑陋的“找不到批处理文件”。错误信息。
If it is OK for the script to close the parent CMD process (the console window), then the following works fine without any error message:
如果脚本可以关闭父 CMD 进程(控制台窗口),那么下面的工作正常,没有任何错误消息:
del "%~f0"&exit
But it is a bit more complicated if you want the script to delete itself and exit withoutclosing the parent CMD process, and without any error message.
但是,如果您希望脚本在不关闭父 CMD 进程的情况下删除自身并退出,并且没有任何错误消息,那就有点复杂了。
Method 1: Start a second process that runs within the same console window that actually performs the delete. The EXIT /B
is probably not necessary, but I put it in to maximize the probability that the batch script will close before the STARTed process attempts to delete the file.
方法 1:启动在实际执行删除的同一控制台窗口中运行的第二个进程。这EXIT /B
可能不是必需的,但我将其放入以最大化批处理脚本在 STARTed 进程尝试删除文件之前关闭的可能性。
start /b "" cmd /c del "%~f0"&exit /b
Method 2: Create and transfer control to another temp batch file that deletes itself as well as the caller, but don't use CALL, and redirect stderr to nul.
方法 2:创建并将控制权转移到另一个临时批处理文件,该文件删除自身以及调用方,但不要使用 CALL,并将 stderr 重定向到 nul。
>"%~f0.bat" echo del "%~f0" "%~f0.bat"
2>nul "%~f0.bat"
回答by JoshD
If I'm not mistaken, %0 will be the path used to call the batch file.
如果我没记错的话,%0 将是用于调用批处理文件的路径。
回答by ubiquibacon
Tested and working:
测试和工作:
del %0
exit
回答by peterchen
%0 gives you the relative path the from the directory where the bat file was started. So if you call it
%0 为您提供 bat 文件启动目录的相对路径。所以如果你叫它
mybats\delyourself.bat tango roger
%0 will contain mybats\delyourself.bat
%0 将包含 mybats\delyourself.bat
del %0
works if you haven't changed your current directory.
del %0
如果您没有更改当前目录,则有效。
%~f0
exapnds to the full path to the file.
%~f0
扩展到文件的完整路径。
回答by be981
For me it was important, that I have noerrorLevel 1 after I exited the batch file. I found an suitable and easy answer this way: DeleteMyself.cmd:
对我来说,退出批处理文件后没有错误级别 1很重要。我通过这种方式找到了一个合适且简单的答案: DeleteMyself.cmd:
START CMD /C DEL DeleteMyself.cmd
Test batch if DeleteMyself.cmd is returning errorLevel 0. TestDeleteMyself.cmd:
如果 DeleteMyself.cmd 返回 errorLevel 0,则测试批处理。TestDeleteMyself.cmd:
call DeleteMyself.cmd
echo Errorlevel: %errorLevel%