中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-23 13:38
47,812 topics / 349,917 posts / today 0 new / 48,273 members
DOS批处理 & 脚本技术(批处理室) » [Help] How to delete the batch file itself in batch processing?
Printable Version  2,328 / 5
Floor1 hyzys Posted 2004-11-25 00:00
初级用户 Posts 17 Credits 189
Remember there is a command to delete the batch file itself, but I forgot it for a moment. For example: What command can be used in A.BAT to delete A.BAT itself?
Floor2 Climbing Posted 2004-11-26 00:00
铂金会员 Posts 2,753 Credits 6,962 From 河北保定
@echo off
echo Before delete:
dir %0.*
echo Going for delete myself...
echo %0 | find /i ".bat"
if errorlevel 1 goto _addext
copy %0 %0.bak > nul
del %0
goto _quit:_addext
copy /y %0.bat %0.bat.bak > nul
del %0.bat:_quit
Floor3 autoit Posted 2004-11-26 00:00
系统支持 Posts 339 Credits 904 From 厦门
Here is the speech of Climbing quoted on 2004-11-26 13:21:37:

@echo off
echo Before delete:
dir %0.*
echo Going for delete myself...
echo %0 | find /i ".bat"
if errorlevel 1 goto _addext
copy %0 %0.bak > nul
del %0
goto _quit
:_addext
copy /y %0.bat %0.bat.bak > nul
del %0.bat
:_quit

Can just `del /q a.bat` work?
Floor4 小石头 Posted 2004-11-27 00:00
初级用户 Posts 28 Credits 165
What if the batch file is not called A.bat? My batch file is for any file name (of course, the extension must be BAT). The problem the owner put forward is to let the batch file delete itself, and it is not limited that this batch file must be called A.BAT, so we need to judge what its own name is from inside the batch file.
Floor5 linson Posted 2004-12-01 00:00
中级用户 Posts 31 Credits 283
del /q %0 can delete itself.
Floor6 willsort Posted 2004-12-04 00:00
元老会员 Posts 1,512 Credits 4,432
Re Climbing:
Modify your program slightly. Also, there is still a prompt that the batch file cannot be found, and the temporary deletion program will also leave a tail, so it is still necessary to use the system's uninstall mechanism, but the program will be complicated.
Re linson:
The del /q %0 statement cannot delete itself when the main program name is used to call the batch program in the command line.
@echo off
echo Before delete:
dir %0.*
echo Going for delete myself...
::echo %0 | find /i ".bat"
::if errorlevel 1 goto _addext
if exist %0.bat goto _addext
if not exist %0 goto _quit
copy %0 %0.bak > nul
del %0
goto _quit
:_addext
copy /y %0.bat %0.bat.bak > nul
del %0.bat
:_quit
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023