Computing.Net > Forums > Programming > Batch File - controled backup

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch File - controled backup

Reply to Message Icon

Name: godling
Date: February 28, 2006 at 03:21:54 Pacific
OS: windows server 2000
CPU/Ram: 1.8ghz
Product: HP
Comment:

I have a batch file that creates a new directory named the current date using the mmddyyyy format. It then copies the contents of a directory to the newly created directory in the backup directory. I am trying to add to it so that it will automatically remove and delete without prompting a backup directory that is more than 7 days old. This is what the batch file already has in it.

@echo off
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET date=%mm%%dd%%yyyy%
md C:\backup\%date%
xcopy /e C:\data\*.* C:\backup\%date%



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: February 28, 2006 at 06:08:14 Pacific
Reply:

A couple of comments.

[1] This forum has been around the block with "date math". Not easy. I think it was dtech10 who finally hammered out a "julian date" BAT.

[2] w2k has a built in var called DATE. Depending on your system date layout you can:

echo %date:~10,4%%date:~7,2%%date:~4,2%
[in my case]

or

set myVAR=%date:~10,4%%date:~7,2%%date:~4,2%

and nevermind all those FORs.

[3] Resetting DATE in the BAT is probably a receipe for trouble.

[4] If it will suit your purposes, I can work up a BAT which will keep the most recent x number of backups.


If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: godling
Date: March 2, 2006 at 04:27:47 Pacific
Reply:

I realized I was making it much harder than it had to be. Since I only needed to keep it for seven days I renamed the folders after the day of the week instead and rewrote the batch file as follows.

FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET day=%%A
rmdir /s /q C:\backup\%day%
md C:\backup\%day%
xcopy /e C:\sms\*.* C:\backup\%day%

I feel very silly sorry to waste anyone's time.


0

Response Number 3
Name: Mechanix2Go
Date: March 2, 2006 at 22:15:56 Pacific
Reply:

Great solution.

The simplest way is usually the best.


If at first you don't succeed, you're about average.

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch File - controled backup

Batch file help www.computing.net/answers/programming/batch-file-help/14346.html

batch file question www.computing.net/answers/programming/batch-file-question/17284.html

Batch File www.computing.net/answers/programming/batch-file/18964.html