Batch Script to Delete Old Files

Ask a question
Batch files can be created for customized tasks such as cleaning up older systems or copying files from one folder to another as a backup.

Batch Script Command to Delete Files

If you would like to create a batch script that can be used to copy files older than 30 days from one folder to another while simultaneously deleting the old files from the source folder, here's a sample batch script that can automate the task:
forfiles /p "PATH" /s /d -30 /c "cmd /c del @file : date >= 30 days >NUL"

Note that the PATH in the given code should be substituted by the path where you would like your files to be deleted. If there are no spaces in the path, the - symbol is not needed.

This command can be saved in a .bat file and may be scheduled with other automated tasks.

N.B. More than one directory may be included in this command.
Jean-François Pillou

CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jeff Pillou, founder of CCM reaches more than 50 million unique visitors per month and is available in 11 languages.

Learn more about the CCM team