if true echo something else

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
batchcc
Posts: 139
Joined: 17 Aug 2015 06:05
Contact:

if true echo something else

#1 Post by batchcc » 03 Dec 2015 10:36

I have the following code can anyone tell me why it doesnt work and how to fix it.
I want the code to allow the user to enter a word (this) and if the enter it the file prints this and the environmental variable %ar%.
if the user types "mode1" then the file should set %ar% as the word hello and type mode 1 is true.

Code: Select all

@echo off
cls
set ar=ar
:top
set /p word=
if %word% == this echo this ----- %ar% && if %mode1% == true echo mode 1 is true
if %word% == mode1 set mode1=true
if %mode1% == true goto m1
goto top
:m1
set ar= hello
goto top
pause

Thanks

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: if true echo something else

#2 Post by Squashman » 03 Dec 2015 11:17

The variable %mode1% is not defined yet so the IF statement becomes invalid syntax. A better syntax to use for IF comparisons is to use double quotes and remove the spaces between the comparisons.

batchcc
Posts: 139
Joined: 17 Aug 2015 06:05
Contact:

Re: if true echo something else

#3 Post by batchcc » 03 Dec 2015 11:49

Like this?

Code: Select all

@echo off
cls
Set mode1=false
set ar=ar
:top
set /p word=
if %word% == this echo this ----- %ar% && if %mode1% == true echo mode 1 is true
if %word% == mode1 set mode1=true
if %mode1% == true goto m1
goto top
:m1
set ar= hello
goto top
pause

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: if true echo something else

#4 Post by ShadowThief » 03 Dec 2015 20:23

I think that's the same code...

batchcc
Posts: 139
Joined: 17 Aug 2015 06:05
Contact:

Re: if true echo something else

#5 Post by batchcc » 04 Dec 2015 10:48

no I set mode equal to false.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: if true echo something else

#6 Post by ShadowThief » 04 Dec 2015 19:04

Ah, sneaky. I was expecting you to use double quotes because that's what the last thing that was suggested was.

Post Reply