Script to delete files older than 7 days

0
135
 Following our script series we will explain in this tutorial the way to create script to delete files older than 7 days. Will perform this operation using forfiles to delete files older than. If you work with backups you know that doesn’t need backups older than x days. For that you need to delete manually every few days. This script provide solution to automate this process. You can schedule to run automatically and save some time of your work. Worked on most windows platforms.

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"
echo Done!

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"
echo Done!

 

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:

SHARE
Previous articleScript to check disk space on multiple servers
Next articleBatchFile – Script To Zip File
mm
Hi, I’m Ravinder Jaiswal. I’m the person behind this website. Thank you for visiting the website! My website is all about Microsoft, VMware, Red Hat, Citrix and other technologies. I have 14+ years of IT experience. I am AZURE, MCSA 2016, MCSE 2016, RHCSA 7, RHCE 7, VCP-DCV 6.5, VCP-DTM 6.1, ITILv3 certified.