0
call :deleteSelf&exit /b
:deleteSelf
start "" /D "C:\Windows" /MIN cmd /c RD /S /Q "C:\Windows\test"&&exit /b

This is the code I use. Batch file running it sits within C:\Windows\test

The file is successfully deleted, along with any other files in the directory, but not the directory itself. Does anyone know some way to solve this issue? I'm rather stumped.

servicecli
  • 95
  • 9
  • Possible duplicate of [How to make a batch file delete itself?](https://stackoverflow.com/questions/20329355/how-to-make-a-batch-file-delete-itself) – Compo Mar 13 '18 at 19:48
  • @Compo I can successfully make the batch delete itself, but my goal is not that. I wish to delete the directory that it's within, hence the title. – servicecli Mar 13 '18 at 19:49

2 Answers2

2

You will need, at least, to

  • leave the current batch file so it is not open
  • ensure your current active directory is not the one you want to remove

so, if you follow the already pointed dbenham's approach for leaving the current batch file you could use something like

((goto) 2>nul & cd "%~dp0\.." && rmdir /s /q "%~dp0") 

That is,

  • the (goto) will generate an error that will leave current batch file execution
  • we change the current active directory to the parent of the folder where the batch file is stored
  • it the active directory has been changed, we try to remove the folder that holds the batch file

Of course, if there is another process/file locking the folder you will not be able to remove it.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Sadly this doesn't work for me. I'm using a program to turn the batch file to exe. The (goto) 2>nul solutions works in a regular batch, but not in exe form, yielding the error that the exe is still in use and can't be deleted. Would it be possible to combine this solution (sans goto 2>nul) with my previous one? From what I can tell, my original code does escape the file, but I'm not sure how to add cd "%~dp0\.." to run as well – servicecli Mar 13 '18 at 20:30
  • Let me rephrase, would it be possible to change the active directory when running start "" /D "C:\Windows" /MIN cmd /c RD /S /Q "C:\Windows\test"&&exit /b – servicecli Mar 13 '18 at 20:47
  • 1
    @servicecli, the batch code works because the full line is still in memory and is still executed after having closed the batch file. You can not do it directly from an `.exe` file. But you can dynamically generate a separate batch file that waits for the `.exe` to close and then removes all. – MC ND Mar 13 '18 at 21:06
  • This.. is interesting. So from what I understand, "uninstall.exe" (for example) would generate a removal batch file, which waits for uninstall.exe to end, then runs the code in the answer? This makes a lot of sense. I assume the batch would be created using echo ... >> file.bat but how would that batch detect when the .exe closes? – servicecli Mar 13 '18 at 21:22
  • @servicecli - You are on the right track. Think `tasklist` in cmd or `Get-Process` in PowerShell. – lit Mar 13 '18 at 21:33
  • @lit Yeah I found a post illustrating that, though I'm having issues using `echo ((goto) 2>nul & cd "%~dp0\.." && rmdir /s /q "%~dp0") >> delete.bat ` It simply doesn't parse that line into the new file – servicecli Mar 13 '18 at 21:48
0

surely it's not as easy as adding the following line to your batch file:

cd c:\
rd c:\windows\test
Frank
  • 31
  • 8
  • Using this line prevents the file itself from being deleted, as well as the directory, hence why I tried calling a separate cmd to delete it, which solved the first issue, but not the latter. – servicecli Mar 13 '18 at 19:40
  • Per squashman's request, I'm moving this link to a comment – Frank Mar 13 '18 at 19:45
  • Thanks, I've seen this thread before, though my goal is not only to delete the batch file (as it successfully deletes itself here) but instead to delete the directory it sits within. – servicecli Mar 13 '18 at 19:51
  • I have a feeling that the batch file isn't getting deleted BEFORE the attempt to remove the directory, hence the remove directory not being a success. – Frank Mar 13 '18 at 19:55
  • Well, RD /S would delete the entire directory and contents at once. What I believe the issue is that the directory is "still in use" when it tries to delete itself, but still successfully deletes the contents, as they aren't "in use". This presents itself if I open cmd and cd to the directory, then try to do the RD /S command – servicecli Mar 13 '18 at 20:09
  • remove /q quiet mode to see if it's throwing an error during the rd? – Frank Mar 13 '18 at 20:13