Solved What am I doing wrong with my batch IF/THEN?

October 27, 2012 at 15:38:28
Specs: Windows 7
Hello, I'm making a coding language for one of my buddies as a joke. It'll probably never get finished though, because I suck at batch :( Can somebody help me?

Here is my code thus far (or at least the part that is troubling me...):

:proj
::START MAIN
echo [
::line1
set /p line1= }}
(
ECHO %line1%
) >title.txt
::line2
::add all basic combos!
set /p line2= 	}}
If %line2%=="black on white" (
ECHO color f0
) >colorings.txt
If %line2%=="white on black" (
ECHO color 0f
) >colorings.txt
If %line2%=="black on blue" (
ECHO color 10
) >colorings.txt
If %line2%=="blue on black" (
ECHO color 01
) >colorings.txt
If %line2%=="green on black" (
ECHO color 0a
) >colorings.txt
If %line2%=="black on green" (
ECHO color a0
) >colorings.txt
::line3
set /p line2= 	}}
if %line2%= close
	echo ]
goto end

:end
pause>nul

When I enter an answer (e.g.

black on green
or
black on white
) for line two, it tells me 'on' was not expected and closes. Any help?

See More: What am I doing wrong with my batch IF/THEN?

Report •


✔ Best Answer
October 27, 2012 at 18:12:05
you either need to add, or remove () from the arrangement. Each set of () is treated as a "block" of code. for example:
If %line2%==green on black (
ECHO color 0a
) >colorings.txt

here, you have put a block on the IF statement, not on the ECHO statement (although the echo statement doesn't need () since it is single-line output.), which
renders the output to colorings.txt "orphaned", in limbo so to speak.
You want to either double the sets of (), or eliminate all of them, for each "if".
I recommend removing all the extraneous ():
If %line2%==green on black ECHO color 0a >colorings.txt

but if you want all the extra garbage:
If %line2%==green on black (
(ECHO color 0a) >colorings.txt)

which i consider ugly code, hard to read and absorb quickly.

You realize you will have to do about 240 IF's to cover all the color combos? There
are easy ways to avoid that, by exploiting some of batch strengths. You would need 16 variable assignments, instead of 240, and no IF's. Even the 16 variable assignments could be automated from output of "color /?".
(You will need "enabledelayedexpansion" enabled.)
Here's a hint:

for /f "skip=10 tokens=1-5 delims== " %%a in ('color /?') do if /i %%a equ if (goto :next) else (echo set %%b=%%a& echo set %%d-%%e=%%c)

(I put dash instead of space for "light "whatever.)
But I don't want to spoil the chase, Lol!...




#1
October 27, 2012 at 16:31:15
Any help?
When you're confused by your batch's behaviour, leave ECHO on and run the script from the Command Prompt.
on was unexpected at this time.

h:\>If black on green=="black on white" (

How To Ask Questions The Smart Way


Report •

#2
October 27, 2012 at 16:31:30
message edited reason: no useful spend effords with this guy

Report •

#3
October 27, 2012 at 16:43:28
Now when I run it it just lists out the colors 'color 1a', 'color 2a', etc and does not create colors.txt

Report •

Related Solutions

#4
October 27, 2012 at 17:35:32
PLEASE HELP!

Report •

#5
October 27, 2012 at 17:51:15
Yep, RogerSmith's version is broken, too. So you're going to have to learn to fish.

How To Ask Questions The Smart Way


Report •

#6
October 27, 2012 at 18:12:05
✔ Best Answer
you either need to add, or remove () from the arrangement. Each set of () is treated as a "block" of code. for example:
If %line2%==green on black (
ECHO color 0a
) >colorings.txt

here, you have put a block on the IF statement, not on the ECHO statement (although the echo statement doesn't need () since it is single-line output.), which
renders the output to colorings.txt "orphaned", in limbo so to speak.
You want to either double the sets of (), or eliminate all of them, for each "if".
I recommend removing all the extraneous ():
If %line2%==green on black ECHO color 0a >colorings.txt

but if you want all the extra garbage:
If %line2%==green on black (
(ECHO color 0a) >colorings.txt)

which i consider ugly code, hard to read and absorb quickly.

You realize you will have to do about 240 IF's to cover all the color combos? There
are easy ways to avoid that, by exploiting some of batch strengths. You would need 16 variable assignments, instead of 240, and no IF's. Even the 16 variable assignments could be automated from output of "color /?".
(You will need "enabledelayedexpansion" enabled.)
Here's a hint:

for /f "skip=10 tokens=1-5 delims== " %%a in ('color /?') do if /i %%a equ if (goto :next) else (echo set %%b=%%a& echo set %%d-%%e=%%c)

(I put dash instead of space for "light "whatever.)
But I don't want to spoil the chase, Lol!...



Report •

#7
October 28, 2012 at 02:08:55
message edited reason: no useful spend effords with this guy

Report •


Ask Question