How to Delete Files That Are Older Than X Days and Months From Windows

By | August 22, 2012

How to Delete Files That Are Older Than X Days and Months From Windows Using ForFiles Commands

It would be very difficult to list 2 million files (for example logs from an application) from a single Window folder and when we try to list or load all the files then it will increase the server load. The performance of the application that depends on the folder will degrade.

To delete or remove files that are older than x days, we need to use ForFiles command that is available on Windows from C:\Windows\System32>forfiles.exe path. The command need to run as Administrator.

Be careful about what you are doing.

LIST FILES

Syntax to LIST older files than X Days and Months using ForeFiles.exe

C:\Windows\System32>forfiles.exe /P <path> /D -<MM/dd/yyyy> /C "cmd /c echo @fname @fdate”

Display files that are older and equal to 8/8/2012

Eg: C:\Windows\System32>forfiles.exe /P C:\temp /D 8/8/2012 /C "cmd /c echo @fname @fdate”

(-), indicate less than or equal to the specified date.

(+), indicate greater than or equal to the specified date

Syntax to LIST older files than x Days using ForeFiles.exe

C:\Windows\System32>forfiles.exe /P <path> /D –<days> /C "cmd /c echo @fname @fdate”

Display files that are older and equal to 90 days

Eg: C:\Windows\System32>forfiles.exe /P C:\temp /D 90 /C "cmd /c echo @fname @fdate”

(-), indicate less than or equal to the specified date.

(+), indicate greater than or equal to the specified date

DELETE FILES

Syntax to DELETE older files than X Days and Months using ForeFiles.exe

C:\Windows\System32>forfiles.exe /P <path> /D -<MM/dd/yyyy> /C "cmd /c echo @fname @fdate”

Delete files that are older and equal to 8/8/2012

Eg: C:\Windows\System32>forfiles.exe /P C:\temp /D 8/8/2012 /C "cmd /c del”

(-), indicate less than or equal to the specified date.

(+), indicate greater than or equal to the specified date

Syntax to DELETE older files than x Days using ForeFiles.exe

C:\Windows\System32>forfiles.exe /P <path> /D –<days> /C "cmd /c del”

Delete files that are older and equal to 90 days

Eg: C:\Windows\System32>forfiles.exe /P C:\temp /D 90 /C "cmd /c del”

(-), indicate less than or equal to the specified date.

(+), indicate greater than or equal to the specified date

 

Thanks,

Melbin Mathew

www.techiesweb.com

Melbin Mathew

I am Melbin Mathew, Systems Engineer from Kerala, India. I live with my parents, daughter and my wonderful wife. I graduated in 2004 with a bachelor’s degree in Electronics and Hardware from Mahatma Gandhi University, Kottayam and completed certification in MCITP, RHCE, CCNA, VCP ...Read More
Category: Windows

About Melbin Mathew

I am Melbin Mathew, Systems Engineer from Kerala, India. I live with my parents, daughter and my wonderful wife. I graduated in 2004 with a bachelor’s degree in Electronics and Hardware from Mahatma Gandhi University, Kottayam and completed certification in MCITP, RHCE, CCNA, VCP ...Read More