Instructables

Variables in a batch file?

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

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
NachoMahma5 years ago
. Try this:
...set /p letter=Type a letter between a and d, then press ENTER:if %letter%=a goto ltra:ltra...
Pawn_of_Prophecy (author)  NachoMahma5 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
. 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...
Pawn_of_Prophecy (author)  NachoMahma5 years ago
No, i've already tried this, it lets you enter the letter, you press enter and it still closes the box
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.
Wrong. You must put two = signs, not one.
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!!
Pawn_of_Prophecy (author)  dsman1952765 years ago
This is closer, but now it echos this is what happens when you type A regardless of what you type
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 ;-)
TheBatchies4 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
Arbitror5 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 "="!
Soo...
Did it work for you?
TATcreator5 years ago
In your script, you wrote
if %letter%=a
You should have wrote
if %letter%==a
with two = signs
But still, then any letter he entered would echo "This is the result of entering A". Read my above comment!
True, just giving compatibility/stability advice.
I realize that MS-DOS batch files aren't going away anytime soon. But you might consider a slight upgrade to Windows Scripting.
He doesn't need it...
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!