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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

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

#1 Post by Dos_Probie » 06 Jun 2013 16:09

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
Last edited by Dos_Probie on 18 Jun 2013 07:25, edited 1 time in total.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Delete Script and Script's root folder?

#2 Post by Squashman » 06 Jun 2013 16:16

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Delete Script and Script's root folder?

#3 Post by Squashman » 06 Jun 2013 16:32

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>

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: Delete Script and Script's root folder?

#4 Post by Dos_Probie » 06 Jun 2013 17:19

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Delete Script and Script's root folder?

#5 Post by Squashman » 06 Jun 2013 17:27

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.

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: Delete Script and Script's root folder?

#6 Post by Dos_Probie » 06 Jun 2013 18:26

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 :(

zpimp
Posts: 12
Joined: 14 Jun 2013 12:58

Re: Delete Script and Script's root folder?

#7 Post by zpimp » 14 Jun 2013 13:19

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 :)

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: Delete Script and Script's root folder?

#8 Post by Dos_Probie » 18 Jun 2013 07:22

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

Post Reply