Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Backup Script

Status
Not open for further replies.

Brianfree

Programmer
Joined
Feb 6, 2008
Messages
220
Location
GB
Hi, i am trying to write a backup script (backup.bat) on an xp Pro pc to backup files/directories to a folder with todays date i.e...

2008-02-06

Please can anyone help!

regards,

BF
 
Well, to create a folder with today's date, you could just do:

Code:
echo %date% > temp.txt
for /F "tokens=1,2,3,4 delims=/ " %%1 in (temp.txt) do set dirname=%4-%3-%2
mkdir %dirname%

Then it's just a matter of getting using whatever method you use of backing up your data: ntbackup, xcopy, copy, etc.
 
Sorry, if that's supposed to be YYYY-MM-DD, it should be: set dirname=%4-%2-%3
 
Sorry, one more correction (that's what I get for not actually testing in a batch file)

Code:
echo %date% > temp.txt
for /F "tokens=1,2,3,4 delims=/ " %%1 in (temp.txt) do echo %%4-%%3-%%2 > temp2.txt
for /F %%1 in (temp2.txt) do mkdir %%1

100% works
 
Hi, i have the following code which works...

Code:
@ECHO OFF

FOR /F "tokens=1-4 delims=/ " %%I IN ('DATE /t') DO SET DATESTAMP=%%K-%%I-%%J
MD K:\BACKUPS\%DATESTAMP%

XCOPY K:\DATA\*.* K:\BACKUPS\%DATESTAMP%

How can i now modify this so it backs up to a folder called Monday, Tuesday, etc... (a backup for each day of the week) deleting what ever may be in the folder first. So i can have access to files on any day over the last week?

Kindest regards,

BF
 
Please can anyone help with modifying this script for use with a 'weekday' named backup?

Kindest regards,

Brian
 
Something like this would work:
Code:
@echo off

REM set paths to folders
set storage=K:\BACKUPS
set data=K:\DATA

REM get day of the week
FOR /F "tokens=1 delims=/ " %%I IN ('DATE /t') DO SET today=%%I

REM create the folder if it doesn't already exist
IF NOT EXIST==%storage%\%today% md %storage%\%today%

REM delete anything that might already be there
del %storage%\%today%\*.* /q /s

REM copy the stuff
XCOPY %data%\*.* %storage%\%today%

exit
 
Thankyou for replying, i will test this; this morning.

Kindest regards,

BF
 
Hi i have run this script thanks and it creates a folder called "27" in the backup directory. How can i modify this so it creates a directory called Wed_2008-02-27

Kindest regards,

BF
 
Well, on mine that picks up the day - like Wed. If you're getting a different token, then it would be because your date format is different. You might need to adjust the tokens and/or delimiters (did you include the space character in the delimiters?). At the command line, type date /t - if your format is something other than 'Wed 02/27/2008', you'll need to make some adjustments.

The last batch that I posted was working with this:
Brainfree said:
How can i now modify this so it backs up to a folder called Monday, Tuesday, etc... (a backup for each day of the week) deleting what ever may be in the folder first. So i can have access to files on any day over the last week?

If you want to go back to indvidual dates for the folder names, go back to what your earlier code which you said works:
Brainfree said:
Hi, i have the following code which works...

Code:
@ECHO OFF

FOR /F "tokens=1-4 delims=/ " %%I IN ('DATE /t') DO SET DATESTAMP=%%K-%%I-%%J
MD K:\BACKUPS\%DATESTAMP%

XCOPY K:\DATA\*.* K:\BACKUPS\%DATESTAMP%
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top