Delete files in subfolder using batch script

batch-file

I have to delete files from a sub folder with a same name. My file path is like as follows.

d:\test\test1\archive\*.txt
d:\test\try\archive\*.txt
d:\test\model\archive\*.txt

I tried deleting using del command in batch script.
But there are more than 100 folders with in the folder "test". So it is very difficult to use del for each and every path.

Except for the parent folder name of "archive" folder, everything remains the same for all the paths. So I guess there might be some easy way to delete the files using batch script.

Can anyone guide me whether there is any easy way to delete the files using batch script? Or i have to repeat del for all 100 folders?

Best Answer

You can use the /s switch for del to delete in subfolders as well.

Example

del D:\test\*.* /s

Would delete all files under test including all files in all subfolders.

To remove folders use rd, same switch applies.

rd D:\test\folder /s /q

rd doesn't support wildcards * though so if you want to recursively delete all subfolders under the test directory you can use a for loop.

for /r /d D:\test %a in (*) do rd %a /s /q

If you are using the for option in a batch file remember to use 2 %'s instead of 1.