# how to delete files and folders if they are 7days old



## narutrix (Feb 7, 2010)

Hi Guys,

I'm maintaining a folder in our sever whose content ( of files and folders ) needs to be automatically deleted after 7 days.

Is there a batch file I can use? Our server is using Windows 2003.

So far, this is only what I have:

_forfiles /p D:\test\ /s /m *.* /d -7 /c "cmd /c del echo @FILE"_

but I think it only deletes files, not folders. Hope you can assist me.


----------



## narutrix (Feb 7, 2010)

i posted a similar thread in Tech Support Guy, .... just checking how to delete this thread?


----------



## TheOutcaste (Aug 8, 2007)

You need to run two commands, the first to delete the files, then the 2nd to delete empty folders.
You should also not use a mask. **.** will skip files and folders that do not have an extension. Most folders don't have an extension, so they will be missed.

Give these two lines a try. Remove the Echo to actually Delete/Remove.
I put quotes around *D:\Test*; they aren't needed in this case, but if the path contains spaces it will need to be quoted.

```
Forfiles /P "D:\test" /S /D -7 /C "Cmd /C If @isdir==FALSE Echo Del @Path"
Forfiles /p "D:\test" /S /D -7 /C "Cmd /C If @isdir==TRUE Echo RD @Path 2>Nul"
```


----------



## narutrix (Feb 7, 2010)

I'll give it a shot guys and i'll give you feedback =)


----------



## narutrix (Feb 7, 2010)

TheOutcaste said:


> You need to run two commands, the first to delete the files, then the 2nd to delete empty folders.
> You should also not use a mask. **.** will skip files and folders that do not have an extension. Most folders don't have an extension, so they will be missed.
> 
> Give these two lines a try. Remove the Echo to actually Delete/Remove.
> ...


Hi TheOutcaste

I've tried the script you gave and it works for deleting the files and the content of the folder, but not the folder itself.

To clarify, the code i'm using is:

_Forfiles /P "C:\test" /S /D -7 /C "Cmd /C If @isdir==FALSE Del @Path"
Forfiles /p "C:\test" /S /D -7 /C "Cmd /C If @isdir==TRUE RD @Path 2>Nul"_

For example I have a folder inside of c:\test\ named "test2" created today, Feb 10. On the 17th of Feb, the content of the folder "test2" will be deleted, but the "test2" folder (and the subfolders inside) will only be deleted on the 24th of Feb.

Any way this can be remedied?

I've added the following:

_Forfiles /p "C:\test" /S /D -7 /C "Cmd /C If @isdir==FALSE RD /s /q @Path 2>Nul"_

but it's still the same problem.


----------



## TheOutcaste (Aug 8, 2007)

Sorry, missed this post somehow.

Didn't stop to think about it, but deleting the files will obviously change the modified dates on the folders to the current time, so they won't be 7 days old any more.
Another problem, Forfiles searches from the Top down, so a tree with just empty folders would end up having only the lowest level folder removed.
So, let's try this, use Forfiles to first get a list of folders older than 7 days, and create a batch file with the RD commands in it. Reverse sort that to start at the lowest level folder in each tree.
Then delete the files
Then run the batch file
Then clean up.
So in this case, the Echo statement shown in red should be left in.


```
Set _tmpfl=%temp%\~rdfldr
Set _Source=C:\Test
If Exist "%_tmpfl%.*" Del "%_tmpfl%.*"
Forfiles /p "%_Source%" /S /D -7 /C "Cmd /C If @isdir==TRUE [B][COLOR=Red]Echo[/COLOR][/B] @RD @Path 2^>Nul" >"%_tmpfl%.bat"
Forfiles /P "%_Source%" /S /D -7 /C "Cmd /C If @isdir==FALSE Del @Path"
Sort /R "%_tmpfl%.bat" /O "%_tmpfl%.cmd"
"%_tmpfl%.cmd"
If Exist "%_tmpfl%.*" Del "%_tmpfl%.*"
```


----------



## narutrix (Feb 7, 2010)

TheOutcaste said:


> Sorry, missed this post somehow.
> 
> Didn't stop to think about it, but deleting the files will obviously change the modified dates on the folders to the current time, so they won't be 7 days old any more.
> Another problem, Forfiles searches from the Top down, so a tree with just empty folders would end up having only the lowest level folder removed.
> ...


tried it and so far it works!!!! 

i'll keep you posted after further testing


----------



## mymotherisafish (Feb 18, 2010)

What does the caret after the number 2 mean on this line?

Forfiles /p "%_Source%" /S /D -30 /C "Cmd /C If @isdir==TRUE Echo @RD @Path 2^>Nul" >"%_tmpfl%.bat"

Thanks!


----------



## Squashman (Apr 4, 2003)

Used to escape a character.
http://www.robvanderwoude.com/escapechars.php


----------

