self-deleting batch files


Go Back   Computer Help Articles > Windows XP General
User Name
Password
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 01-05-2006, 02:36 AM
cwp
 
Posts: n/a
Default self-deleting batch files

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??

Reply With Quote
  #2  
Old 01-05-2006, 02:36 AM
Jon
 
Posts: n/a
Default Re: self-deleting batch files

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


"cwp" <chungwangpong@yahoo.com.au> wrote in message
news:1136413546.419107.269130@g43g2000cwa.googlegr oups.com...
> 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??
>


Reply With Quote
  #3  
Old 01-05-2006, 02:36 AM
Pegasus \(MVP\)
 
Posts: n/a
Default Re: self-deleting batch files


"cwp" <chungwangpong@yahoo.com.au> wrote in message
news:1136413546.419107.269130@g43g2000cwa.googlegr oups.com...
> 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



Reply With Quote
  #4  
Old 01-05-2006, 02:36 AM
foxidrive
 
Posts: n/a
Default Re: self-deleting batch files

On 4 Jan 2006 14:25:46 -0800, cwp wrote:

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

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Keeping backup of my pictures in my order Gladys222 Windows XP Photos 10 01-05-2006 07:08 AM
Files and Settings Transfer Wizard - Is Batch Possible? Jim Windows XP Setup Deployment 3 01-05-2006 06:25 AM
Permanently deleting files in the C:\Recycler folder flyhigh182 Windows XP Security Admin 8 01-05-2006 04:21 AM
deleting files created by files and settings transfer wizard Ted Curtin Windows XP New Users 1 01-05-2006 02:53 AM
Batch file to delete old backup Anders Hellstrom Windows XP General 3 01-05-2006 02:35 AM


All times are GMT. The time now is 10:03 AM.


Powered by vBulletin Version 3.5.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd. SEO by vBSEO 2.3.2 © 2005, Crawlability, Inc.

self-deleting batch files