# Solved: delete folders/subfolders



## lance2003 (Nov 7, 2010)

Hi Guys,

Just new here and I just need some help with this task of deleting folders weekly. Basically we have a drive that saves videos of all the cameras in the building and we need to delete old folders and leave at least 7 latest subfolders.

I've attached an image of our directory.









Here I want to leave only the 7 latest folders, 20101101-20101107, and repeat the process to every folder on E:

I've tried one of the codes I found here but it only deletes folder on the drive not the subfolders.

TIA.


----------



## etaf (Oct 2, 2003)

have a read here http://forums.techguy.org/dos-other/772656-dos-batch-file-delete-folders.html see if that helps at all


----------



## lance2003 (Nov 7, 2010)

i've already checked that but we have different folder structure, he doesn't have subfolders.

I'm currently testing this bat:


> @Echo Off
> :: User Variables
> :: Set this to the number of folders you want to keep
> Set _NumtoKeep=6
> ...


I want to retain the structure and delete only the subfolders inside older than 7 days (these folders are named as 20101101...)


----------



## etaf (Oct 2, 2003)

try deltree command - 
http://www.computerhope.com/deltree.htm
Note: Use DELTREE cautiously. Every file and subdirectory within the specified directory will be deleted. Once deleted, you cannot recover the information.
EDIT 
That maybe for earlier versions of dos - seems the S may help in del and Q to keep quite 
so 
DEL /S /Q
also see here
http://www.computerhope.com/rmdirhlp.htm
example on above link may help
rmdir c:\test /s

see here also
http://forums.techguy.org/windows-xp/664554-dos-command-delete-several-folders.html


----------



## lance2003 (Nov 7, 2010)

i really new to this and might take a while for me to get it.


----------



## TheOutcaste (Aug 8, 2007)

Just need to add another loop to get the top level folders, and call the code that finds the folders to delete as a subroutine.
Give this a try on some test data:

```
@Echo Off
:: User Variables
:: Set this to the number of folders you want to keep
Set _NumtoKeep=6
:: Set this to the Parent folder that contains the folders whose subfolders you want to check and delete
Set _Root=E:\
PushD %_Root%
For /F "Tokens=1 Delims=" %%A In ('Dir /AD /B "%_Root%"') Do Call :_Sub1 "%%~fA"
PopD
Goto :EOF
:_Sub1
Set _Path=%~1
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
PushD %_Path%
Set _s=%_NumtoKeep%
If %_NumtoKeep%==1 set _s=single
Echo Please wait, searching "%_Path%" for folders other than the %_s% most recent
For /F "tokens=* skip=%_NumtoKeep%" %%I In ('Dir "%_Path%" /AD /B /O-D /TW') Do (
If Exist "%temp%\tf}1{" (
Echo %%~fI>>"%temp%\tf}1{"
) Else (
Echo.>"%temp%\tf}1{"
(Echo Do you wish to delete the following folders?
Echo Name
Echo %%~fI)>>"%temp%\tf}1{"
))
PopD
If Not Exist "%temp%\tf}1{" Echo No Folders Found to delete & Goto _Done
Type "%temp%\tf}1{" | More
Set _rdflag= /q
:_Prompt1
Set /P _resp=Delete All, None, or Prompt for each (A/N/P)?
If /I "%_resp:~0,1%"=="N" Goto _Done
If /I "%_resp:~0,1%"=="A" Goto _Removeold
If /I NOT "%_resp:~0,1%"=="P" (Echo (A/N/P only please)&Goto _Prompt1
Set _rdflag=
:_Removeold
For /F "tokens=1 skip=3 Delims=" %%I In ('type "%temp%\tf}1{"') Do (
If "%_rdflag%"=="" Echo Deleting
rd /s%_rdflag% "%%I")
:_Done
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
```


----------



## lance2003 (Nov 7, 2010)

i owe you and this site a big one. that did the trick.

thank you.


----------

