Need help with coding a BAT file.

  OCRIDION 00:11 21 Dec 2009
Locked
Answered

I kind of at a learner level in BAT Files, No matter what I try my output is "Saidno", seems to be im not comparing the strings properly. Different sites say different things.

This is my code so far:

@echo off

ECHO.
ECHO Welcome to the Batch program produced by Andrew James for turning Test Mode ON and OFF, This program was written for Vista 64bit.
ECHO.
pause

CLS

ECHO.
SET /p TESTMODE = Do you want to want to turn test mode ON? (Type Y / N)
CLS

If (%TESTMODE%==y) (goto :saidyes) Else (goto :saidno)

:saidyes
ECHO.
echo "Said yes"
goto :end
:saidno
ECHO.
echo "Said no"
:end
ECHO.
pause

  OCRIDION 00:41 21 Dec 2009

Nope, tried it all on that page.

  PalaeoBill 09:34 21 Dec 2009
Answer

Two problems.
First, you have a space between TESTMODE and the equals operator, so the environment variable you are setting up is "TESTMODE " and not "TESTMODE", plus it should really be in quote marks for the comparison.

Second, the ELSE clause in DOS is very strange and very particular about where the brackets go:

IF <statement> (
......
) ELSE (
.......
)

So if you change your script to this, it will work:

@echo off

ECHO.
ECHO Welcome to the Batch program produced by Andrew James for turning Test Mode ON and OFF, This program was written for Vista 64bit.
ECHO.
pause

CLS

ECHO.
SET /p TESTMODE= Do you want to want to turn test mode ON? (Type Y / N)
CLS

If "%TESTMODE%" == "y" (
goto saidyes
) Else (
goto saidno
)

:saidyes
ECHO.
echo "Said yes"
goto :end
:saidno
ECHO.
echo "Said no"
:end
ECHO.

  PalaeoBill 09:35 21 Dec 2009

Forgot to say, you don't need the colon in front of the saidyes and saidno in the GOTO statements. Its only necessary for the label itself.

This thread is now locked and can not be replied to.

Elsewhere on IDG sites

How secure is your Wi-Fi?

AMD vs Intel: we put the most powerful desktops for designers and artists head-to-head

iPhone tips & tricks

Les meilleurs ordinateurs portables 2017