self-deleting batch files

C

cwp

This is less of a BAT file question, more of an OS question I think....

BAT files can delete themselves. This is really handy to clean up
after execution. To test this works is simple enough:

C:\test_dir>echo del runme.bat > runme.bat

C:\test_dir>type runme.bat
del runme.bat

C:\test_dir>runme.bat

C:\test_dir>del runme.bat
The batch file cannot be found. <<< this is OK

C:\test_dir>type runme.bat
The system cannot find the file specified.

C:\test_dir>

On a recent Windows XP box that I came across in a company, the above
doesn't work! Explorer.EXE "locks" the BAT file and the file doesn't
delete. If you try manually deleting, even through Windows GUI, the
error is "Access is denied". If you kill explorer.exe, the file
suddenly disappears (same for a reboot).

The strange thing is this "standard operating environment" of Windows
XP used to work fine for bat files to self-delete. The only change as
far as I can tell is that the new XP box used sysprep to get built
(which changes reg settings I think).

Is there any reg keys / system settings that could possibly stop bat
files from self-deleting??
 
J

Jon

If, for some reason, the "read-only" attribute of the bat file were set,
then that would stop it from self-deleting, giving the "Access is denied"
message. Using "del /f runme.bat" would be one way round that, if that were
the case.

Jon
 
P

Pegasus \(MVP\)

cwp said:
This is less of a BAT file question, more of an OS question I think....

BAT files can delete themselves. This is really handy to clean up
after execution. To test this works is simple enough:

C:\test_dir>echo del runme.bat > runme.bat

C:\test_dir>type runme.bat
del runme.bat

C:\test_dir>runme.bat

C:\test_dir>del runme.bat
The batch file cannot be found. <<< this is OK

C:\test_dir>type runme.bat
The system cannot find the file specified.

C:\test_dir>

On a recent Windows XP box that I came across in a company, the above
doesn't work! Explorer.EXE "locks" the BAT file and the file doesn't
delete. If you try manually deleting, even through Windows GUI, the
error is "Access is denied". If you kill explorer.exe, the file
suddenly disappears (same for a reboot).

The strange thing is this "standard operating environment" of Windows
XP used to work fine for bat files to self-delete. The only change as
far as I can tell is that the new XP box used sysprep to get built
(which changes reg settings I think).

Is there any reg keys / system settings that could possibly stop bat
files from self-deleting??

Deleting a batch file while it is executing is not "nice". It is
equivalent to sawing off the branch you're sitting on. A more
elegant way goes like this. It should work in all environments.

runme.bat
=======
@echo off
.. . . some useful lines
start /b cmd /c del runme.bat
 
F

foxidrive

This is less of a BAT file question, more of an OS question I think....

BAT files can delete themselves. This is really handy to clean up
after execution. To test this works is simple enough:

C:\test_dir>echo del runme.bat > runme.bat

C:\test_dir>type runme.bat
del runme.bat

C:\test_dir>runme.bat

C:\test_dir>del runme.bat
The batch file cannot be found. <<< this is OK

C:\test_dir>type runme.bat
The system cannot find the file specified.

C:\test_dir>

On a recent Windows XP box that I came across in a company, the above
doesn't work! Explorer.EXE "locks" the BAT file and the file doesn't
delete. If you try manually deleting, even through Windows GUI, the
error is "Access is denied". If you kill explorer.exe, the file
suddenly disappears (same for a reboot).

Refresh the explorer view. The file will be gone.
 
R

RazTK

I use this way to delete the batch file:

@echo off
start /min cmd /c del /q %~s0
goto :eof

This will delete the batch file without any error.
 
C

cwp

thanks for the replies...

The BAT file is defintly not read only. Also, refreshing explorer view
doesn't fix it. After the BAT runs and apparently self-deletes, if you
try to copy another BAT file on top of the still-there-deleted one, you
also get acess denied. It doesnt *seem* to be a simple refresh problem
(I used "handle.exe" to confirm that explorer.exe was still holding on
to the bat).

I use this way to delete the batch file:

@echo off
start /min cmd /c del /q %~s0
goto :eof

This will delete the batch file without any error.


I'll try the above routine because it's a cool and different approach!,
but i'm still baffled that the OS is behaving like this... it just
shouldn't lock onto the file like that!
 
C

cwp

cwp said:
I'll try the above routine because it's a cool and different approach!,
but i'm still baffled that the OS is behaving like this... it just
shouldn't lock onto the file like that!

Well, RazTK's routine didn't work, because the problem was more along
the lines that explorer was locking a bat file even after it had run.
(eg even a normal, non-self deleting bat file would finishing running,
then opening it in notepad to edit and save it would also cause access
denied error as if the file was read only).

But RazTK's idea did get me thinking.... and the following regpatch
fixed the problem!

REGEDIT4

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\batfile\shell\open\command]
@="C:\\WINNT\\system32\\cmd.exe /c \"%1\" %*"

This forces windows to use an instance of CMD to execute the bat and
then CMD closes, closing all open handles. I still have no idea why
this install of windows behaves like this but maybe the above might me
useful to someone eventually....
 
R

RazTK

cwp said:
cwp said:
I'll try the above routine because it's a cool and different approach!,
but i'm still baffled that the OS is behaving like this... it just
shouldn't lock onto the file like that!

Well, RazTK's routine didn't work, because the problem was more along
the lines that explorer was locking a bat file even after it had run.
(eg even a normal, non-self deleting bat file would finishing running,
then opening it in notepad to edit and save it would also cause access
denied error as if the file was read only).

But RazTK's idea did get me thinking.... and the following regpatch
fixed the problem!

REGEDIT4

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\batfile\shell\open\command]
@="C:\\WINNT\\system32\\cmd.exe /c \"%1\" %*"

This forces windows to use an instance of CMD to execute the bat and
then CMD closes, closing all open handles. I still have no idea why
this install of windows behaves like this but maybe the above might me
useful to someone eventually....

I'm happy I could help. ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top