Limit Backups In Folder To Files Created Within 30 Days

Recently, I was setting up Sage 50 Automatic Backups for a client. The utility lacks the ability to automatically clean-up old backups.

Each backup file created has the date postpended to the filename. “Overwrite the existing backup file” would only apply if you ran two backups on the same Company on the same day.

After time, the backup directory will need to be cleaned up. Instead of trying to remember to manually remove old backups, you can handle this problem with a simple batch script.

Open up notepad, insert the following, and save as whatever-you-want.bat. You’ll have to change the paths to match your environment.

forfiles /p "D:\Sage\Peachtree\Company\Backups" /m *.ptb /D -30 /C "cmd /c del @PATH"

Before you actually run this, you should use echo instead of delete and write the results to a text file. See below. The results.txt file will contain the files that would be deleted if you were actually using delete instead of echo.

forfiles /p "D:\Sage\Peachtree\Company\Backups" /m *.ptb /D -30 /C "cmd /c echo @PATH >> results.txt"

After you test it, you can add it as a scheduled task. In our case, we always have 30 days of backups on hand. In addition, we have cloud backups of these backups and so on…

Here’s more information on the Windows command forfiles:

This technique can be used for other database dumps such as MySQL, Microsoft SQL Server, QuickBooks, etc.