Self-deleting batch file doesn't work

January 6, 2010 at 11:42:46
Specs: Windows XP, 2.4 GHz/2GB

I have a batch file that I keep on hand to perform some tasks then delete itself using del %0 at the end. I want to be able to double click the .bat file from Windows Explorer.

The problem is it isn't deleting itself when I run the batch file from Explorer. If I run it from a cmd prompt window it deletes itself fine.

Any idea what's wrong?


See More: Self-deleting batch file doesnt work

Report •

January 6, 2010 at 12:07:12
I'm not 100% sure as I don't do DOS batchfiles as well as I script in UNIX but I suspect the issue is the %0 I know is UNIX, $0 is the name of the program (ie: what's typed to invoke the program).

I believe that is the variable that stores whatever is typed in at the command line (batchfile name). In this case, the name of the batchfile. Ergo, (if I'm right) when you double click on the batchfile in windows explorer, there is nothing stored in that variable.

If I'm right, I suspect you're typing the entire batchfile name in the following format: batchfile.bat to invoke it from the command line. Since it's got the .bat extension, this shouldn't be necessary. Try typing just batchfile without the extension. The batchfile should still run but in that case will not delete the batchfile because of the lack of the extension.

To check and see what's stored in that variable, you could put something like:

echo %0

in the batchfile before the command to delete the batchfile to see what is held in that variable. Run it from both the command line and windows explorer and see what is held in that variable in each instance.

If I'm right, then you would have to add a specific:

del batchfile.bat

command at the end of the batchfile you wish to run from windows explorer to have it actually delete the batchfile at the end of the run.

Report •

January 6, 2010 at 12:26:31
Curt, your suggestion did shed some light to the differences of running the batch file from Explorer vs from a command prompt.

I edited my test bat file to the following:

@echo off
echo %0
del %0

When running it from Explorer, I get the full path. When it from the command prompt, I only get the filename.

This still doesn't help solve the problem though because the del command works whether given a full path or just a filename.

Report •

January 6, 2010 at 12:36:15
If this helps any more, I'm able to see the error message when the file fails to delete itself. It's "The system cannot find the file specified."

When the script runs correctly, the file is deleted and I get the expected "The batch file could not be found."

If I try to run the batch file using just the name (without .bat) it fails to delete the file and says "Could Not Find [full path without .bat]"

The 2nd and 3rd error messages seem to show exactly what should be expected. I'm not sure what's causing the first one though.

Report •

Related Solutions

January 6, 2010 at 13:30:46
I have figured out the problem. The path contained a directory with a ! character. I renamed the directory to remove the ! character and now it's working.

Report •

January 6, 2010 at 13:31:17
If you double click on this bat:
@echo off & setLocal EnableDELAYedeXpansion

echo del %0
echo del %0.bat
you get:

del "C:\temp\-\d10\gtest\tok\y\z\SELFD.BAT"
del "C:\temp\-\d10\gtest\tok\y\z\SELFD.BAT".bat

which tells us that you need:

echo del %0

for it to work in explorer. And if used at the prompt type bat name WITH extension.

Helping others achieve escape felicity


Report •

January 6, 2010 at 15:11:44
"I have figured out the problem. The path contained a directory with a ! character. I renamed the directory to remove the ! character and now it's working."

You could have put quotes around it to support extended file name to allow for special characters in the directory...

del "%0"

Report •

Ask Question