First effort could use some tips.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
D3l1v3ryb01
Posts: 3
Joined: 03 Sep 2013 17:52

First effort could use some tips.

#1 Post by D3l1v3ryb01 » 03 Sep 2013 18:02

Hey folks.

Decided to try and learn how to make some batch files. The idea came when my laptop fried and I hadn't got anything backed up. So, after I got my replacement laptop, I decided I wanted to make my own little "program" to backup my files for me. Along with my familys.

Now, I know there's plenty of stuff out there to do this for me. Heck eve my WD Passport 1tb came with software to do it automatically for me. But I wanted something new, and didn't mind the challenge. Plus I enjoy learning.

Anyway, after a few hours of studying the internet and its resources, I managed to come up with this so far. Problem is, it doesn't work lol. Hopefully by showing you, you'll see what I'm attempting to do. If not, please feel free to ask questions. I could use any and all help =).

Code: Select all

@echo off
color a

:start
cls

echo                                ~BACKUP MENU~
echo                            ***********************
echo                            * 1. Backup Documents *   
echo                            * 2. Backup Pictures  *
echo                            * 3. Backup Videos    *
echo                            * 4. Backup Music     *
echo                            * 5. Backup All       *
echo                            ***********************
echo                               -By: D3l1v3ryb01-

echo                              "Enter your choice"

set /p choice=                       {1,2,3,4,5}

if /p '%choice%' ='1' goto :Backup Documents
if /p '%choice%' ='2' goto :Backup Pictures
if /p '%choice%' ='3' goto :Backup Videos 
if /p '%choice%' ='4' goto :Backup Music
if /p '%choice%' ='5' goto :Backup All

:Backup Documents
if successful md G:\Backup
%backupcmd% "%USERPROFILE%\documents" "%drive%\Backup\Documents"
goto start

:Backup Pictures
if successful md G:\Backup
%backupcmd% "%USERPROFILE%\pictures" "%drive%\Backup\Pictures"
goto start

:Backup Videos
if successful md G:\Backup
%backupcmd% "%USERPROFILE%\videos" "%drive%\Backup\Videos"
goto start

:Backup Music
if successful md G:\Backup
%backupcmd% "%USERPROFILE%\music" "%drive%\Backup\Music"
goto start
if successful md G:\Backup

:Backup All
%backupcmd% "%USERPROFILE%\pictures" "%drive%\Backup\Pictures"
%backupcmd% "%USERPROFILE%\documents" "%drive%\Backup\Documents"
%backupcmd% "%USERPROFILE%\videos" "%drive%\Backup\Videos"
%backupcmd% "%USERPROFILE%\music" "%drive%\Backup\Music"
goto start

:exit
exit


Now, Please. Keep in mind that I have absolutely NO knowledge of programming. None, nada, nothin. I don't know syntax, I don't know anything period. So I apologize for my faults and failures in this code. I'm doing the best I can to learn, and this is the product of about 5 hours of study, and I still don't get any of it, other than the fact that I can now make a bat file that will back up all of these at once, but I wanted to try and give myself a little more option by creating a menu that provided me the ability to choose individual selections, or all of them at once.

Really appreciate any tips and advice. Explanations as to how this all works and why, or even direction towards something I can study to figure this all out would be much appreciated =)

Thanks.
D3l1v3ryb01

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: First effort could use some tips.

#2 Post by foxidrive » 03 Sep 2013 19:20

Try this to get your input and branch afterward:

Make your labels a single word as below:

Code: Select all

set "choice="
set /p "choice=                       {1,2,3,4,5} "

if "%choice%"=="1"  goto :Backup_Documents
if "%choice%"=="2"  goto :Backup_Pictures
if "%choice%"=="3"  goto :Backup_Videos
if "%choice%"=="4"  goto :Backup_Music
if "%choice%"=="5"  goto :Backup_All
goto :start

D3l1v3ryb01
Posts: 3
Joined: 03 Sep 2013 17:52

Re: First effort could use some tips.

#3 Post by D3l1v3ryb01 » 03 Sep 2013 21:36

foxidrive wrote:Try this to get your input and branch afterward:

Make your labels a single word as below:

Code: Select all

set "choice="
set /p "choice=                       {1,2,3,4,5} "

if "%choice%"=="1"  goto :Backup_Documents
if "%choice%"=="2"  goto :Backup_Pictures
if "%choice%"=="3"  goto :Backup_Videos
if "%choice%"=="4"  goto :Backup_Music
if "%choice%"=="5"  goto :Backup_All
goto :start


Well, I see what you're saying. But unfortunately when I make a selection it says some error that I can't read due to the bat immediately closing.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: First effort could use some tips.

#4 Post by foxidrive » 04 Sep 2013 04:23

It works here:

Code: Select all

@echo off
:start
set "choice="
set /p "choice=                       {1,2,3,4,5} "

if "%choice%"=="1"  goto :Backup_Documents
if "%choice%"=="2"  goto :Backup_Pictures
if "%choice%"=="3"  goto :Backup_Videos
if "%choice%"=="4"  goto :Backup_Music
if "%choice%"=="5"  goto :Backup_All
goto :start

:Backup_Documents
echo :Backup_Documents
pause

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: First effort could use some tips.

#5 Post by penpen » 04 Sep 2013 06:36

I recommend you to use an additional menu item no 6 named "Exit" or "Quit".

And i think your program will don't backup anything, because the variables backupcmd and drive seem to be undefined,
if they weren't set outer the batch file.

You may set these variables using:

Code: Select all

set backupcmd=a program with some switches, for example xcopy
set drive=a volume descriptor for example C:
Just replace the exampe values with some you need.

But while programming you should simulate the commands by echoing the full string to execute.

penpen

Edit: Corrected "set drive=a drive letter" line.
Last edited by penpen on 04 Sep 2013 14:20, edited 1 time in total.

D3l1v3ryb01
Posts: 3
Joined: 03 Sep 2013 17:52

Re: First effort could use some tips.

#6 Post by D3l1v3ryb01 » 04 Sep 2013 09:19

I'm so hopelessly confused lol. I tried to take ya'lls advice, but I still can't get it right. Figured I'd give you the code that works, and then the code that isn't.

Working Code, automatically copies everything. No options.

Code: Select all

@echo off 
:: variables
color a
cls
md G:\Backup
/min
SET odrive=%odrive:~0,2%
set backupcmd=xcopy /s /c /d /e /h /i /r /y
echo off
%backupcmd% "%USERPROFILE%\pictures" "%drive%\Backup\Pictures"
%backupcmd% "%USERPROFILE%\documents" "%drive%\Backup\Documents"
%backupcmd% "%USERPROFILE%\videos" "%drive%\Backup\Videos"
%backupcmd% "%USERPROFILE%\music" "%drive%\Backup\Music"
@echo off
cls



Code that sucks and only gives me my menu.

Code: Select all

@echo off
color a

:start
set backupcmd=xcopy
set drive=g
cls

echo                                ~BACKUP MENU~
echo                            ***********************
echo                            * 1. Backup Documents *   
echo                            * 2. Backup Pictures  *
echo                            * 3. Backup Videos    *
echo                            * 4. Backup Music     *
echo                            * 5. Backup All       *
echo                            * 6. Exit             *
echo                            ***********************
echo                               -By: D3l1v3ryb01-

echo                              "Enter your choice"
set "choice="
set /p "choice=                       {1,2,3,4,5} "

if "%choice%"=="1"  goto :Backup_Documents
if "%choice%"=="2"  goto :Backup_Pictures
if "%choice%"=="3"  goto :Backup_Videos
if "%choice%"=="4"  goto :Backup_Music
if "%choice%"=="5"  goto :Backup_All
if "%choice%"=="6"  goto :Exit

@echo off
:start

if "%choice%"=="1"  goto :Backup_Documents
if "%choice%"=="2"  goto :Backup_Pictures
if "%choice%"=="3"  goto :Backup_Videos
if "%choice%"=="4"  goto :Backup_Music
if "%choice%"=="5"  goto :Backup_All
if "%choice%"=="6"  goto :Exit

:Backup_Documents
set drive = g:
if successful md G:\Backup
%backupcmd% "%USERPROFILE%\documents" "%drive%\Backup\Documents"
goto start
:Backup_Pictures
if successful md G:\Backup
%backupcmd% "%USERPROFILE%\pictures" "%drive%\Backup\Pictures"
goto start
:Backup_Videos
if successful md G:\Backup
%backupcmd% "%USERPROFILE%\videos" "%drive%\Backup\Videos"
goto start
:Backup_Music
if successful md G:\Backup
%backupcmd% "%USERPROFILE%\music" "%drive%\Backup\Music"
goto start
if successful md G:\Backup
:Backup_All
%backupcmd% "%USERPROFILE%\pictures" "%drive%\Backup\Pictures"
%backupcmd% "%USERPROFILE%\documents" "%drive%\Backup\Documents"
%backupcmd% "%USERPROFILE%\videos" "%drive%\Backup\Videos"
%backupcmd% "%USERPROFILE%\music" "%drive%\Backup\Music"
goto start
:exit
exit

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: First effort could use some tips.

#7 Post by penpen » 04 Sep 2013 14:18

I see a flaw in my post above, i will correct it, sorry for that:
I was seduced by the variable name "drive".

But you have to set a volume descriptor there, this is a volume name and and a colon, in your case this would be "G:".
And according to your functioning version the backupcmd should be "xcopy /s /c /d /e /h /i /r /y".

Additionally i didn't notice this on first read:

Code: Select all

if successful md G:\Backup
%backupcmd% (...)
Where (...) is a mask for the different parameters.

I assume you wanted to do this:

Code: Select all

md G:\Backup && %backupcmd% (...)

I recommend you to do it this way, and add some error message that you are able to read, so adding a pause after this message:

Code: Select all

if not exist G:\Backup md G:\Backup
if exist G:\Backup (
   (/%backupcmd% (...)) || (
      echo backup not successfull message
      pause
   )
) else (
   echo md G:\Backup not successfull message
   pause
)

penpen

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: First effort could use some tips.

#8 Post by foxidrive » 04 Sep 2013 20:26

Your old code had some issues too.

Here is an edited version of your new code with menu - it is untested:


Code: Select all

@echo off
color a

:start
set backupcmd=xcopy /s /h /e /k /f /c
set drive=g:
cls

echo                                ~BACKUP MENU~
echo                            ***********************
echo                            * 1. Backup Documents *   
echo                            * 2. Backup Pictures  *
echo                            * 3. Backup Videos    *
echo                            * 4. Backup Music     *
echo                            * 5. Backup All       *
echo                            * 6. Exit             *
echo                            ***********************
echo                               -By: D3l1v3ryb01-

echo                              "Enter your choice"
set "choice="
set /p "choice=                       {1,2,3,4,5} "

if "%choice%"=="1"  goto :Backup_Documents
if "%choice%"=="2"  goto :Backup_Pictures
if "%choice%"=="3"  goto :Backup_Videos
if "%choice%"=="4"  goto :Backup_Music
if "%choice%"=="5"  goto :Backup_All
if "%choice%"=="6"  goto :Done
goto :start

:Backup_Documents
%backupcmd% "%USERPROFILE%\documents\*.*" "%drive%\Backup\Documents\"
goto :start

:Backup_Pictures
%backupcmd% "%USERPROFILE%\pictures\*.*" "%drive%\Backup\Pictures\"
goto :start

:Backup_Videos
%backupcmd% "%USERPROFILE%\videos\*.*" "%drive%\Backup\Videos\"
goto :start

:Backup_Music
%backupcmd% "%USERPROFILE%\music\*.*" "%drive%\Backup\Music\"
goto :start

:Backup_All
%backupcmd% "%USERPROFILE%\pictures\*.*" "%drive%\Backup\Pictures\"
%backupcmd% "%USERPROFILE%\documents\*.*" "%drive%\Backup\Documents\"
%backupcmd% "%USERPROFILE%\videos\*.*" "%drive%\Backup\Videos\"
%backupcmd% "%USERPROFILE%\music\*.*" "%drive%\Backup\Music\"
goto :start

:done
exit

Jess Selien
Posts: 13
Joined: 26 Sep 2012 12:08

Re: First effort could use some tips.

#9 Post by Jess Selien » 05 Sep 2013 18:54

There is a nice command called choice.
choice /? for details, it uses error codes from user input to pick from your menu.

Start small with a test script with two options.
pick it apart.
if "%choice%"=="1" goto :Backup_Documents

I am lazy to lookup but, maybe dos is saying 1 is a numeric value and not a string. You could try if %choice% equ 1 (goto Backup_Documents)
:Backup_Documents
echo.test good.

Also it is good to validate the variable exists...
IF not defined choice (goto error)
:error
echo.test bad.

Post Reply