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脚本能够自动删除。这要求脚本知道它的名称,然后使用该名称来删除它自己。这是可能的吗?
5 个解决方案
#1
11
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 without closing 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可能不是必需的,但是我将它放入到最大限度,以便在开始的进程尝试删除文件之前关闭批处理脚本。
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:将控制创建和传输控制到另一个临时批处理文件,该文件删除自己和调用者,但不要使用调用,并将stderr重定向到nul。
>"%~f0.bat" echo del "%~f0" "%~f0.bat"
2>nul "%~f0.bat"
#2
3
If I'm not mistaken, %0 will be the path used to call the batch file.
如果我没有弄错的话,%0将是用来调用批处理文件的路径。
#3
2
Tested and working:
测试和工作:
del %0
exit
#4
1
%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 exapnds到文件的完整路径。
#5
-1
For me it was important, that I have no errorLevel 1 after I exited the batch file. I found an suitable and easy answer this way: DeleteMyself.cmd:
对我来说很重要的一点是,在我退出批处理文件之后,我没有errorLevel 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%
#1
11
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 without closing 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可能不是必需的,但是我将它放入到最大限度,以便在开始的进程尝试删除文件之前关闭批处理脚本。
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:将控制创建和传输控制到另一个临时批处理文件,该文件删除自己和调用者,但不要使用调用,并将stderr重定向到nul。
>"%~f0.bat" echo del "%~f0" "%~f0.bat"
2>nul "%~f0.bat"
#2
3
If I'm not mistaken, %0 will be the path used to call the batch file.
如果我没有弄错的话,%0将是用来调用批处理文件的路径。
#3
2
Tested and working:
测试和工作:
del %0
exit
#4
1
%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 exapnds到文件的完整路径。
#5
-1
For me it was important, that I have no errorLevel 1 after I exited the batch file. I found an suitable and easy answer this way: DeleteMyself.cmd:
对我来说很重要的一点是,在我退出批处理文件之后,我没有errorLevel 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%