1. Home
  2. PC and Server
  3. Windows Instructions
  4. Automatically Delete Files in a Folder That Is Older Than XX Days – Windows Instructional Article

Automatically Delete Files in a Folder That Is Older Than XX Days – Windows Instructional Article

Summary

This Instruction Article explains how to setup a Windows batch file to delete files that are older than XX number of days. This is very useful for setting up an automated system cleanup.

Instructions

Windows has a command called forfiles that we can use for this purpose.

FORFILES [/P pathname] [/M searchmask] [/S] [/C command] [/D [+ | -] {MM/dd/yyyy | dd}]

Description: Selects a file (or set of files) and executes a command on that file. This is helpful for batch jobs.

Parameter List:
/P	pathname 	   Indicates the path to start searching. The default folder is the current working directory (.).

/M 	searchmask   Searches files according to a searchmask. The default searchmask is '*' .

/S 		   Instructs forfiles to recurse into subdirectories. Like "DIR /S".

/C 	command 	   Indicates the command to execute for each file. Command strings should be wrapped in double quotes.

		The default command is "cmd /c echo @file".

		The following variables can be used in the command string:
		@file - returns the name of the file.
		@fname - returns the file name without extension.
		@ext - returns only the extension of the file.
		@path - returns the full path of the file.
		@relpath - returns the relative path of the file.
		@isdir - returns "TRUE" if a file type is a directory, and "FALSE" for files.
		@fsize - returns the size of the file in bytes.
		@fdate - returns the last modified date of the file.
		@ftime - returns the last modified time of the file.

		To include special characters in the command 
		line, use the hexadecimal code for the character
		in 0xHH format (ex. 0x09 for tab). Internal 
		CMD.exe commands should be preceded with "cmd /c".
/D 	date 	Selects files with a last modified date 
		greater than or equal to (+), or less than or equal to
		(-), the specified date using the "MM/dd/yyyy" 
		format; or selects files with a last modified date 
		greater than or equal to (+) the current date plus 
		"dd" days, or less than or equal to (-) the current 
		date minus "dd" days. A valid "dd" number of days 
		can be any number in the range of 0 - 32768.
		"+" is taken as default sign if not specified.
/? 		Displays this help message.

Using forfiles, we can setup a batch or command file (*.bat, *.cmd) to perform a task on files in specific folder.

Example:

forfiles /p “C:\Backup” /D -60 /C “cmd /c del @fname.@ext”

The command above will scan the folder C:\Backup for files that are older than 60 days and delete them.  You can further tell the command to scan subfolders within the original folder by adding /S parameter before /D (/S /D -60...)

After creating the BAT or CMD file, you can create a scheduled task in Windows to periodically execute the file.

Updated on April 9, 2018

Was this article helpful?

Need Support?
Can’t find the answer you’re looking for? Don’t worry we’re here to help!
Contact Support