tags:

views:

75

answers:

3

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?

+1  A: 

If I'm not mistaken, %0 will be the path used to call the batch file.

JoshD
I'm curious, what happens when a Batch File (or any program for that matter) calls another batch file...what `%0` in the Second batch file?
st0le
I'd have to try that, but my guess would be that it's the path provided in the caller batch file.
JoshD
No, it is the current batch file.
typoknig
So, to be sure I understand, if A.bat calls B.bat, %0 is "B.bat", right? That was my guess, though I may have not stated it clearly.
JoshD
%0 is always the call of the current batch file.
peterchen
+1  A: 

Tested and working:

del %0
exit
typoknig
atzz
+1  A: 

%0 gives you the relative path the from the directory where the bat file was started. So if you call it

mybats\delyourself.bat tango roger

%0 will contain mybats\delyourself.bat

del %0 works if you haven't changed your current directory.

%~f0 exapnds to the full path to the file, but still you need to do it before changing current directory, e.g. start your batch file with

SET path_to_bat=%~f0
peterchen