How do I delete old files from a directory while keeping the most recent ones on Windows

KiwiNige picture KiwiNige · Sep 9, 2008 · Viewed 21.8k times · Source

Possible Duplicate:
Batch file to delete files older than N days

I want to run a scheduled windows task that deletes all files from a directory that are older than 2 weeks.

The reason is that these are IIS and Tomcat logs that fill up my server, but I want to keep the most recent logs in case I need to investigate a problem.

Does any one know an easy way to do this?

Cheers

Nige

Answer

subha picture subha · Sep 20, 2010

exact syntax: FORFILES /p d:\new /d -30 /m * /c "cmd /c del @file"