goto and if

M

Mr. Novice

When I run the script listed below I get "goto was
unexpected at this time".

echo The question.
echo 1. option 1
echo 2. option 2
set /p var=pick your option

if "%var%"=="1" set var2=string1&goto label
if "%var%"=="2" set var2=string2&goto label


I searched a google newsgroup and saw that Phil had
suggested changing things up a bit so I tried things
like...

if [%var%]==[1] set var2=string1&goto label

if {%var%}=={1} set var2=string1&goto label

if [%var%]==[1] set var2=string1&goto :label

if "%var%"=="1" (
set var2=string1
goto label
)

I even tried taking out the set command...

if "%var%"=="1" goto label


but haven't had any luck. Any suggestions?

Chad
 
D

Dean Wells [MVP]

Below is a simple extension of your syntax that functions perfectly -

:: [BEGIN SCRIPT]
@echo off
echo The question.
echo 1. option 1
echo 2. option 2
set /p var=pick your option

if "%var%"=="1" set var2=string1&goto label1
if "%var%"=="2" set var2=string2&goto label2

:label1
echo label1
goto :END

:label2
echo label2
goto :END

:END
:: [END SCRIPT]

HTH

Dean
 
M

Mr. Novice

Yes, that's what was driving me so crazy. Your example and
mine were pretty much the same and should have worked.
After making the post I found that it was actually
following the goto command but didn't like the fist
statement under that label and for some reason it was
saying that it didn't like the goto command. When I took
out the first line under the goto label it worked fine. I
haven't figured out why it did't like that first statement
yet. =( Working on that now. =)

Thanks Dean!

Chad

-----Original Message-----
Below is a simple extension of your syntax that functions perfectly -

:: [BEGIN SCRIPT]
@echo off
echo The question.
echo 1. option 1
echo 2. option 2
set /p var=pick your option

if "%var%"=="1" set var2=string1&goto label1
if "%var%"=="2" set var2=string2&goto label2

:label1
echo label1
goto :END

:label2
echo label2
goto :END

:END
:: [END SCRIPT]

HTH

Dean

--
Dean Wells [MVP / Windows platform]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


Mr. Novice said:
When I run the script listed below I get "goto was
unexpected at this time".

echo The question.
echo 1. option 1
echo 2. option 2
set /p var=pick your option

if "%var%"=="1" set var2=string1&goto label
if "%var%"=="2" set var2=string2&goto label


I searched a google newsgroup and saw that Phil had
suggested changing things up a bit so I tried things
like...

if [%var%]==[1] set var2=string1&goto label

if {%var%}=={1} set var2=string1&goto label

if [%var%]==[1] set var2=string1&goto :label

if "%var%"=="1" (
set var2=string1
goto label
)

I even tried taking out the set command...

if "%var%"=="1" goto label


but haven't had any luck. Any suggestions?

Chad


.
 

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