Script to delete all files older than 7 days
We will configure the script to delete all files older than 7 days.
Echo forfiles delete files older than 7 days
forfiles -p D:\Backup\ -s -m *.* /D -7 /C "cmd /c del /q @path"
Script to delete files older than 7 days based on extension.
In this example we will configure script to delete old files with .bak . Copy the below command and past in text file. Save as delete.bat. Save anywhere except D:\Backup\ .
Echo Forfiles to delete files older than 7 days
forfiles -p D:\Backup\ -s -m *.bak* /D -7 /C "cmd /c del /q @path"
Explanation of the commands:
D:\Backup\ – Replace with your path.
*.bak* – Replace with your file extension.
-7 – Period to be deleted. Can replace with your needs.
- -p – The path to search for the files you want to check the date of and remove.
- -s – Recourse subdirectories contained within the path specified using /p and check them as well. Remove if you doesn’t want to delete files in subdirectories.
- -m – The search mask to be used for the file type you want to check the date on (*.* being all files).
- /d – The date to compare the files. Also can be used (dd/mm/yyyy) a standard date type.
- /c – The command to be used on a file that matches the /m and /d criteria.
- /q – Used within /c to instruct the del command to delete files quietly.
Other Parameters here.
Also you can READ: