How to write a batch file for backup?

E

Eric

Does anyone have any suggestions on how to write a batch file to backup a
list of files and directories?
For example,
There is a list of files for backup
C:\backup1.txt
C:\backup2.txt
C:\backup3.txt
There is a list of directories for backup, I would like to backup everything
within following directory, including its sub-directory.
C:\Directory1\
C:\Directory2\
C:\Directory3\

Does anyone have any suggestions?
Thanks in advance for any suggestions
Eric
 
P

Pegasus \(MVP\)

Eric said:
Does anyone have any suggestions on how to write a batch file to backup a
list of files and directories?
For example,
There is a list of files for backup
C:\backup1.txt
C:\backup2.txt
C:\backup3.txt
There is a list of directories for backup, I would like to backup
everything
within following directory, including its sub-directory.
C:\Directory1\
C:\Directory2\
C:\Directory3\

Does anyone have any suggestions?
Thanks in advance for any suggestions
Eric

Here you go:
@echo off
xcopy /d /y c:\backup1.txt "F:\My Backups\"
xcopy /d /y c:\backup2.txt "F:\My Backups\"
xcopy /d /y c:\backup3.txt "F:\My Backups\"
xcopy /d /y /s c:\Directory1.txt "F:\My Backups\Directory1\"
xcopy /d /y /s c:\Directory2.txt "F:\My Backups\Directory2\"
xcopy /d /y /s c:\Directory3.txt "F:\My Backups\Directory3\"

I recommend that you do this in order to work out for yourself what the
various switches mean:
- Click Start / Run / cm{OK}
- Type this command:
xcopy /? | more{Enter}
 
B

Big_Al

Pegasus said:
Here you go:
@echo off
xcopy /d /y c:\backup1.txt "F:\My Backups\"
xcopy /d /y c:\backup2.txt "F:\My Backups\"
xcopy /d /y c:\backup3.txt "F:\My Backups\"
xcopy /d /y /s c:\Directory1.txt "F:\My Backups\Directory1\"
xcopy /d /y /s c:\Directory2.txt "F:\My Backups\Directory2\"
xcopy /d /y /s c:\Directory3.txt "F:\My Backups\Directory3\"

I recommend that you do this in order to work out for yourself what the
various switches mean:
- Click Start / Run / cm{OK}
- Type this command:
xcopy /? | more{Enter}

Sorry Pegasus, dont you mean
xcopy /d /y /s c:\Directory1\*.* "F:\My Backups\Directory1\"
xcopy /d /y /s c:\Directory2\*.* "F:\My Backups\Directory2\"
xcopy /d /y /s c:\Directory3\*.* "F:\My Backups\Directory3\"
 
P

Pegasus \(MVP\)

Big_Al said:
Sorry Pegasus, dont you mean
xcopy /d /y /s c:\Directory1\*.* "F:\My Backups\Directory1\"
xcopy /d /y /s c:\Directory2\*.* "F:\My Backups\Directory2\"
xcopy /d /y /s c:\Directory3\*.* "F:\My Backups\Directory3\"

I did, of course, but got a little carried away with copying and pasting.
Thanks for pointing out my error.
 
P

(PeteCresswell)

Per Eric:
Does anyone have any suggestions?

Batch files will do the job, but I like something called
SecondCopy - which does basically the same thing with a few bells
and whistles - like keeping multiple copies of replaced files.
 
E

Eric

Thank everyone very much for suggestions

Within the batch file, will it be possible to create a folder named by today
before copy into this folder? such as
"F:\My Backups\291108", which stands for 29-Nov-2008
Does anyone have any suggestions?
Thank everyone very much for any suggestions
Eric
 
B

Big_Al

Eric said:
Thank everyone very much for suggestions

Within the batch file, will it be possible to create a folder named by today
before copy into this folder? such as
"F:\My Backups\291108", which stands for 29-Nov-2008
Does anyone have any suggestions?
Thank everyone very much for any suggestions
Eric
Odd you ask! I just saw this a few days ago and use it myself.
----------------------------------
for /f "tokens=2-4 delims=/ " %%i in ('date/t') do (
(set MON=%%i)
(set DAY=%%j)
(set YR=%%k)
)
set TODAY=%YR%%MON%%DAY%

rem .... this makes the TODAY and MON DAY YR variables.
-----------------------------
Put the part between the ----'s in your batch file.
Now you can use
%TODAY%.txt
etc in your batch file. OR you can use the individual pieces MON DAY YR.
 
E

Eric

Thank everyone very much for suggestions

So how should I write coding on batch file?
The following code does not seem working.
Thank everyone very much for any suggestions
Eric

for /f "tokens=2-4 delims=/ " %%i in ('date/t') do (
(set MON=%%i)
(set DAY=%%j)
(set YR=%%k)
)
set TODAY=%YR%%MON%%DAY%

xcopy /d /y c:\backup1.txt "F:\My Backups\TODAY"
xcopy /d /y /s c:\Directory1\*.* "F:\My Backups\TODAY\Directory1\"
 
B

Big_Al

Eric said:
Thank everyone very much for suggestions

So how should I write coding on batch file?
The following code does not seem working.
Thank everyone very much for any suggestions
Eric

for /f "tokens=2-4 delims=/ " %%i in ('date/t') do (
(set MON=%%i)
(set DAY=%%j)
(set YR=%%k)
)
set TODAY=%YR%%MON%%DAY%

xcopy /d /y c:\backup1.txt "F:\My Backups\TODAY"
xcopy /d /y /s c:\Directory1\*.* "F:\My Backups\TODAY\Directory1\"
Using a variable like TODAY is done this way.

xcopy /d /y c:\backup1.txt "F:\My Backups\%TODAY%"
xcopy /d /y /s c:\Directory1\*.* "F:\My Backups\%TODAY%\Directory1\"


You put the variable surrounded by %'s
 
E

Eric

Thank everyone very much for suggestions

When I run the batch file, it generates a folder Nov08 and miss the day.
Do you have any suggestions on how to change the format into 291108? which
represent 29 Nov, 2008.
Thank everyone very much for any suggestions
Eric


for /f "tokens=2-4 delims=/ " %%i in ('date/t') do (
(set MON=%%i)
(set DAY=%%j)
(set YR=%%k)
)
set TODAY=%YR%%MON%%DAY%

xcopy /d /y /s "D:\Directory1\file1.txt" "F:\Backup\%TODAY%\"
 
E

Eric

Thank everyone very much for suggestions
My files are placed at different directories, I don't like to select every
file everytiime I perform backup.
Regards
Eric
 
P

Pegasus \(MVP\)

Eric said:
Thank everyone very much for suggestions

Within the batch file, will it be possible to create a folder named by
today
before copy into this folder? such as
"F:\My Backups\291108", which stands for 29-Nov-2008
Does anyone have any suggestions?
Thank everyone very much for any suggestions
Eric

Yes, it is, but the method depends on the date format generated by your
installation. Do this:
- Click Start / Run / cmd {OK}
- Type this command:
echo %date%{Enter}
- Report exactly what you see.
 
E

Eric

Thank everyone very much for suggestions
- Click Start / Run / cmd {OK}
- Type this command:
echo %date%{Enter}

29/Nov/08 is displayed.
When I run the batch file, it generates a folder Nov08 instead of 29/Nov/08
and miss the day for today.
Do you have any suggestions on how to show the date in any format? which
represent 29 Nov, 2008.
Thank everyone very much for any suggestions
Eric

H
 
P

Pegasus \(MVP\)

"29/Nov/08" is a somewhat unusual date format. Note also that you cannot
have forward or back slashes in your folder names. Try this batch file:
@echo off
set MyDate=%date:/=%
xcopy /d /y c:\backup1.txt "F:\My Backups\%MyDate%\"
xcopy /d /y c:\backup2.txt "F:\My Backups\%MyDate%\"
xcopy /d /y c:\backup3.txt "F:\My Backups\%MyDate%\"
xcopy /d /y /s c:\Directory1 "F:\My Backups\%MyDate%\Directory1\"
xcopy /d /y /s c:\Directory2 "F:\My Backups\%MyDate%\Directory2\"
xcopy /d /y /s c:\Directory3 "F:\My Backups\%MyDate%\Directory3\"

You could also do this:
@echo off
set MyDate=%date:/=-%
xcopy /d /y c:\backup1.txt "F:\My Backups\%MyDate%\"
xcopy /d /y c:\backup2.txt "F:\My Backups\%MyDate%\"
xcopy /d /y c:\backup3.txt "F:\My Backups\%MyDate%\"
xcopy /d /y /s c:\Directory1 "F:\My Backups\%MyDate%\Directory1\"
xcopy /d /y /s c:\Directory2 "F:\My Backups\%MyDate%\Directory2\"
xcopy /d /y /s c:\Directory3 "F:\My Backups\%MyDate%\Directory3\"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top