Page 1 of 1

[ SOLVED ] Delete Script and Script's root folder?

Posted: 06 Jun 2013 16:09
by Dos_Probie
Was looking at the post on self deleting the batch script and was curious if it's also possible to not only self delete the batch file but also the root folder that it resides in (not subdirectories)..Any takers? Thanks DP

Re: Delete Script and Script's root folder?

Posted: 06 Jun 2013 16:16
by Squashman
Well if you delete the folder the batch file is in then you don't need to worry about deleting the batch file.

Just back up one directory and do an rmdir /s /q

Re: Delete Script and Script's root folder?

Posted: 06 Jun 2013 16:32
by Squashman
most likely not going to work if Windows Explorer has the folder open with the batch file in it. So you would need to execute it from the previous directory or kill the explorer window that has that folder open and then kill itself.

Code: Select all

C:\batch>dir /ad /b
deleteme

C:\batch>dir /b .\deleteme\*
deleteme.bat

C:\batch>.\deleteme\deleteme.bat

C:\batch>dir /ad /b

C:\batch>

Re: Delete Script and Script's root folder?

Posted: 06 Jun 2013 17:19
by Dos_Probie
Let me explain this is what I have ..
C:\MyFolder with Clean.bat residing in it ..
I can run on the last line of clean.bat the below script but that only deletes the bat file, other files and any subdirectories in the MyFolder but not the MyFolder root directory..Any ideas? or is this even possible?

Code: Select all

start cmd /c rd /s/q "%~dp0"&exit /b

Re: Delete Script and Script's root folder?

Posted: 06 Jun 2013 17:27
by Squashman
As I said in my previous post, if you had Windows Explorer open to Myfolder it is not going to delete MyFolder because it is in use by Windows. You would need to kill any applications that have the folder in use. Basically you would need to use TASKKILL to kill explorer then do the RMDIR command but that will kill all explorer windows on Windows 7.

Re: Delete Script and Script's root folder?

Posted: 06 Jun 2013 18:26
by Dos_Probie
Ok, thanks anyway.. taskkill /f /im explorer.exe not a good option since that will just blue screen and your unable to run a start explorer command to get back to windows, so guess can't be done :(

Re: Delete Script and Script's root folder?

Posted: 14 Jun 2013 13:19
by zpimp
taskkill explorer will not bluescreen

Code: Select all

pskill explorer
set _pa=%~dp0
cd /d c:\
rd/s/q %_pa%
start explorer.exe


replace first line with taskkill
i dont know why would you have the folder opened, but anyway it works
it even works if i have the folder opened in totalcommander

i used something like this one time, but was launching the script from command line
in the same "project" i learned the importance of not naming "path" the variable you plan to RD, and the usefullness of deepfreeze :)

Re: Delete Script and Script's root folder?

Posted: 18 Jun 2013 07:22
by Dos_Probie
zpimp wrote:taskkill explorer will not bluescreen

Code: Select all

pskill explorer
set _pa=%~dp0
cd /d c:\
rd/s/q %_pa%
start explorer.exe


replace first line with taskkill
i dont know why would you have the folder opened, but anyway it works
it even works if i have the folder opened in totalcommander

i used something like this one time, but was launching the script from command line
in the same "project" i learned the importance of not naming "path" the variable you plan to RD, and the usefullness of deepfreeze :)

Zpimp, Thanks for the suggestion, while your script above will delete-remove the batch and root directory It
still causes a bsod but your variable idea led me to this script that will Work removing the batch and root directory
(without any bsod or 3rd party pskill file!) with the native taskkill command..DP :P

Code: Select all

@echo off
:: SELF DESTRUCT (All Files, Subdirectories and Root Directory)
taskkill /f /im explorer.exe>nul 2>&1
set _sd=%~dp0
cd /d c:\
start cmd /c rd/s/q "%_sd%">nul 2>&1&start explorer.exe>nul 2>&1