0

I want a batch file to delete all the files and subfolders in the directory it's in. This is what I made (it deletes just files):

cd "%~dp0"
for %%i in (*.*) do if not "%%i"=="%~nx0" del /f /q "%%i"

How to make it delete also subfolders?

Error_404
  • 29
  • 6
  • Try as your second line, `RD /S /Q "."`. Please note that you could see an error message because you're also deleting the batch file, whilst it is running. This will however not stop the directory from being emptied, _(subject to you having the required permissions to do so)_. – Compo Oct 20 '21 at 15:26
  • See https://stackoverflow.com/questions/6836566/#24482102 for a short script that does this. – Bill_Stewart Oct 20 '21 at 16:09
  • `cd "%~dp0"` should read `cd /D "%~dp0"` to also change to the target drive in case… – aschipfl Oct 21 '21 at 12:01
  • @Compo, your suggestion would also delete the batch file; only its parent directory would be kept as it is the current directory because of the first line of the script… – aschipfl Oct 21 '21 at 12:14
  • @aschipfl, correct my comment even says so. However, in my defence, I submitted my comment thirty minutes before the OP mentioned in another comment, _(not even under the question)_, that it was not their intention to delete the batch file too. The question was and is specific "delete all the files and subfolders in the directory it's in", and there is absolutely no explicit or implicit information, given that the batch file is a file in the directory it is in, that it should not be included. My commented command line replacement performs exactly the task as written in the question. – Compo Oct 21 '21 at 18:36

2 Answers2

1

The standard FOR command only iterates files. So you need a mechanism to list file and folders at the same time. You also need a mechanism to determine if the object iterated is a file or folder so that you use the correct command to either delete the file or remove the directory.

@echo off
CD /D "C:\root folder"
for /F "delims=" %%G in ('dir /b') do (
    REM check if it is a directory or file
    IF EXIST "%%G\" (
        rmdir "%%G" /s /q
    ) else (
        del "%%G" /q
    )
)
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • ok this works, but I don't want it to delete itself. It's meant to be the only file in the root dir after executing it – Error_404 Oct 20 '21 at 15:56
  • @NuKl3Ar feel free to edit the answer to include your `IF` comparison for the `batch-file` name. – Squashman Oct 20 '21 at 15:59
  • 1
    I'll use **%~nx0** to detect it, so if the user changes its filename it won't get deleted – Error_404 Oct 20 '21 at 16:01
  • As far as I know, `if exist "%%G\*"` is more reliable than `if exist "%%G\"` to detect whether a directory exists, though I cannot remember when the latter fails (with mapped network shares, or with junctions/hardlinks?) Anyway, you could simply just do `rd /S /Q "%%G" || del "%%G"` (no need for `/Q` for `del` here, but `/F` might be useful)… – aschipfl Oct 21 '21 at 12:00
  • By the way, if you change `dir /B` to `dir /B /A`, **all** items are returned and therefore deleted, independent on their attributes (also `del /A` is needed then)… – aschipfl Oct 21 '21 at 12:07
0

It's a known problem in Windows commandline: in order to remove a file, you need the del command. In order to remove a directory, you need rmdir but you can only launch that command if your directory is empty.

So, first you delete the files (which you already have done), and then you can start using rmdir for deleting the directories, but beware: you need to do this from inside to outside (from the deepest subsubsub...directory back to the main directory).

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • 1
    The `RMDIR` command has an option to remove the folder and all the files in it. – Squashman Oct 20 '21 at 15:16
  • The problem with the cmd.exe `rd` (`rmdir`) is that it removes the directory _itself_ (not just the _content_ of the directory), which is normally undesirable (unless the permissions on the deleted subdirectory are identical to the parent directory). – Bill_Stewart Oct 20 '21 at 16:09