Solved having trouble with a self deleting batch

December 27, 2015 at 13:24:07
Specs: Windows 7
i wrote this simple batch to delete a given selection of files across my hdd fast but not accidentally (reson for armed and %pass%) all of that works perfectly but Im having trouble with the batch file deleting it self on exit. all i want is for it to be deleted nothing facncy just not visible to any random person that looks at my computer below is the batch mind you this is the first idk what you would call it thing i wrote

color 2

cls
@ECHO OFF
if NOT EXIST "C:\Users\GHOST\Desktop\armed" goto End 0

:BOOM
echo ------------------------------
echo ------- NUKE ALL FILES -------
echo ------------------------------
set/p "pass=-DELETE code Here-->"
if NOT %pass%== 56 goto FAIL

rd C:\Users\GHOST\Desktop\test /s /q

cls
echo -
echo -
echo Folder nuked
echo -
echo -
echo =======
echo ==== ====
echo == +++ +++ ==
echo == +++++ ++++++ ==
echo == ++++++=++++++ ==
echo == === ==
echo == +=+ ==
echo == +++++ ==
echo === +++++++ ===
echo =======

timeout 3
goto End 1

:FAIL
cls
echo -
echo Invalid password
echo -
timeout 3
goto End 2

:End 2
rd C:\Users\GHOST\Desktop\armed /s /q
exit
:End 1
rd C:\Users\GHOST\Desktop\armed /s /q
del /q /f "C:\Users\GHOST\Desktop\FILE NUKE.bat"
:End 0

message edited by ShadowS


See More: having trouble with a self deleting batch

Report •

✔ Best Answer
December 29, 2015 at 03:50:17
You can use an external CMD process to delete it.

Replace

del /q /f "C:\Users\GHOST\Desktop\FILE NUKE.bat" 

with
cmd /c /q "timeout /t 1 && del /q /f "%~f0""

Also remember to backup important things, I've lost projects way too often when experimenting with this kind of stuff ;)

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)

message edited by RainBawZ



#1
December 27, 2015 at 13:25:01
all of that echo in the middle makes up some text art

Report •

#2
December 27, 2015 at 13:44:30
Whilst a batch file is running it is "in use", so it can't delete itself.

Report •

#3
December 27, 2015 at 13:49:06
Is there a way to get it to do that?
I've done it in the past I just forget how

message edited by ShadowS


Report •

Related Solutions

#4
December 29, 2015 at 03:50:17
✔ Best Answer
You can use an external CMD process to delete it.

Replace

del /q /f "C:\Users\GHOST\Desktop\FILE NUKE.bat" 

with
cmd /c /q "timeout /t 1 && del /q /f "%~f0""

Also remember to backup important things, I've lost projects way too often when experimenting with this kind of stuff ;)

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)

message edited by RainBawZ


Report •

#5
December 29, 2015 at 05:42:43
Thanks for the help I'll try it

Just to be clear what is the purpose of
"%~f0"

Also being new and not knowing is how I lost the first working version then I forgot how I did it but it gave me a chance to expand on the the original project


Report •

#6
December 29, 2015 at 10:22:45
Batch files should not have any trouble deleting themselves; it'd be a backwards compatibility issue if they couldn't. If your batch is getting access denied errors when deleting itself, and you're sure there's no permissions issue, then try your script after disabling your anti-virus.

How To Ask Questions The Smart Way


Report •

#7
December 29, 2015 at 15:00:01
"%~f0" is a variable that contains the path to the current script. So when used with DEL, it deletes the current script file. I could have used the exact path instead, but using variables makes it easier to use in future projects.

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)


Report •

#8
December 29, 2015 at 17:21:10
Ahhh I see thank you for the help rains code worked also it wasn't an error code I was geting, just when it got to the del part of the script nothing would happen but doesn't matter cause it works now


Also no /t is needed when using timeout and looks cleaner when >nul is added for security also add /nobreak

message edited by ShadowS


Report •

Ask Question