This project has moved. For the latest updates, please go here.

Would someone be kind enough to check this post backup script for me.

Mar 29, 2016 at 4:01 PM

Firstly I wanted to thank you for this great backup program its really useful!

Would someone be kind enough to check this post backup script for me. I wanted it to back up, add a time stamp and to only keep the last 5 backups. However it is adding the time stamp but keeping all the backups.

Thanks in advance


rem set backup folder here
cd /D Y:\Angie_PST_Backup

setlocal EnableDelayedExpansion
set dt=%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%

rem iterate over all files and rename them
FOR /F "delims=" %%I IN ('DIR .pst .ost /B') DO (
set x=%%I
ren "!x!" "!x:~0,-4!_%dt%.bak"

@echo off
setlocal enableextensions enabledelayedexpansion

rem set max no of backups to keep and backup folder
cd /D Y:\Angie_PST_Backup

rem set default revision no to 0
set rev=0

rem read last revision no if exists
if exist revision.txt (
for /f "delims=" %%a in (revision.txt) do (
    set rev=%%a

rem calculate next revision no
Set /A rev=!rev!+1
Set /A rev=!rev! %% !MAX_BACKUP!

rem find pst- and osts-files and rename them
FOR /F "delims=" %%I IN ('DIR .pst .ost /B') DO (
set x=%%I
if exist "!x:~0,-4!_!rev!.bak" del "!x:~0,-4!_!rev!.bak"
ren "!x!" "!x:~0,-4!_!rev!.bak"

rem save current revision no
echo !rev! > revision.txt

Mar 29, 2016 at 8:49 PM
it seems that you merely concatenated the script for adding date and keeping N last backups.
This doesn't work because the last script works with cycling the filenames and overwriting the old ones.
email_0.bak, email_1.bak, email_2.bak
If you keep 3 backups, the next filename would be email_0.bak again which will overwrite the first file.

Now if you add a timestamp to the filename, this logic won't work any more.

Here is a sample how to keep the newest N files:

I suggest to go step by step. First add the date. If this works, go on with finding the old files and then take care of deleting.
You must have some experiences in scripting or programming to accomplish this.

Kind regards,
Mar 30, 2016 at 2:29 PM
Hi Thomas, thanks for the reply.

No wonder it wasn't working! As you can probably tell I have little experience with scripting or programming but I will take a look at the site you linked to.

Thanks again