232Views18Replies

Author Options:

Variables in a batch file? Answered

Im working with Vista SP1 and experimenting with batch files, i came up with a problem so i'd see what you thought. This is the code i was using:

:top
@echo off
color 0a
Title Experiment by Me
echo -------------------------------------
echo Type a letter between a and d.
echo -------------------------------------
set /p letter=
if %letter%=a
goto a

:a
echo This is the result of entering A
pause>nul

What happens is that the box closes after you enter your letter and press enter, i dont have any more results (i.e. where it says :a) because i found it didnt work

18 Replies

user
Prof. Pickle (author)2012-08-09

I improved your code:

:top
cls
@echo off
color 0a
Title Experiment by Me
echo -------------------------------------
echo Type a letter between a and d.
echo -------------------------------------
set /p letter=
If %letter%==a goto a
If %letter%==b goto b
If %letter%==c goto c
If %letter%==d goto d
goto please
:a
cls
echo This is the result of entering A
pause>nul
goto top
:please
cls
color 04
echo Oooops, you typed a key that was not an option.
echo.
echo Press any key to go back to the top
pause > nul
goto top

Select as Best AnswerUndo Best Answer

user
NachoMahma (author)2009-03-28
. Try this:
...set /p letter=Type a letter between a and d, then press ENTER:if %letter%=a goto ltra:ltra...

Select as Best AnswerUndo Best Answer

user

Nope, this doesnt work, it just puts "Type a letter between a and d, then press ENTER:" below the line i made but still closes the box when you press your letter and press enter. thanks for trying though

Select as Best AnswerUndo Best Answer

user
. Try stopping after the if - you may not be getting what you think you are.
...if %letter%=a goto ltraecho You typed %letter%pause:ltra...

Select as Best AnswerUndo Best Answer

user

No, i've already tried this, it lets you enter the letter, you press enter and it still closes the box

Select as Best AnswerUndo Best Answer

user

try this:

echo type a letter between A and D
set /p letter=
if %letter%==A then (
echo this is what happens when you type A
)

you need parenthesis on your if statement. without them it stops at that if line, and does not execute the goto line.

Select as Best AnswerUndo Best Answer

user
TATcreator (author)dsman1952762009-04-25

Wrong. You must put two = signs, not one.

Select as Best AnswerUndo Best Answer

user
TheBatchies (author)TATcreator2010-01-23

1. Uhhhh... No, I think. Not when you're setting the variable with the /p command, but I could be wrong.
2. JUST USE STINKING EQU!!

Select as Best AnswerUndo Best Answer

user

This is closer, but now it echos this is what happens when you type A regardless of what you type

Select as Best AnswerUndo Best Answer

user

echo type a letter between A and D
set /p letter=
if %letter%==A (
echo this is what happens when you type A
)

the "then" was not needed ;-)

Select as Best AnswerUndo Best Answer

user
TheBatchies (author)2010-01-23

@echo off
:top
color 0a
Title Experiment by Me
echo -------------------------------------
echo Type a letter between a and d.
echo -------------------------------------
choice /n /c:abcd
if errorlevel d goto d
if errorlevel c goto c
if errorlevel b goto b
if errorlevel a goto a
:a
echo This is the result of entering A
pause>nul
:b
echo This is the result of entering B
pause>nul
:c
echo This is the result of entering C
pause>nul
:d
echo This is the result of entering D
pause>nul

Select as Best AnswerUndo Best Answer

user
Arbitror (author)2009-07-04

:top
@echo off
color 0a
Title Experiment by Me
echo -------------------------------------
echo Type a letter between a and d.
echo -------------------------------------
set /p letter=
if %letter%==a goto a
goto end

:a
echo This is the result of entering A
pause>nul
:end

This should solve your problem. =) Notice what lines I changed. Make sure not to miss the double "="!

Select as Best AnswerUndo Best Answer

user
Arbitror (author)Arbitror2009-10-12

Soo...
Did it work for you?

Select as Best AnswerUndo Best Answer

user
TATcreator (author)2009-04-25
In your script, you wrote
if %letter%=a
You should have wrote
if %letter%==a
with two = signs

Select as Best AnswerUndo Best Answer

user
Arbitror (author)TATcreator2009-07-04

But still, then any letter he entered would echo "This is the result of entering A". Read my above comment!

Select as Best AnswerUndo Best Answer

user
TATcreator (author)Arbitror2009-07-07

True, just giving compatibility/stability advice.

Select as Best AnswerUndo Best Answer

user
NobodyInParticular (author)2009-03-28

I realize that MS-DOS batch files aren't going away anytime soon. But you might consider a slight upgrade to Windows Scripting.

Select as Best AnswerUndo Best Answer