Is it legal for a Bat script to delete itself.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Is it legal for a Bat script to delete itself.

#1 Post by alan_b » 31 Mar 2014 15:39

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

ShadowThief
Expert
Posts: 1163
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Is it legal for a Bat script to delete itself.

#2 Post by ShadowThief » 31 Mar 2014 16:31

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Is it legal for a Bat script to delete itself.

#3 Post by dbenham » 31 Mar 2014 16:37

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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Is it legal for a Bat script to delete itself.

#4 Post by aGerman » 31 Mar 2014 16:39

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

Code: Select all

del "%~f0"

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Is it legal for a Bat script to delete itself.

#5 Post by foxidrive » 31 Mar 2014 21:35

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. :)

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Is it legal for a Bat script to delete itself.

#6 Post by alan_b » 01 Apr 2014 04:46

Thanks everyone.

Regards
Alan

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: Is it legal for a Bat script to delete itself.

#7 Post by carlos » 01 Apr 2014 12:39

you can use the nested action:

Code: Select all


(
del "%~f0"
)

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Is it legal for a Bat script to delete itself.

#8 Post by dbenham » 01 Apr 2014 13:23

@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

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: Is it legal for a Bat script to delete itself.

#9 Post by alan_b » 01 Apr 2014 15:09

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

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: Is it legal for a Bat script to delete itself.

#10 Post by Dos_Probie » 03 Apr 2014 20:00

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

Code: Select all

cmd /c rd /s/q "%~dp0"&exit /b


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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Is it legal for a Bat script to delete itself.

#11 Post by dbenham » 06 Jul 2015 10:03

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:

Code: Select all

(goto) 2>nul & del "%~f0"

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 8)

The GOTO behavior is described at viewtopic.php?f=3&t=6491


Dave Benham

Squashman
Expert
Posts: 4472
Joined: 23 Dec 2011 13:59

Re: Is it legal for a Bat script to delete itself.

#12 Post by Squashman » 06 Jul 2015 10:46

This new GOTO trick seems to be the cure for everything.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Is it legal for a Bat script to delete itself.

#13 Post by dbenham » 06 Jul 2015 12:04

I know I haven't had a cold since it was posted :wink:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Is it legal for a Bat script to delete itself.

#14 Post by foxidrive » 06 Jul 2015 12:10

dbenham wrote:I know I haven't had a cold since it was posted :wink:


The incubation period hasn't expired yet. :)

mcnd
Posts: 27
Joined: 08 Jan 2014 07:29

Re: Is it legal for a Bat script to delete itself.

#15 Post by mcnd » 13 Jul 2015 04:12

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

Code: Select all

@"%~f0">"%~f0" && del "%~f0"


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

Post Reply