Your Ad Here
 

Reply to comment

devans's picture

Using Forfiles in a BAT script to Purge Old Data on Windows

Your rating: None Average: 3 (2 votes)

As somewhat a continuance from my post regarding the use 7-Zip as part of an overall backup solution therein also lies the need for a file retention strategy too.

If we simply allow the creation of daily or weekly backups to run unchecked then dependant upon the size of the archive, the frequency of the backup job and ultimately the amount of available disk space we reach some kind of threshold. This necessitates the need for a retention strategy.

Daily backups may be viewed as no longer relevant after 24-hours in which case you could simply overwrite the data with the new information or if your retention schedule is defined as such, purge the information after say one week of retention.

The same could also be said for weekly or monthly backups for which you have defined that 30 days (for weekly) or 90 days (for monthly) is sufficient.

Having established your basis for a retention strategy you then have the task to follow through with executing and maintaining the retention strategy. Busy schedules being what they are we do not always have time to check without fail that we have compiled sufficient backups and can safely purge the remainder. Sooner rather than later this will start to feel like a neverending task, but do not worry we can make this easy!

To begin you will need to download the tool forfiles.exe. This can be obtain from here . Extract the downloaded file to your c:\ root directory and then continue to read below.

To automate this process we can simply create a .BAT file that does the purging for us and assign this task to be executed via the Windows Scheduler at pre-defined intervals.

For the weekly backups we can purge related information weekly and for the monthly backups we can purge on a quarterly basis.

Here is an example of a .BAT script that could be run weekly to purge data older than 30-days.


forfiles /P c:\ /M *backup.zip /D -30 /C "cmd /C del @file"

Here is an example of a .BAT script that could be run monthly to purge data older than 90-days.


forfiles /P c:\ /M *backup.zip /D -90 /C "cmd /C del @file"

In this next example we are using 'forfiles' to maintain log files to a safe level.


forfiles /P d:\logs /S /M *.log /D -30 /C "cmd /C del @file"

So what does the above really mean? Basically speaking we are telling forfiles to begin at the path (/P) of d:\logs then recursively (/S) search the directory for files identified as *.log (/M *.log). It then goes on to check for files being dated 30 days or older (/D -30) and run from the command line (/C "cmd) the specified command for each file (/C del @file"), in this case the delete command.

To re-iterate here we have a script that is designed to recursively search the log directory for files named *.log (where * indicates a wildcard) and providing they are more than 30 days old delete them.

Naturally this example is aimed at maintaining a directory of logs to a sufficient level that it does not choke the drive on which it resides. It you only have a single logical drive, the default c:\ for example, filling this drive with log files that are rotated daily or backup archives created at a regular interval spells trouble at some point!

By utilising the 'forfiles' application you are able to maintain a healthy level of files on your system without sacrificing the availability of your system/applications and or backup data.

I hope this page has helped you gain an understanding into what is a great way for maintaing file systems to appropriate levels. If anyone has any other great uses for this tool please let me know.

Enjoy!

Reply

The content of this field is kept private and will not be shown publicly.
 
  • Allowed HTML tags: <a><em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
Testing for human visitors.