China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-01 11:14
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] How to delete the batch file itself in batch processing? View 2,172 Replies 5
Original Poster Posted 2004-11-25 00:00 ·  中国 广东 河源 电信
初级用户
Credits 189
Posts 17
Joined 2004-09-23 00:00
21-year member
UID 31697
Gender Male
Status Offline
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?
Floor 2 Posted 2004-11-26 00:00 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
@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
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 3 Posted 2004-11-26 00:00 ·  中国 福建 厦门 电信
系统支持
★★★
Credits 904
Posts 339
Joined 2002-10-10 00:00
23-year member
UID 1904
From 厦门
Status Offline
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?
Floor 4 Posted 2004-11-27 00:00 ·  中国 河北 保定 联通
初级用户
Credits 165
Posts 28
Joined 2004-08-09 00:00
21-year member
UID 29892
Gender Male
Status Offline
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.
Floor 5 Posted 2004-12-01 00:00 ·  中国 福建 厦门 电信
中级用户
Credits 283
Posts 31
Joined 2004-03-06 00:00
22-year member
UID 19334
Gender Male
Status Offline
del /q %0 can delete itself.
Floor 6 Posted 2004-12-04 00:00 ·  中国 山西 运城 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
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
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Forum Jump: