Page 1 of 2
Is it legal for a Bat script to delete itself.
Posted: 31 Mar 2014 15:39
by alan_b
I think I remember that under Command.com it was either impossible or could cause grief if a *.BAT script tried to modify or delete itself.
For use by XP onwards with CMD.EXE I have a temporary need for a Run-Once script,
which will do its job and then delete itself because there is no further need for it.
I find I get perfect results without any tricky convolutions in my script,
which typically is this, in the file END_DISK_3.bat on the path R:\#-R-VHD\Sub-1\
Code: Select all
CALL "R:\#-R-VHD\Sub-1\DETACH.BAT" "R:\#-R-VHD\t.vhd"
DEL "R:\#-R-VHD\Sub-1\END_DISK_3.bat"
Are my memories wrong about self-modifying difficulties with Command.com ?
Am I perfectly safe with CMD.EXE - or am I doing something dangerous ?
Am I lucky because CMD.EXE is more user friendly than Command.com ?
Regards
Alan
Re: Is it legal for a Bat script to delete itself.
Posted: 31 Mar 2014 16:31
by ShadowThief
You'll get a "The batch file cannot be found." error when you try to delete the file, but the file will still get deleted.
Code: Select all
@echo off
cls
echo Goodbye, cruel world!
pause >nul
del %~nx0
Re: Is it legal for a Bat script to delete itself.
Posted: 31 Mar 2014 16:37
by dbenham
Actually, there is a simple way to have a batch script delete itself without raising an error - you just need to launch a new process to do the deletion. It takes time for the new process to start, which gives the original batch script enough time to close prior to deletion.
Code: Select all
start /b "" cmd /c del "%~f0"&exit /b
Dave Benham
Re: Is it legal for a Bat script to delete itself.
Posted: 31 Mar 2014 16:39
by aGerman
I don't remember the behavior under DOS or Win95. But I tested with a Batch file that run in a command.com shell under my Win7 and I didn't have any problems to let it delete itself.
In a cmd.exe shell I would rather use
This way you don't have to take care about the name and directory of the file.
It's safe as long as you have write permissions in the folder where the file is placed. Note that it could be restricted by the User Account Control (Vista onwards) in various directories like C:\, C:\Windows, C:\Program Files, etc.
Regards
aGerman
Re: Is it legal for a Bat script to delete itself.
Posted: 31 Mar 2014 21:35
by foxidrive
You could delete the running batch in command.com too - it just leaves a harmless error message on the screen.
Back then you worked around that by generating a batch file in the temp directory to do the deletion and executed that without a call statement IIRC
You cleared the temp folder periodically anyway.
Re: Is it legal for a Bat script to delete itself.
Posted: 01 Apr 2014 04:46
by alan_b
Thanks everyone.
Regards
Alan
Re: Is it legal for a Bat script to delete itself.
Posted: 01 Apr 2014 12:39
by carlos
you can use the nested action:
Re: Is it legal for a Bat script to delete itself.
Posted: 01 Apr 2014 13:23
by dbenham
@carlos - That still suffers from the
"The batch file cannot be found." error.
I don't understand why everyone seems to have overlooked my prior post.
The simplest way to have a batch file delete itself without generating an error is to use:
Code: Select all
start /b cmd /c del "%~f0"&exit /b
Dave Benham
Re: Is it legal for a Bat script to delete itself.
Posted: 01 Apr 2014 15:09
by alan_b
Thanks for your concerns about the error message - but I never saw any error.
I now find that if I am running a CMD.EXE command line and invoke
R:\#-R-VHD\Sub-1\END_DISK_3.bat
Then CMD.EXE does indeed show me
Code: Select all
File Not Found
The batch file cannot be found.
In my particular application however that does not apply.
I have Windows Explorer showing a folder with a few BAT scripts displayed as large Icons.
Using another instance of Explorer on another drive and path I can select *.VHD files and "drag-drop" onto my BAT Icon which uses DISKPART to mount as virtual Disks,
and that creates "END_DISK_nn.BAT" (where 'nn' is the Disk Number) with the instruction to detach the VHD file and then delete itself.
When I have finished with a Virtual Disk then I use Windows Explorer to double click "END_DISK_nn.BAT" and this detaches the VHD file (with a name I have forgotten) and having served its purpose it then deletes itself, and Windows Explorer does not give me any warning or grief.
Regards
Alan
Re: Is it legal for a Bat script to delete itself.
Posted: 03 Apr 2014 20:00
by Dos_Probie
Dave's solution is the best for doing a batch self destruct when done, also if you have your batch running from a specified directory that is no longer needed you can also do the same with a self delete of the directory which also takes care of any orphan script files inside..DP
dbenham wrote:@carlos - That still suffers from the
"The batch file cannot be found." error.
I don't understand why everyone seems to have overlooked my prior post.
The simplest way to have a batch file delete itself without generating an error is to use:
Code: Select all
start /b cmd /c del "%~f0"&exit /b
Dave Benham
Re: Is it legal for a Bat script to delete itself.
Posted: 06 Jul 2015 10:03
by dbenham
I've discovered the best way yet to have a batch script delete itself without generating an error. Assuming your script is at the root CALL level:
It relies on the fact that (GOTO) 2>NUL behaves like EXIT /B, except it allows concatenation of subsequent commands that execute in the context of the caller
The GOTO behavior is described at
viewtopic.php?f=3&t=6491Dave Benham
Re: Is it legal for a Bat script to delete itself.
Posted: 06 Jul 2015 10:46
by Squashman
This new GOTO trick seems to be the cure for everything.
Re: Is it legal for a Bat script to delete itself.
Posted: 06 Jul 2015 12:04
by dbenham
I know I haven't had a cold since it was posted
Re: Is it legal for a Bat script to delete itself.
Posted: 06 Jul 2015 12:10
by foxidrive
dbenham wrote:I know I haven't had a cold since it was posted
The incubation period hasn't expired yet.
Re: Is it legal for a Bat script to delete itself.
Posted: 13 Jul 2015 04:12
by mcnd
Just for completion, as it has not been posted, and under the assumption that there is no CALL in progress inside the batch file to be deleted
It is a similar idea to the dbenham's (GOTO) code, leave the current batch context to be able to delete the file. In this case, the redirection will erase all file contents, the direct invocation will replace the current batch context with a new empty one and once this context is discarded the del operation is executed