0

I need to delete all the subfolders in D:\TEMP

I don't want to delete the files in D:\TEMP, I only need to delete the subfolders in D:\TEMP (and all the files and subfolders they contain)

There are a lot of questions (and answers) about deleting files and subfolders, but I can't find any question about deleting only subfolders

Joe Jobs
  • 201
  • 1
  • 12
  • 2
    `for /D %%I in ("D:\TEMP\*") do rd /S /Q "%%~I"`… – aschipfl Jun 26 '21 at 23:46
  • 2
    Rather than a `For /D` loop, it may be better to use a `For /F` loop and the `Dir` command to perform the enumeration. This is because `For /D` will exclude hidden objects, and could therefore leave some behind. `@For /F Delims^=^ EOL^= %%G In ('Dir "D:\TEMP" /B /A:D 2^>NUL') Do @RD /S /Q "%%G" 2>NUL` You should also be aware that you can still only remove directories for which you have sufficient permissions. This would technically be a less memory intensive method than using `forfiles.exe` because the commands are internal and use only one cmd.exe instance. – Compo Jun 27 '21 at 01:08
  • 1
    The solution by @Compo is definitely the best, but there is a small, but very serious mistake in the command line as ``D:\TEMP`` is missing in folder path on command __RD__. The command __DIR__ outputs just the folder names without path. So the really working solution is: `@For /F Delims^=^ EOL^= %%G In ('Dir "D:\TEMP" /B /A:D 2^>NUL') Do @RD /S /Q "D:\TEMP\%%G" 2>NUL` or `@for /F "eol=| delims=" %%I in ('dir "D:\TEMP" /AD-L /B 2^>nul') do @rd /Q /S "D:\TEMP\%%I" 2>nul`. The second solution works nearly the same as the first solution. It uses just a bit different syntax and ignores junctions. – Mofi Jun 27 '21 at 08:59
  • 1
    Well, my line of code was not intended as a full solution but merely as a starting point for this (off-topic) question. Anyway, @Mofi, please elaborate on the issue with FAT drives; are you talking about potential problems with short 8.3 names? I would expect a few *"file not found"* messages but not any skipped files/directories… – aschipfl Jun 28 '21 at 08:36
  • Yes, @Mofi, this is a consequence of the behaviour of `for`/`for /D`, which does not enumerate the whole set of files/directories in advance, so other processes can of course interfere (this can also be problematic when you are renaming files/directories in a loop, even on NTFS; see the thread [At which point does `for` or `for /R` enumerate the directory (tree)?](https://stackoverflow.com/q/31975093/5047996))… – aschipfl Jun 29 '21 at 09:04

1 Answers1

1

Check "forfiles", it has an "@isdir" switch for detecting directories and the "cmd" you can combine with "rmdir".

Edit after comments, hereby the working command:

forfiles /P D:\TEMP /c "cmd /c if @isdir==TRUE rd /s /q @file"
Dominique
  • 16,450
  • 15
  • 56
  • 112
  • 2
    That command line is the worst solution of all provided solutions. It is not only the slowest solution, it also does not work safely on FAT32 or exFAT drives and it can result in deletion of wrong directories in case of current directory on execution is not `D:\Temp` as `@file` references just the name of the directory without path. So while `%SystemRoot%\System32\forfiles.exe` processes all directory entries in `D:\Temp` and starts for each directory entry (file or directory) a `cmd.exe` process to do the __IF__ condition, the command __RD__ deletes the directory in __current directory__. – Mofi Jun 27 '21 at 08:49
  • 3
    Well, the main and very serious mistake can be fixed by replacing `@file` by `@path`, but the slowness and the problem with not working safely on FAT drives cannot be fixed on using `forfiles`. The solution is using the code as posted in my second comment on the question which works even on Windows XP on which `forfiles.exe` is not available at all. – Mofi Jun 27 '21 at 08:52