GOTO not working as it should

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mail.sidney
Posts: 3
Joined: 28 Mar 2009 11:01

GOTO not working as it should

#1 Post by mail.sidney » 28 Mar 2009 11:09

Hi,

Have created a script to backup files based on user input. Pretty simple but i am having a problem getting the GOTO functions working..namely the ones below

GOTO SHOWDOCS
GOTO SHOWARCH

The script directly takes me back to the menu once the prompt checks if i want to see the backed up files. Need help desparately. Bear in mind, I am a newbie to DOS programming.

SCRIPT BELOW:

@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
Echo .

Echo Ensure that your drive has been connected and is visible under Windows Explorer.

Echo *****************************************************************************
Echo * *
Echo * Only Proceed if drive is the USB drive is visible *
Echo * *
Echo *****************************************************************************
Echo .
Echo .


FOR /F "tokens=1,2,3 Delims=/" %%A in ('Date /T') do (set foldername=%%A-%%B-%%C)
set foldername=%foldername: =%
Echo "%foldername%"
pause

:MENU
Echo What do you want to backup.
Echo Select your task.
Echo .

cls
Echo [1] Backup My Documents.
Echo [2] Backup Archives.
Echo [3] Backup My Documents and Archives.
Echo [4] Exit.

set /P M=Select your option [1,2,3,4]:
IF %M%==1 GOTO MYDOCS
IF %M%==2 GOTO MYARCH
IF %M%==3 GOTO BOTH
IF %M%==4 GOTO EOF


:MYDOCS
SET /P DRVLTR=ENTER THE DRIVE LETTER FOR THE USB DRIVE [Example D]:

Mkdir %drvltr%:\backup\%username%\%foldername%\My_Docs
xcopy "%userprofile%\My documents\*.*" "%drvltr%:\backup\%username%\%foldername%\My_Docs\" /Y /G /H /O /E
Echo .
Echo .
ECHO COMPLETE.........................................
ECHO .
ECHO .
set /P G=Do you wish to see the backed up files[Y/N]:
IF %G%==Y GOTO SHOWDOCS
IF %G%==N GOTO EOF
GOTO MENU



:MYARCH
SET /P DRVLTR=ENTER THE DRIVE LETTER FOR THE USB DRIVE [Example D]:

Mkdir %drvltr%:\backup\%username%\%foldername%\Archives
xcopy "%userprofile%\Local settings\Application Data\Microsoft\Outlook\*.pst" "%drvltr%:\backup\%username%\%foldername%\Archives\" /Y /G /H /O /E
Echo .
Echo .
ECHO COMPLETE.........................................
Echo .
Echo .

set /P F=Do you wish to see the backed up files[Y/N]:

IF %F%=="Y" GOTO SHOWARCH
IF %F%=="N" GOTO EOF
Goto MENU

:BOTH
SET /P DRVLTR=ENTER THE DRIVE LETTER FOR THE USB DRIVE [Example D]:

Mkdir %drvltr%:\backup\%username%\%foldername%\My_Docs
Mkdir %drvltr%:\backup\%username%\%foldername%\Archives

xcopy "%userprofile%\My documents\*.*" "%drvltr%:\backup\%username%\%foldername%\My_Docs\" /Y /G /H /O /E
xcopy "%userprofile%\Local settings\Application Data\Microsoft\Outlook\*.pst" "%drvltr%:\backup\%username%\%foldername%\Archives\" /Y /G /H /O /E
GOTO MENU


:SHOWARCH
explorer "%drvltr%:\backup\%username%\%foldername%\Archives\"
GOTO Menu

:SHOWDOCS
explorer "%drvltr%:\backup\%username%\%foldername%\My_Docs\"
goto MENU

:EOF
cls

JustJoe
Posts: 6
Joined: 17 Mar 2009 12:54

#2 Post by JustJoe » 28 Mar 2009 13:23

Greetings:

You forgot the colon in your goto statment

Example:

currently "GOTO SHOWDOCS"

should be "GOTO :SHOWDOCS"

another problem is with your conditionals

Example:

currently "IF %F%=="Y" GOTO SHOWARCH"

should be "IF "!F!"=="Y" GOTO :SHOWARCH"

cheers

mail.sidney
Posts: 3
Joined: 28 Mar 2009 11:01

tried the modified code

#3 Post by mail.sidney » 28 Mar 2009 16:57

Hi i modified the code as below...

set /P F=Do you wish to see the backed up files[Y/N]:

IF "!F!"=="Y" GOTO :SHOWARCH
IF "!F!"=="N" GOTO :EOF
Goto MENU

Still doesnt work. It jumps directly to the Goto Menu and takes me back to the menu. I am surprised why the
GOTO MENU statement works without the colon.
Appreciate your help..need more :)

mail.sidney
Posts: 3
Joined: 28 Mar 2009 11:01

Finally fixed it

#4 Post by mail.sidney » 28 Mar 2009 18:32

I fixed it. The reason for the script not working as it should was because the value of the variable was case sensitive. had to use a bit of string manipulation.

after accepting the value from the user. had to convert it to upper case as i was comparing it to a upper case Y. Made the following modification and it works like a charm :)

set /P F=Do you wish to see the backed up files[Y/N]:
set F=%F:y=Y%
set F=%F:n=N%
IF %F%==Y GOTO SHOWBOTH
IF %F%==N GOTO EOF
GOTO MENU

RElliott63
Expert
Posts: 80
Joined: 04 Feb 2009 10:03

#5 Post by RElliott63 » 29 Mar 2009 20:53

Just as an FYI...

Since you're enabling the extensions, you could change your IF statement to be

Code: Select all

IF /I "!F!" equ "Y" goto SHOWBOTH


That causes the compare to be case insensitive. Then, you wouldn't have the additional lines to force everything to upper case.

-R

Post Reply