244Views18Replies

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

Discussions

0
None
Prof. Pickle

6 years ago

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

0
None
NachoMahma

9 years ago

. Try this:
...set /p letter=Type a letter between a and d, then press ENTER:if %letter%=a goto ltra:ltra...
0
None
Pawn_of_ProphecyNachoMahma

Answer 9 years ago

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

0
None
NachoMahmaPawn_of_Prophecy

Answer 9 years ago

. 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...
0
None
Pawn_of_ProphecyNachoMahma

Answer 9 years ago

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

0
None
dsman195276Pawn_of_Prophecy

Answer 9 years ago

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.

0
None
TheBatchiesTATcreator

Answer 8 years ago

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!!

0
None
Pawn_of_Prophecydsman195276

Answer 9 years ago

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

0
None
dsman195276Pawn_of_Prophecy

Answer 9 years ago

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 ;-)

0
None
TheBatchies

8 years ago

@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

0
None
Arbitror

9 years ago

: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 "="!

0
None
TATcreator

9 years ago

In your script, you wrote
if %letter%=a
You should have wrote
if %letter%==a
with two = signs
0
None
ArbitrorTATcreator

Answer 9 years ago

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

0
None
TATcreatorArbitror

Answer 9 years ago

True, just giving compatibility/stability advice.