Ask a question »

Batch script to delete file older than 30 days

June 2013

Batch files can be created for customized tasks such as cleaning up older files from a system or copying files from one folder to another as backup. Some programming knowledge can prove useful when writing batch scripts. To write this script, the forfiles command can be used. The batch script can be used to automatically delete files at the root of the directory specified. More than one directory can also be included in the batch file. The batch script can be scheduled to run with the scheduled tasks.


Issue


I would like to create a batch script which could copy files older than 30 days from one folder to another. I would also like it to delete the 30 days old files from the source folder after copying them to the destination folder.

Solution


Try the following command to automatically delete files at the root of a directory. This command can be saved in a .bat file and scheduled with scheduled tasks.

forfiles /p "PATH" /s /d -30 /c "cmd /c del @file : date >= 30 days >NUL"


Change "PATH" with the path where you want the files to be deleted. If there are no spaces in the path you don't need the - symbol.

Note


Solved by Thomas

See also

Knowledge communities.

Published by aakai1056 - Latest update by Jeff
This document entitled « Batch script to delete file older than 30 days » from Kioskea (en.kioskea.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.
Receive our newsletter

health.kioskea.net

Dos arithmetic
remotely access another computer using C#