[Question] Can I use "And" whene comparing strings with "If"

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

[Question] Can I use "And" whene comparing strings with "If"

#1 Post by Ranguna173 » 20 Sep 2011 11:59

Hello, again!

I was thinking if it was possible to use an "and" in "if" like:

Code: Select all

if %string1% and %string2%==%string3% (goto ok) else (goto nop)

:ok
cls
echo String 1 and 2 are the same as string 3.
pause
exit

:nop
cls
echo String 1 and 2 aren't the same as string 3.
pause
exit


I usually I do it like this:

Code: Select all

If %string1%==%string3% (goto check2) else (goto nop)

:check2
if %string2%==%string3% (goto ok) else (goto nop)

:ok
cls
echo String 1 and 2 are the same as string 3.
pause
exit

:nop
cls
echo String 1 and 2 aren't the same as string 3.
pause
exit


But it would be easyer if there was an "and"
(is it easyer or easy-er?)

It could be an "and" or a comma or something.

Code: Select all

if %String1%,%string2%==%string3%


But it doesn't work..

Please help me here.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: [Question] Can I use "And" whene comparing strings with

#2 Post by !k » 20 Sep 2011 12:07

AND it's easy

Code: Select all

if 1==1 if 2==2 if 3==3 echo true

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: [Question] Can I use "And" whene comparing strings with

#3 Post by trebor68 » 20 Sep 2011 14:44

Here a complete code in one row.

Code: Select all

if "%string1%"=="%string3%" ( if "%string2%"=="%string3%" (goto :ok) else (goto :nop1) ) else (goto :nop2)


To understand:

Code: Select all

if string1==string3 ( if string2==string3 (TRUE_2) else (FALSE_2) ) else (FALSE_1)

The TRUE_1 is the complete second if command.

Ranguna173
Posts: 104
Joined: 28 Jul 2011 17:32

Re: [Question] Can I use "And" whene comparing strings with

#4 Post by Ranguna173 » 21 Sep 2011 10:03

trebor68 wrote:Here a complete code in one row.

Code: Select all

if "%string1%"=="%string3%" ( if "%string2%"=="%string3%" (goto :ok) else (goto :nop1) ) else (goto :nop2)


To understand:

Code: Select all

if string1==string3 ( if string2==string3 (TRUE_2) else (FALSE_2) ) else (FALSE_1)

The TRUE_1 is the complete second if command.

So it's like:

Code: Select all

if %string1%==%string3% (
                         if %string2%==%string3% (
                                                  goto ok
                                                  ) else (
                                                          goto nop1
                                                          )
                         ) else (
                                 goto nop2
                                )

In short:

Code: Select all

if "%string1%"=="%string3%" ( if "%string2%"=="%string3%" (goto :ok) else (goto :nop1) ) else (goto :nop2)


Thank you trebor68




!k wrote:AND it's easy

Oh! Thought it was "easyer" lol
!k wrote:

Code: Select all

if 1==1 if 2==2 if 3==3 echo true

So that's how it goes!
Thanks !k

Post Reply