The following does NOT work:
if (condition1) || (condition2) (GOTO CHECKLABEL)
It yields:
"|| was not expected at this time"
Bernd
Untested:
@echo off
if /i "%env1%"=="a" if /i "%env2%"=="b" echo "%env1%%env2%"=="ab"
@echo off
set var=
set /p "var=enter either a,b,c,d: "
for %%a in (a b c d) do if "%%a"=="%var%" goto :abcd
goto :EOF
:abcd
echo %var%
@echo off
set var=
set /p "var=enter either a,b,c,d: "
echo abcd|find "%var%">nul && goto :abcd
goto :EOF
:abcd
echo %var%
AFAIK there's no such operator in MS supplied CLIs.
Workarounds:
OR:
if (condition1) GOTO CHECKLABEL
if NOT (condition2) GOTO elsewhere
:CHECKLABEL
AND:
if NOT (condition1) GOTO elsewhere
if NOT (condition2) GOTO elsewhere
:: Check Label
Also for AND:
if (condition1) if (condition2) GOTO CHECKLABEL
A real CLI like 4DOS/4NT does of course supply those operators:
if (condition1) .OR. (condition2) GOTO CHECKLABEL
4NT's conditional expressions are documented at
<http://jpsoft.com/help/conditionalexpresssions.htm>.
--
Michael Bednarek http://mbednarek.com/ "POST NO BILLS"
> On 23 Jul 2006 11:58:50 GMT, Bernd Oninger wrote in alt.msdos.batch.nt:
>
>>How do I implement boolean AND and OR operators in if commands in msdos batch files ?
>>
>>The following does NOT work:
>>
>>if (condition1) || (condition2) (GOTO CHECKLABEL)
>>
>>It yields:
>>
>>"|| was not expected at this time"
>
> AFAIK there's no such operator in MS supplied CLIs.
|| equates to "if errorlevel 1" under OT OSs and
&& equates to "if errorlevel 0"
AND:
if (condition1) IF (condition2) (GOTO CHECKLABEL)
OR:
if (condition1) (GOTO CHECKLABEL)
if (condition2) (GOTO CHECKLABEL)
See <http://gearbox.maem.umr.edu/batch/logic1.htm> for a discussion of
logic primitives in DOS. NT operating systems have more sophisticated
operators, including ||, but this is neither an NT nor batch group.
alt.msdos.batch and alt.msdos.batch.nt are the primary batch groups,
and are OS specific - the latter for NT/W2K/XP.
--
T.E.D. (tda...@gearbox.maem.umr.edu) Remove "gearbox.maem" to get real address - that one is dead
Or:
IF condition 1 GOTO CHECKLABEL
IF condition 2 GOTO CHECKLABEL
: Failed test code goes here
:CHECKLABEL
And:
IF NOT condition 1 GOTO FAILEDTEST
IF condition 2 GOTO CHECKLABEL
:FAILEDTEST
: Failed test code goes here
:CHECKLABEL
- Bill
"goto".
A batch file that determines if one or both of two files exists:
@echo off
if exist 1.txt goto found
if exist 2.txt goto found
echo did not find either 1.txt or 2.txt
goto exit
:found
echo 1.txt and 2.txt or both found!
:exit
A batch file that determines whether both of two files exists:
@echo off
if not exist 1.txt goto notfound
if not exist 2.txt goto notfound
echo 1.txt and 2.txt both found!
goto exit
:notfound
echo either 1.txt or 2.txt missing!
:exit
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
Since you set followup to alt.msdos.batch.nt, it appears you are asking for
CMD.EXE help, not MSDOS batch file help.
::AND
IF (condition1) IF (condition2) (goto both_true)
::OR
If (condition1) goto either
If (condition2) goto either
--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)
120} How do I use AND/OR/XOR/NOT operators in an IF statement?
167495 Jun 18 2006 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi
All the best, Timo
--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html
OR: (this is a bit harder)
If (condition1) Goto label
If (condition2) Goto label
Goto other
Alternative OR:
If Not (condition1 If Not (condition2) Goto other
:label
Hope this helps
Bruce
One way to create an AND in batch is ...
if (condition1) If (condition2) GOTO CHECKLABEL
likewise an OR would be ...
if (condition1) GOTO CHECKLABEL
If (condition2) GOTO CHECKLABEL
Tom Lavedas
==============
http://members.cox.net/tglbatch/wsh
Since this is cross posted to an NT group, the below info might
be of use. I've used it to do bit wise operations in batch files
on my xp machine. Try set /? for info.
Two new switches have been added to the SET command:
SET /A expression
SET /P variable=[promptString]
The /A switch specifies that the string to the right of the equal
sign is a numerical expression that is evaluated. The expression
evaluator is pretty simple and supports the following operations,
in decreasing order of precedence:
() - grouping
! ~ - - unary operators
* / % - arithmetic operators
+ - - arithmetic operators
<< >> - logical shift
& - bitwise and
^ - bitwise exclusive or
| - bitwise or
= *= /= %= += -= - assignment
&= ^= |= <<= >>=
, - expression separator
If you use any of the logical or modulus operators, you will need
to enclose the expression string in quotes. Any non-numeric
strings in the expression are treated as environment variable
names whose values are converted to numbers before using them.
If an environment variable name is specified but is not defined
in the current environment, then a value of zero is used. This
allows you to do arithmetic with environment variable values
without having to type all those % signs to get their values. If
SET /A is executed from the command line outside of a command
script, then it displays the final value of the expression. The
assignment operator requires an environment variable name to the
left of the assignment operator. Numeric values are decimal
numbers, unless prefixed by 0x for hexadecimal numbers, and 0 for
octal numbers.
So 0x12 is the same as 18 is the same as 022. Please note that
the octal notation can be confusing: 08 and 09 are not valid
numbers because 8 and 9 are not valid octal digits.
> It yields:
> Bernd
*** Those operators are not usable in all forms of DOS. One may get
around this with versions that don't support them by using an after-market
batch command enhancer. If this is not suitable, try:
IF CONDITION 1 GOTO CON-2
GOTO (whatever)
:CON-2
IF CONDITION 2 GOTO CHECKLABEL
Richard Bonner
http://www.chebucto.ca/~ak621/DOS/