Max Lam
Max Lam

Reputation: 53

How do I make a batch file delete it's own directory?

Okay, I apologize that I am very new at this, but I am trying to make my batch file delete it's own directory after it has been launched. This is how my folders are arranged:

My goal is to make "delete.bat" delete "Folder1" after "delete.bat" has been launched. So here's my code:

rd /s /q %~dp0..\Folder1

This seems like it would work but it only deletes the contents of "Folder1" rather than the whole directory itself. What am I doing wrong?

Upvotes: 5

Views: 4310

Answers (4)

kostasvs
kostasvs

Reputation: 431

My implementation is effectively the same as Soja's, plus the info from dbenham's comment. I have added a 2-sec delay, to ensure there are no timing issues, even though I believe the error when the .bat file is deleting itself is harmless.

@echo off

:: Do the work
...your command here...

:: In order to delete the current dir we are running from (and all subdirs), none of them can be the
:: current working directory of any running process. Therefore, we are setting our own CWD to something
:: else, so it will be inherited by the below cmd.exe.
cd /d %temp%

:: The countdown is there to allow this batch file to exit, so it can then be deleted safely.
set DelayedSelfDeleteCommand="timeout /t 2 >NUL && rmdir /s /q "%~dp0""

:: Start a separate process (without waiting for it), to execute the command
start "" /b cmd.exe /C %DelayedSelfDeleteCommand%

Upvotes: 2

AnonA
AnonA

Reputation: 1

Well I think it cannot be done (at least as normal user)

    start /b "" cmd /c rd /s "%~dp0"

deletes folder but only with right permissions I think

    start /b "" cmd /c rmdir /s "C:/folder"

This has the same result

    del /s /q "C:\Temp\folder\*"
    rmdir /s /q "C:\Temp\folder"
    del %0

only way as for batch files is to use vbs script or autohotkey (send !{Space} // send e // send p formula) or hack it, you can delete only file used and content of folder but not working directory due to specification of cmd. Any other language would have no problem with it cuz it in RAM memory.

I recommend this way to do it (as for normal users): in your bat file add

    copy C:\urpath\deleteafter.bat C:\Temp\deleteafter.bat
    start "" autohotkey.exe "X:\patchto\deletebatchfile.ahk"


    deletebatchfile.ahk
    sleep 2000
    Run, C:\Temp\deleteafter.bat, C:\Temp\
    
    deleteafter.bat
    rmdir /s /q "C:\Temp\batfileworkingpath"
    sleep 3
    del %0

Upvotes: 0

soja
soja

Reputation: 1607

Some thoughts...

  • %~dp0 gets the drive and path of the batch file, so you don't need to include ..\Folder1.
  • What you have should work. If it's not removing the folder itself, it means that it's locked, probably because cmd's current folder is Folder1. (That's a likely guess, but it's not the only reason it might be locked.) If it is cmd, you'll have to call the batch file from another folder, outside of Folder1.
  • While what you have will work, it will result in a funny error when resuming the non-existent batch file: The system cannot find the path specified. You can avoid that in the solution below.

One good solution: start /b "" cmd /c rd /s /q "%~dp0"

This creates a new process to remove the folder (and everything in it, including the batch file itself). Be careful. =)

Upvotes: 4

Vada Poché
Vada Poché

Reputation: 780

From the corresponding MSDN link for rd:

You cannot use rmdir to delete the current directory. You must first change to a different directory (not a subdirectory of the current directory) and then use rmdir with a path.

I guess this is what's going wrong in your case since the batch file is located within the directory that you're trying to delete.

Upvotes: 1

Related Questions