Batch File Help

vendetta81

New Member
hi,

i'm trying to create a batch file that backs up data for a college assessment, i've got as far as making it back up fully and incrementally to a destination drive\folder.

The problem is i need help in understanding how to tell the batch file to backup to a folder which has the date as its title (which will change over time), then when the batch is run again at a later date it copys data to the same folder and updates the folders name as todays date.

hope i make sense, i've only basic knowledge of batch files ???.

thanks in advance for any help.

David :)
 
Yeh it has to be a batch for the assessment, we seem to just learn the basic.

now bare in mind i'm no expert, plz be nice,



echo off
rem turns off default display

title DSG Password Protected
rem gives batch file a title

color A
rem changes text color

mode con:cols=50 lines=15
rem fixes the batch files window size

cls
rem clears scren for menu

:pass
rem password protection

echo.
echo.
echo.
echo.

set /p pas= Please Type Your Password:

if '%pas%'== 'P@ssw0rd' goto Begining

echo.
echo Access Denied
echo.
pause

cls

goto pass


:Begining
rem start of batch file, for navigating around batch file

title DSG Backup Utility
rem gives batch file a title

mode con:cols=65 lines=20
rem fixes the batch files window size

cls
rem clear screen

echo.
echo.
echo.
echo.
echo 1. Start Backing Up Your Data
echo.
echo 2. Start Restoring Files from a previous Backup
echo.
echo 3. Exit Backup Utility
echo.
echo.
echo.
rem this is the first menu

set Choice=
rem creates a variable choice

set /p choice= Input relevent number and press ENTER
rem /p is the switch to pause the display and sets the variable choice from user's input

if /i '%choice%'=='1' goto DOIT

if /i '%choice%'=='2' goto DOIT3

if /i '%choice%'=='3' goto END

goto err
rem goes to err,when unlisted numbers/choices have been entered

goto end
rem when user decides to exit

:DOIT

cls
rem clear screen

rem opens the backup menu

echo.
echo.
echo.
echo.
echo 1. Create a full Backup of your Documents
echo.
echo 2. Create a Incremental Backup of Your Documents
echo.
echo 3. Return to Main Menu
echo.
echo 4. Exit Backup Utility
echo.
echo.
echo.
rem backup menu

set Choice=
rem creates a variable choice

set /p choice= Input relevent number and press ENTER
rem /p is the switch to pause the display and sets the variable choice from user's input

if /i '%choice%'=='1' goto DOIT1

if /i '%choice%'=='2' goto DOIT2

if /i '%choice%'=='3' goto Begining

if /i '%choice%'=='4' goto END

goto err3
rem goes to err,when unlisted numbers/choices have been entered

goto end
rem when user decides to exit

:DOIT1
rem full my documents backup (backs up all files)

echo off
rem turns off default display

title DSG Full Backup
rem gives batch file a title

color A
rem changes text color

mode con:cols=70 lines=20
rem fixes the batch files window size

cls
rem clears scren for menu

set Choice=
rem creates a variable choice

echo.
echo.
echo.
echo.
echo. This will backup all of your Documents, Emails, Contacts, Favorites
echo. Music, Pictures etc along With Your Registry
echo. To a drive of your choice
echo.
set /p choice= Please enter DRIVE letter to which you want to backup to :
rem /p is the switch to pause the display and sets the variable choice from user's input


if /i '%choice%'=='a' goto a
if /i '%choice%'=='b' goto a1
if /i '%choice%'=='c' goto a2
if /i '%choice%'=='d' goto a3
if /i '%choice%'=='e' goto a4
if /i '%choice%'=='f' goto a5
if /i '%choice%'=='g' goto a6
if /i '%choice%'=='h' goto a7
if /i '%choice%'=='i' goto a8
if /i '%choice%'=='j' goto a9

if /i '%choice%'=='k' goto b
if /i '%choice%'=='l' goto b1
if /i '%choice%'=='m' goto b2
if /i '%choice%'=='n' goto b3
if /i '%choice%'=='o' goto b4
if /i '%choice%'=='p' goto b5
if /i '%choice%'=='q' goto b6
if /i '%choice%'=='r' goto b7
if /i '%choice%'=='s' goto b8
if /i '%choice%'=='t' goto b9

if /i '%choice%'=='u' goto c
if /i '%choice%'=='v' goto c1
if /i '%choice%'=='w' goto c2
if /i '%choice%'=='x' goto c3
if /i '%choice%'=='y' goto c4
if /i '%choice%'=='z' goto c5

goto err1
rem goes to err,when unlisted numbers/choices have been entered

goto end
rem when user decides to exit

:a
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=a:\Backup\%FDate%
goto StartFull

:a1
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=b:\Backup\%FDate%
goto StartFull

:a2
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=c:\Backup\%FDate%
goto StartFull

:a3
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=d:\Backup\%FDate%
goto StartFull

:a4
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=e:\Backup\%FDate%
goto StartFull

:a5
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=f:\Backup\%FDate%
goto StartFull

:a6
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=g:\Backup\%FDate%
goto StartFull

:a7
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=h:\Backup\%FDate%
goto StartFull

:a8
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=i:\Backup\%FDate%
goto StartFull

:a9
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=j:\Backup\%FDate%
goto StartFull
:b
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=k:\Backup\%FDate%
goto StartFull

:b1
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=l:\Backup\%FDate%
goto StartFull

:b2
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=m:\Backup\%FDate%
goto StartFull

:b3
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=n:\Backup\%FDate%
goto StartFull

:b4
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=o:\Backup\%FDate%
goto StartFull

:b5
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=p:\Backup\%FDate%
goto StartFull

:b6
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=q:\Backup\%FDate%
goto StartFull

:b7
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=r:\Backup\%FDate%
goto StartFull

:b8
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=s:\Backup\%FDate%
goto StartFull

:b9
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=t:\Backup\%FDate%
goto StartFull

:c
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=u:\Backup\%FDate%
goto StartFull

:c1
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=v:\Backup\%FDate%
goto StartFull

:c2
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=w:\Backup\%FDate%
goto StartFull

:c3
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=x:\Backup\%FDate%
goto StartFull

:c4
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=y:\Backup\%FDate%
goto StartFull

:c5
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=z:\Backup\%FDate%
goto StartFull

:StartFull

set backupcmd=xcopy /s /c /e /h /i /r /y

%backupcmd% "%USERPROFILE%\Desktop" "%drive%\Desktop"
rem backing up desktop folder

%backupcmd% "%USERPROFILE%\Favorites" "%drive%\Favorites"
rem backing up favorites folder

%backupcmd% "%USERPROFILE%\My Documents" "%drive%\My Documents"
rem backing up my documents folder

%backupcmd% "%USERPROFILE%\Application Data\Microsoft\Address Book" "%drive%\Address Book"
%backupcmd% "%USERPROFILE%\Local Settings\Application Data\Identities" "%drive%\Outlook Express"
rem backing up outlook express emails and address book

%backupcmd% "%USERPROFILE%\Local Settings\Application Data\Microsoft\Outlook" "%drive%\Outlook"
rem backing up office outlook emails and contacts

if not exist "%drive%\Registry" mkdir "%drive%\Registry"
if exist "%drive%\Registry\regbackup.reg" del "%drive%\Registry\regbackup.reg"
regedit /e "%drive%\Registry\regbackup.reg"
rem backing up registry


echo *Full Backup Is Complete*
pause

goto begining


:DOIT2
rem incremental my documents backup (backs up only new or edited files)

echo off
rem turns off default display

title DSG Incremental Backup
rem gives batch file a title

color A
rem changes text color

mode con:cols=70 lines=20
rem fixes the batch files window size

cls
rem clears scren for menu

set Choice=
rem creates a variable choice

echo.
echo.
echo.
echo.
echo. This will backup all of your Documents, Emails, Contacts, Favorites
echo. Music, Pictures etc along With Your Registry
echo. To a drive of your choice
echo.
set /p choice= Please enter DRIVE letter to which you want to backup to :
rem /p is the switch to pause the display and sets the variable choice from user's input


if /i '%choice%'=='a' goto a
if /i '%choice%'=='b' goto a1
if /i '%choice%'=='c' goto a2
if /i '%choice%'=='d' goto a3
if /i '%choice%'=='e' goto a4
if /i '%choice%'=='f' goto a5
if /i '%choice%'=='g' goto a6
if /i '%choice%'=='h' goto a7
if /i '%choice%'=='i' goto a8
if /i '%choice%'=='j' goto a9

if /i '%choice%'=='k' goto b
if /i '%choice%'=='l' goto b1
if /i '%choice%'=='m' goto b2
if /i '%choice%'=='n' goto b3
if /i '%choice%'=='o' goto b4
if /i '%choice%'=='p' goto b5
if /i '%choice%'=='q' goto b6
if /i '%choice%'=='r' goto b7
if /i '%choice%'=='s' goto b8
if /i '%choice%'=='t' goto b9

if /i '%choice%'=='u' goto c
if /i '%choice%'=='v' goto c1
if /i '%choice%'=='w' goto c2
if /i '%choice%'=='x' goto c3
if /i '%choice%'=='y' goto c4
if /i '%choice%'=='z' goto c5

goto err2
rem goes to err,when unlisted numbers/choices have been entered

goto end
rem when user decides to exit

:a
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=a:\Backup\%FDate%
goto StartIncrement

:a1
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=b:\Backup\%FDate%
goto StartIncrement

:a2
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=c:\Backup\%FDate%
goto StartIncrement

:a3
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=d:\Backup\%FDate%
goto StartIncrement

:a4
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=e:\Backup\%FDate%
goto StartIncrement

:a5
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=f:\Backup\%FDate%
goto StartIncrement

:a6
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=g:\Backup\%FDate%
goto StartIncrement

:a7
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=h:\Backup\%FDate%
goto StartIncrement

:a8
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=i:\Backup\%FDate%
goto StartIncrement

:a9
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=j:\Backup\%FDate%
goto StartIncrement
:b
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=k:\Backup\%FDate%
goto StartIncrement

:b1
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=l:\Backup\%FDate%
goto StartIncrement

:b2
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=m:\Backup\%FDate%
goto StartIncrement

:b3
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=n:\Backup\%FDate%
goto StartIncrement

:b4
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=o:\Backup\%FDate%
goto StartIncrement

:b5
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=p:\Backup\%FDate%
goto StartIncrement

:b6
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=q:\Backup\%FDate%
goto StartIncrement

:b7
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=r:\Backup\%FDate%
goto StartIncrement

:b8
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=s:\Backup\%FDate%
goto StartIncrement

:b9
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=t:\Backup\%FDate%
goto StartIncrement

:c
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=u:\Backup\%FDate%
goto StartIncrement

:c1
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=v:\Backup\%FDate%
goto StartIncrement

:c2
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=w:\Backup\%FDate%
goto StartIncrement

:c3
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=x:\Backup\%FDate%
goto StartIncrement

:c4
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=y:\Backup\%FDate%
goto StartIncrement

:c5
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
set drive=z:\Backup\%FDate%
goto StartIncrement

:StartIncrement

set backupcmd=xcopy /s /c /d /e /h /i /r /y

%backupcmd% "%USERPROFILE%\Desktop" "%drive%\Desktop"
rem backing up desktop folder

%backupcmd% "%USERPROFILE%\Favorites" "%drive%\Favorites"
rem backing up favorites folder

%backupcmd% "%USERPROFILE%\My Documents" "%drive%\My Documents"
rem backing up my documents folder

%backupcmd% "%USERPROFILE%\Application Data\Microsoft\Address Book" "%drive%\Address Book"
%backupcmd% "%USERPROFILE%\Local Settings\Application Data\Identities" "%drive%\Outlook Express"
rem backing up outlook express emails and address book

%backupcmd% "%USERPROFILE%\Local Settings\Application Data\Microsoft\Outlook" "%drive%\Outlook"
rem backing up office outlook emails and contacts

if not exist "%drive%\Registry" mkdir "%drive%\Registry"
if exist "%drive%\Registry\regbackup.reg" del "%drive%\Registry\regbackup.reg"
regedit /e "%drive%\Registry\regbackup.reg"
rem backing up registry


echo *Incremental Backup Is Complete*
pause

goto begining

:DOIT3

cls
rem clear screen

rem opens the restoration menu

echo.
echo.
echo.
echo.
echo 1. Restore all your Data from a Backup
echo.
echo 2. Return to Main Menu
echo.
echo 3. Exit Backup Utility
echo.
echo.
echo.
rem backup menu

set Choice=
rem creates a variable choice

set /p choice= Input relevent number and press ENTER
rem /p is the switch to pause the display and sets the variable choice from user's input

if /i '%choice%'=='1' goto DOIT4

if /i '%choice%'=='2' goto Begining

if /i '%choice%'=='3' goto end



goto err
rem goes to err,when unlisted numbers/choices have been entered

goto end
rem when user decides to exit

:DOIT4
rem restoration of full backup

:DOIT5
rem restoration of incremental backup

:err
rem errors

cls
rem clear screen

echo.
echo.
echo.
echo.
echo.
echo. * * * * * * * * * * * * * * * * * * * *
echo. * * * * * * * * * * *
echo. * * * * * * * * * * * * * * * * * * *
echo. * * * * * * * * * * * *
echo. * * * * * * * * * * * * * * * * *
echo.
echo.
echo.
echo You have made an Invalid entry of '%choice%'
rem displays error message to user for incorect entry

pause
goto Begining
rem redirects to main menu

:err1
rem errors

cls
rem clear screen

echo.
echo.
echo.
echo.
echo.
echo. * * * * * * * * * * * * * * * * * * * *
echo. * * * * * * * * * * *
echo. * * * * * * * * * * * * * * * * * * *
echo. * * * * * * * * * * * *
echo. * * * * * * * * * * * * * * * * *
echo.
echo.
echo.
echo You have made an Invalid entry of '%choice%'
rem displays error message to user for incorect entry

pause

goto DOIT1
rem redirects to full backup

:err2
rem errors

cls
rem clear screen

echo.
echo.
echo.
echo.
echo.
echo. * * * * * * * * * * * * * * * * * * * *
echo. * * * * * * * * * * *
echo. * * * * * * * * * * * * * * * * * * *
echo. * * * * * * * * * * * *
echo. * * * * * * * * * * * * * * * * *
echo.
echo.
echo.
echo You have made an Invalid entry of '%choice%'
rem displays error message to user for incorect entry

pause

goto DOIT2
rem redirects to Incremental backup

:err3
rem errors

cls
rem clear screen

echo.
echo.
echo.
echo.
echo.
echo. * * * * * * * * * * * * * * * * * * * *
echo. * * * * * * * * * * *
echo. * * * * * * * * * * * * * * * * * * *
echo. * * * * * * * * * * * *
echo. * * * * * * * * * * * * * * * * *
echo.
echo.
echo.
echo You have made an Invalid entry of '%choice%'
rem displays error message to user for incorect entry

pause

goto DOIT
rem redirects to Incremental backup

:end
rem program will close
 
Wow, that is why I hate batch files, they are so convoluted...

Here is how much code I would have to right to do this in shell....

Code:
#!/bin/bash
selection=
until [ "$selection" = "0" ]; do
    echo ""
    echo "Select an option please"
    echo "1 - Do something"
    echo "2 - Display something"
    echo "3 - third option"
    echo "4 - Print something"
    echo "5 - List some stuff"
    echo "6 - Display system information"
    echo "0 - exit and log out"
    echo " "
    echo -n "Enter selection: "
    read selection
    echo ""
    case $selection in
    1)  some command ;;
    2)  some command ;;
    3)  some command ;;
    4)  some command ;;
    5)  some command ;;
    6)  some command ;;
    0)  exit and log out ;;
*) echo "Please select a valid option"

done

Now to answer your question....

What you will need to do is use a command to update the folder name to that day's date. This can be most easily accomplished with the move command. This is a command that can be invoked like this.

example:
Code:
set today=date

move c:\path\to\backup\%BackupPath% c:\path\to\backup\%today%

However, if the date is ever changing you may need to write some code to search the path first and pull the strings (ie, grep) of what the previous file was called. You can do that by using the findstr (find string) command.

example:
Code:
set BackupPath=findstr c:\path\to\parent\backup\folder

See how I referenced the original directory with this code, with the first example above? Since it is going to be ever changing you gotta basically pull out the string of what the current folder is called if you want to replace it.

To be honest though, best practices would be to keep daily back ups and then rotate them out say once a week, two weeks, or a month even. For example, I have a particular server at work that dumps the MySQL database every night to a bin file (MySQLs binary files) and every thirty days, it rotates out the back ups. So I can go back to any date exactly how it was with in 30 days.

I must admit, I haven't use batch files in years so I am not the best source. I also highly recommend you learn the power shell, as I think it is going to be Microsoft's answer to the lack of a scripting commands currently in windows. Also, PS comes standard in all Windows 7 installations I think.
 
Thanks mate, this has been doing my head in for hours,

I'll give it a go now,

I've got Win 7 Pro Ill check it out, doing my HND and all we use is the basics e.g. Win XP,

XP is okay but a little schooling on new methods and tech would be nice,

Got my MCP and MCDST last year for XP and now Win 7 is out, things have obviously changed since the birth of vista, but hey ho.

Thanks again!!!
 
Thanks mate, this has been doing my head in for hours,

I'll give it a go now,

I've got Win 7 Pro Ill check it out, doing my HND and all we use is the basics e.g. Win XP,

XP is okay but a little schooling on new methods and tech would be nice,

Got my MCP and MCDST last year for XP and now Win 7 is out, things have obviously changed since the birth of vista, but hey ho.

Thanks again!!!

It is basic programming. You input something to get desired output. If something is going to ever change you may want to hard code a variable to search for the desired string, if you are looking at a constant path, hardcode that path as a variable.

It just takes time. Took me years to know what I know now, and I am still learning every day.
 
Back
Top