Question

Locked

Self Deleting Batch File

By 3timestrouble ·
I am trying to create a self deleting batch file to insert some registry keys at startup and reboot the machine. The batch has some admin info in it but I need the batch file to delete itself afterwords so that:

1. The admin info is deleted.

2. The computer does not continue running the batch after every start-up.

I have already tried using the del %0.bat method and it does not seem to work(maybe my syntax is wrong, so if someone has used this please let me know).

Also if anyone knows how to make a batch file wait before it performs the next command(with a time period, not a press any key function) that would also be greatly appreciated. I cannot get the TIMEOUT function to work.

This conversation is currently closed to new comments.

10 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

Call function.

by 1bn0 In reply to Self Deleting Batch File

I'm not sure what youre trying to do but:

You can CALL another batch file from within a batch file. The first batch file stops processing until the called batch file completes.

I would ask how you are getting the batch file with the admin information onto the machine in the first place.

autoexec.bat
call editreg.bat


editreg.bat
copy admin.bat from \\server\share
call admin.bat
del admin.bat


admin.bat
regedit /r file admin admin password ???


autoexecbat calls editreg.bat
edit reg calls admin.bat
admin.bat executes and returns to editreg.bat
editreg.bat is now complete and returnsto autoexec.bat

Collapse -

Choice command for time delay.

by 1bn0 In reply to Call function.

The choice command is used to give the user a list of options to choose from and then executes based on the selection. It has timeout function to automatically select the default after a set time delay.

You may be able to use this without any displayed choices to give you the delay you want.

Collapse -

A better reference for Batch commands

by 1bn0 In reply to Call function.
Collapse -

%0

by TonytheTiger In reply to Self Deleting Batch File

is the command that runs the batch file, so if you run it by "test.bat" then within test.bat the line "del %0" would work (though it will generate an error message). If you run it by "test" then del %0.bat (as you have typed above) would work.

An alternative method would be to have another batch file do the deleting:

===Test.bat===
copy deltest.bat C
other commands
c:\deltest

===deltest.bat===
if exist test.bat del test.bat>nul
(>nul so nothing will show on the screen)

Collapse -

Finally Got it

by 3timestrouble In reply to %0

the del command does work and gives no error message. I forgot tocheck the attributes of the forlder I had to remove the read-only attrib.

Collapse -

maybe....

by ChaoZero In reply to Self Deleting Batch File

chances are, that when you use "del %0.bat", the computer wil try to: del "[itself].bat.bat" which doesn't exist..

you should try just "del %0"

hope this works.

<edit>
(I just checked this... it works!! :) )
</edit>

Collapse -

to create a self deleting batch file

by jessechad In reply to Self Deleting Batch File

well...to create a self-deleting batch file that deletes it self (can not be recovered) or deletes other programs and it self or just other programs do this:

open notepad and type:

@echo off

del "path of file"/Q/S>nul

there!
to delete multiple files type:

@echo off

del "path of file"/Q/S>nul
del "path of file"/Q/S>nul
del "path of file"/Q/S>nul
del "path of file"/Q/S>nul

there, to delete this and multiple files type:

@echo off

del "path of this batch"/Q/S>nul
del "path of file"/Q/S>nul
del "path of file"/Q/S>nul
del "path of file"/Q/S>nul

remember to save it as .bat
and change the save file type as: to all files

i hoped this sort of helped!

Collapse -

Correct syntax

by brt_thiru In reply to Self Deleting Batch File

Hi buddy,
Actually, your deletion of batch work wont work because of wrong syntax. The correct syntax should be
del %0

This is because the attribute %0 itself includes the file with the extension. So you need not to mention the extension again.

I am just learning the batch file processing and the hacks of it. If i come across such a function, i would share it here. If you come across one before i do, please share the info here.

Collapse -

Depends on how the self-deleting batch file is called

by NTHNTH In reply to Correct syntax

Depends on how the self-deleting batch file is called.

If this was typed:

SelfDelete

then %0==SelfDelete

if this was typed:

SelfDelete.bat

then %0==SelfDelete.bat

You would have to test for both.

ie.

del %0
del %0.bat

Back to Windows Forum
10 total posts (Page 1 of 1)  

Related Discussions

Related Forums