# delete folder older than x



## StudMuffin (Jan 17, 2011)

So I've been working on a way to delete folders within a certain directory after X amount of days and came up with this but it ends up deleting all folders regardless of their creation date if it finds one folder matching that criteria. i barely understand programming. can somebody help me? here's what i have so far:

@echo off
set Yesterday=06/01/2011
for /d %%d in ("E:\folder\*") do (
for /f "tokens=1" %%a in ("%%~td") do (
echo Folder: %%d Creation date: %%a
if %%a NEQ %Yesterday% rmdir /S /Q "%%d"
)
)


----------



## Squashman (Apr 4, 2003)

I think the best utility for this is too use the ForFiles command. Comes standard on most Windows Server installations. I believe you can get it from the server resource kits.

I believe by default the date you are trying to display is the last modified date and time. Not the date and time created.


----------



## StudMuffin (Jan 17, 2011)

so something like this to delete folders older than 5 days? 

forfiles /p "e:\folder\" /c "cmd /c if @isdir==TRUE RMDIR /S /Q @path" /d -5

when i run it, after installing the server resource kit, cmd just repeats that line of code over and over and over and over


----------



## Squashman (Apr 4, 2003)

StudMuffin said:


> so something like this to delete folders older than 5 days?
> 
> forfiles /p "e:\folder\" /c "cmd /c if @isdir==TRUE RMDIR /S /Q @path" /d -5
> 
> when i run it, after installing the server resource kit, cmd just repeats that line of code over and over and over and over


Looks like the right syntax. I don't know if you have to have the slash at the end of the path or not.


----------



## StudMuffin (Jan 17, 2011)

any guidance on how to get forfiles to run on xp or how to get my previous script to work properly?

EDIT:
got forfiles running. have the following but it comes out 'can't execute' will continue playing

forfiles -pE:\folder -d-5 -c "CMD /c if @isdir==TRUE RMDIR /S /Q @path"


----------



## Squashman (Apr 4, 2003)

The older version of for files used dashes for switches. The newer version uses slashes.
Both of them work on XP if memory serves me corectly. You just need to use the correct syntax for the version you have.


----------

