Batch file to backup files the newest files

Microsoft Windows server 2003 enterprise
November 29, 2009 at 18:01:48
Specs: Windows 7
Hi there.
I have a backup folder that has files with a filename like this:

200911300627-www.xxxxxxxx.com.au.tar.gz

Each day, there is 14 different files with the same date range.

How do I go about creating a Batch script that will copy over the files with todays date and also delete the oldest from the folder?
The folder needs to hold 5 days of backups.
So basically, every day, the new files are added and the oldest (5 day old files) are removed from the folder.

This will run on a win2k3 server using scheduled tasks

Any help appreciated


See More: Batch file to backup files the newest files

Report •


#1
November 29, 2009 at 20:17:29
I'm not quite with you but does this boil down to 'keep the five newest files' ?


=====================================
Helping others achieve escape felicity

M2


Report •

#2
November 29, 2009 at 20:50:11
Yeh basically thats correct. We have a folder with maybe 200+ backups on that server (they are only a couple of meg files).
We would like to keep the 5 newest back ups on a different server

Report •

#3
November 29, 2009 at 21:02:58
@echo off & setLocal EnableDELAYedExpansion

set N=
for /f "tokens=* delims= " %%a in ('dir/b/o-d/a-d') do (
set /a N+=1
if !N! gtr 5 goto :eof
copy "%%a" d:\bak
)


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
November 29, 2009 at 21:08:26
Great. Thanks for the quick reply. How do I use this exactly? Where do I put my original folder to be backed up? I rang the file and it just crated a file without an extension with the name specified on the second last line (d:\bak)

Report •

#5
November 29, 2009 at 21:31:04
This presupposes your destination exists.

===========================
@echo off & setLocal EnableDELAYedExpansion

set /p src=source ?:
set /p dest=destination ?:

set N=
pushd "!src!"
for /f "tokens=* delims= " %%a in ('dir/b/o-d/a-d') do (
set /a N+=1
if !N! gtr 5 goto :eof
copy "%%a" "!dest!\"
)


=====================================
Helping others achieve escape felicity

M2


Report •

#6
November 30, 2009 at 14:06:54
Hi. Thanks for that.
I've tryed it and am having a couple of problems, and maybe didnt explain correctly.
The folder that has the backups has around 14 different backups for the one day.
So really, the folder that the back ups will go to will have 14 * 5 files (around 84 files at one time).

I guess this could be done if the original backup manually had the 5 days worth of files, and then all the script has to do is remove the oldest backup in the folder (with the date range being the lowest) and just copy the newest out of the folder to be backed up (with the date range the highest)?


Report •

#7
December 1, 2009 at 14:12:13
anybody?

Report •

#8
December 4, 2009 at 09:51:00
change

if !N! gtr 5 goto :eof

to

if !N! gtr 84 goto :eof


Report •


Ask Question