-2

I have a folder tree similar to this:

main_folder\folder1\Debug\(file1.a1, file2.a2, file3.a3 ...)  
main_folder\folder2\Debug\(file1.a1, file2.a2, file3.a3 ...)  
main_folder\folder3\Debug\(file1.a1, file2.a2, file3.a3 ...)  

folder1, 2, 3 contain many other files and folders besides Debug. Debug subfolder is not empty in any of the folders. I want to delete the Debug subfolder (and everything in it) from all folders 1, 2, 3. How do I delete the Debug subfolder in all these folders using a bat script?

user13267
  • 6,871
  • 28
  • 80
  • 138
  • 1
    For usage in a command prompt window: `for /F "delims=" %I in ('dir "C:\path\main_folder\debug" /AD-L /B /S 2^>nul') do if exist "%I\" rd /Q /S "%I"` which removes all directories quietly and with all subdirectories and files with name `debug` found in `C:\path\main_folder` or any subdirectory of that directory and its entire directory tree. Replace all three occurrences of `%I` by `%%I` for usage of this command line in a batch file. `if exist "%I\"` can be omitted on no directory with name `debug` contains ever in its directory tree another directory with name `debug`. – Mofi May 19 '22 at 12:15
  • @Mofi this worked. If you can post it as an answer I will accept. – user13267 May 19 '22 at 12:21
  • 1
    Please read my answer on [Delete files or folder recursively on Windows CMD](https://stackoverflow.com/a/71547603/3074564) if you are interested in an explanation how exactly this command line works. I don't want to explain the command line a second time. Well, there is one difference: `if exist "%I\"` is used to avoid the attempt to delete a `debug` folder being a subfolder of a `debug` folder which was already deleted before. The referenced answer contains the command line with `2>nul` to simply suppress any error message output like on folder to delete not existing anymore. – Mofi May 19 '22 at 12:21

1 Answers1

0

UNTESTED It loop thropugh main_folder, read names of its subdirs than delete Debug subfolder of each main_folder subfolder, including its content.

for /f "tokens=3 delims=\" %%a in ('dir /b /s /a:d "main_folder"') do rd /S main_folder\%%a\Debug

user2956477
  • 1,208
  • 9
  • 17