Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: batch file as backup: delete oldest file(s)?  (Read 5524 times)

0 Members and 1 Guest are viewing this topic.

xcopywarrior

  • Guest
batch file as backup: delete oldest file(s)?
« on: May 09, 2007, 09:18:31 AM »
Hi, and thanks in advance for reading.

I frequently use batch files for backing up important files, etc.  Scheduling is done through Windows "Scheduled Tasks" applet.  For example, "xcopy c:\stuff\* \\pc2\c$\backup\"...  Simple xcopy stuff.

What I would like to do is to be able to keep a week's worth of revisions of a given file, with the batch commands simply killing off the oldest copy and adding the newest copy.  The object is to have, at any given time, the five most recent versions of the file in backup.

Since I'm not really a programmer, just a simple batch file guy, I don't know how to do this.  Any ideas?  The simpler (more reliable), the better!

Thanks in advance!

gpl



    Apprentice
  • Thanked: 27
    Re: batch file as backup: delete oldest file(s)?
    « Reply #1 on: May 09, 2007, 11:49:36 AM »
    The oldest filename can be grabbed like this :
    Code: [Select]
    Set FileName=
    > $temp$ Dir /b /od "path"
    Set /P FileName=<$temp$
    Del $temp$

    This does a directory listing of the directory identified above as path - into a file, sorted in order of date (oldest first). The set statement takes the first line from the file and places it into the variable (the oldest file)

    Good luck
    Graham

    GuruGary



      Adviser
      Re: batch file as backup: delete oldest file(s)?
      « Reply #2 on: May 09, 2007, 10:08:38 PM »
      To make sure I understand, you are looking for code that will keep 5 backup copies of C:\Stuff?  So basically what you want is C:\Stuff to be backed up to:
      \\pc2\C$\Backup\Stuff1
      \\pc2\C$\Backup\Stuff2
      \\pc2\C$\Backup\Stuff3
      \\pc2\C$\Backup\Stuff4
      \\pc2\C$\Backup\Stuff5
      and then on day 6, the Stuff 5 gets deleted, and everything gets bumped up a number?

      xcopywarrior

      • Guest
      Re: batch file as backup: delete oldest file(s)?
      « Reply #3 on: May 10, 2007, 07:46:05 AM »
      Graham - Thanks a lot, I will try this.

      Gary - Yep, that's the idea.  The c:\stuff folder contains .bak files that get written every night.  I'd like to keep the five most recent .bak files in the backup folder, with the oldest effectively being replaced by the newest every night.  This is probably simplified by the fact that I'm not dealing with a messy folder filled with files and sub-folders, etc.

      In the backup folder, at any given time, I'll have five and only five files - just the five most recent backups.  On day six, and every day thereafter, the oldest gets canned and the newest gets written.

      Thanks, guys!

      GuruGary



        Adviser
        Re: batch file as backup: delete oldest file(s)?
        « Reply #4 on: May 10, 2007, 07:37:50 PM »
        I think this is what you want:
        Code: [Select]
        @echo off
        setlocal
        set NumBackups=5

        if exist C:\Backup\Stuff%NumBackups% rd C:\Backup\Stuff%NumBackups% /q /s
        for /l %%a in (%NumBackups%,-1,2) do call :BumpDir %%a
        xcopy C:\Stuff C:\Backup\Stuff1\ /e /h /k /c
        goto :EOF

        :BumpDir
        set /a Prev=%1-1
        if exist C:\Backup\Stuff%Prev% ren C:\Backup\Stuff%Prev% Stuff%1
        goto:EOF