Batch delete of subfolders

January 4, 2010 at 18:11:35
Specs: Windows XP
Hi there.
I want to be able to write a batch script to delete any subfolder entitled 'old_reports' say in a specific file structure. However within this file strucuture prior to getting to this subfolder the number of folders and names may vary.

Can someone provide tips on how I might be able to handle a varying file tree structure?


From the above I want to delete everything from old downwards but how do I declare a variable for this file structure where the folders will vary?

Thanks in advance.

See More: Batch delete of subfolders

Report •

January 5, 2010 at 07:40:35
If the directory tree is rooted at C:\All and there are varying levels of subfolders under C:\All but there isn't a directory actually called old_reports under C:\All itself, then the following should do it:
@echo off
for /f "delims=" %%a in ('dir /ad /s /b C:\All\old_reports') do (
   echo rd /s /q %%a

Remove the "echo" once you are satisfied that it will do the right thing.

Report •

January 5, 2010 at 12:58:03
thanks for your helpful reply klint.

I can get a list of all the directories containing a sub-folder entitled old_reports using below code but how do I easily combine the remove to delete this directory and all it's sub-directories as it finds it on a specific drive:?

for %%d in (h) do (

dir/s/b/ad %%d:\old_reports


Report •

January 5, 2010 at 14:52:12
Combining the two pieces of code:

for %%d in (h) do (
   for /f "delims=" %%a in ('dir/s/b/ad %%d:\old_reports') do (
      echo rd /s /q %%a

Report •

Related Solutions

Ask Question