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
2010-10-03 08:30:19
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
2010-10-03 08:32:42
I'd have to try that, but my guess would be that it's the path provided in the caller batch file.
JoshD
2010-10-03 08:34:45
No, it is the current batch file.
typoknig
2010-10-03 08:38:05
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
2010-10-03 08:42:16
%0 is always the call of the current batch file.
peterchen
2010-10-03 08:52:43
+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
2010-10-03 08:50:43