1.

Solve : Batch to create a backup.?

Answer»

I want to create a batch file to copy my email profile every time I boot up my PC.
I know the code to do the copying process.

I would like to keep the last 5 backups. So BackUp1, Backup2, BackUp3 and so on. These could be different folders.

If possible I would also like to just do one copy per day. In other words if the PC was rebooted many times in one day, I just want one copy for that day.

Any help you can provide me with would be great.
Cheers,
Code: [Select]@echo off

for /f "skip=1 tokens=2-4 delims=(./-)" %%a in ('echo.^|date') do (
for /f "tokens=1-3 delims=./- " %%A in ("%date:~-10%") do (
set %%a=%%A& set %%b=%%B& set %%c=%%C
))
echo Today Stamp is '%yy%%mm%%dd%'

md %yy%%mm%%dd%.bak && (
echo copy your files here to folder %yy%%mm%%dd%.bak
for /f "skip=5" %%a in ('dir/b/ad/o-n 20*.bak') do rd/s/q %%a
)
Thanks for the reply.
I have tried the code and it works do far. Thanks again.

Do i have to put my code where your line that contains the
'echo copy your files here to folder %dd%%mm%%yy%.bak' code is
Hence does the last line of code have to be
'for /f "skip=5" %%a in ('dir/b/ad/o-n 20*.bak') do rd/s/q %%a'

Or if you could step me through your code so I know what it doing I could work it out from there.

Cheers very much.

After further experimentation I think a better option would be to have five folders named 'Bak1' 'Bak2', 'Bak3', and so on.
The reason this is is that my email folder is quite big and with the existing code the whole folder will have to written every time the code is run.
Sicking with common folder names would mean the xcopy command would only have to up date files that are new or have been altered.

How could I ADJUST the code to take into account of this.
I would need the code to check or keep account of the last used directory/folder I guess.

Any help would be much appreciated.
Cheers,


Bak1-bak5 will result in longer code, i guess, as i havent tried it.
but as you only need to update existing files, faster execution is more important. i'll see what i can write after i go home, or if anyone else have some idea, dont hesitate to post it.
twosides, if you have partial code, please post it.

about the previous code, i am not good at explaining:
line 3-6 is to get datestamp as close as possible with the many dfferent of REGIONAL settings (ANOTHER way is
to use debug, windows scripting or regedit)
line 7 is screen output so i knew correct datestamp is obtained (this line can be commented)
line 9 create folder yyyymmdd.bak, if no error, execute line 10,11,12
line 10 you are supposed to replace the echo statement with the xcopy/copy statement
line 11 keep the last 5 folder, using dir as a sorter
for more information type for/?, dir/?, md/?, rd/? for help

Code: [Select][1]@echo off
[2]
[3]for /f "skip=1 tokens=2-4 delims=(./-)" %%a in ('echo.^|date') do (
[4] for /f "tokens=1-3 delims=./- " %%A in ("%date:~-10%") do (
[5] set %%a=%%A& set %%b=%%B& set %%c=%%C
[6]))
[7]echo Today Stamp is '%yy%%mm%%dd%'
[8]
[9]md %yy%%mm%%dd%.bak && (
[10] echo copy your files here to folder %yy%%mm%%dd%.bak
[11] for /f "skip=5" %%a in ('dir/b/ad/o-n 20*.bak') do rd/s/q %%a
[12])
haha, finally, after some mock-up on a few alternative, i think this is the best i can comeup with atm.
it use no temporary file and not involving with the cumbersome date math.

Code: [Select]@echo off & setlocal enabledelayedexpansion

for /l %%a in (5,-1,1) do set/a bak%%a=%%a%%5+1 & md bak%%a 2>nul

for /f %%a in ('dir/b/ad/tw/o-d bak?') do (
set d=%%~ta
if "!d:~0,-6!" NEQ "%date%" (
echo copy your files to folder bak!%%~na!
)
goto:eof
)

i havent test the code intensively, so let me know if it works or not.Many thanks for your help.
Your code creates the directories but does not echo the message.
I have added a line of code to do the copying.

It still does not do any copying.
Any ideas as this is nearly there, I just cant see how to do it.
Cheers.

Code: [Select]@echo off & setlocal enabledelayedexpansion

for /l %%a in (5,-1,1) do set/a bak%%a=%%a%%5+1 & md bak%%a 2>nul

for /f %%a in ('dir/b/ad/tw/o-d bak?') do (
set d=%%~ta
if "!d:~0,-6!" neq "%date%" (
echo copy your files to folder bak!%%~na!

)
goto:eof
)

xcopy /s /c /d /e /h /i /r /k /y c:\Gail c:\bak!%%~na!Code: [Select][1]@echo off & setlocal enabledelayedexpansion
[2]
[3]for /l %%a in (1,1,5) do set/a bak%%a=%%a%%5+1 & md bak%%a 2>nul
[4]::set bak&dir/ad/tw/o-d bak?
[5]for /f %%a in ('dir/b/ad/tw/o-d bak?') do (
[6] set d=%%~ta
[7] if "!d:~0,-6!" neq "%date%" (
[8] xcopy /s /c /d /e /h /i /r /k /y c:\Gail c:\bak!%%~na!
[9] )
[10] goto:eof
[11])

line 3: create variable array of bak1-bak5, teaching the computer to learn round-robin counting
4,5,1,2,3,4,5,1,... then create directory of bak1-bak5 supressing the error message if the folder exist
line 5: use dir as a sorter to list directory bak? with the newest (written date) on top
line 6: get the date last written of the directory, its necessary since it cannot be shorten to !%%~ta:~0,-6!
line 7: compare the date of directory against current date, if not the same do xcopy at line 8
line 10: terminate because we only need to process the first directory, note, anyline after this line is skipped.
**uncomment line 4 to undestand how things works within the batch code.

yeah, that's the drawback, the first time the bat run, it wont do the xcopy, because the date directory created is the same date as today, it will only do the xcopy tomorrow. so it need another variable to check if the directory is just created, its up to you to modify the code.

sidenote: the first time run, the directory chosen is random, and not always starting from bak1.So this is my code so far.
Using a little routine to rotate the directory names.
The only problem is that it always has to copy the files to BackUp_1 from the start.
I was hoping somehow to benefit from the files just been refreshed rather than a full copy but as the BackUp_1 directory is always new and hence empty then a full copy is always been writen to directory BackUp_1

I could do with trying to get the code to sequence or rotate the 'xcopy' command through the directories once they are made. This would just refresh after the initial 5 full copy processes and hence speed things up then.

I am struggling to see how I would do this though.

Any advice etc

Cheers,

Code: [Select]@echo off

if exist c:\BackUp_5 rd /s /q c:\BackUp_5
if exist c:\BackUp_4 ren c:\BackUp_4 BackUp_5
if exist c:\BackUp_3 ren c:\BackUp_3 BackUp_4
if exist c:\BackUp_2 ren c:\BackUp_2 BackUp_3
if exist c:\BackUp_1 ren c:\BackUp_1 BackUp_2

::Make dir
IF NOT EXIST C:\BackUp_1 MD C:\BackUp_1
IF exist c:\BackUp_1 xcopy /s /c /d /e /h /i /r /k /y c:\Gail c:\BackUp_1

exitnice one, i like your batch. here is my suggestion so you only refresh the existing folder.
use a temp folder when renaming as shown below:

if exist c:\BackUp_5 ren c:\BackUp_5 BackUp_6
if exist c:\BackUp_4 ren c:\BackUp_4 BackUp_5
if exist c:\BackUp_3 ren c:\BackUp_3 BackUp_4
if exist c:\BackUp_2 ren c:\BackUp_2 BackUp_3
if exist c:\BackUp_1 ren c:\BackUp_1 BackUp_2
if exist c:\BackUp_6 ren c:\BackUp_6 BackUp_1

Quote

If possible I would also like to just do one copy per day.
how about this?


Discussion

No Comment Found