help with operators

  • Thread starter Thread starter winter
  • Start date Start date
W

winter

Hi All,

Can anyone point me to some good primers on the operators available
for set (/a) and if (/i)? I know /? gives me usage - but im must be
too think to get the full jist of it.....


thx
 
Hi All,

Can anyone point me to some good primers on the operators available
for set (/a) and if (/i)? I know /? gives me usage - but im must be
too think to get the full jist of it.....


thx


From the help file (tip 2815 in the 'Tips & Tricks' at http://www.jsiinc.com)

Using /a
The following table lists the operators supported for /a in descending order of
precedence.

Operator Operation performed
< > Grouping
* / % + - Arithmetic
<< >> Logical shift
& Bitwise AND
^ Bitwise exclusive OR
| Bitwise OR
= *= /= %= += -= &= ^= |= <<= >>= Assignment
, Expression separator

If you use logical (&& ||) or modulus (%) operators, enclose the expression
string in quotation marks. Any non-numeric strings in the expression are
considered environment variable names whose values are converted to numbers
before being processed. If you specify an environment variable name that is not
defined in the current environment, a value of zero is allotted, which allows
you to do arithmetic with environment variable values without using the % to
retrieve a value.

If you run set /a from the command line outside of a command script, it displays
the final value of the expression.

Numeric values are decimal numbers unless prefixed by 0× for hexadecimal numbers
or 0 for octal numbers. Therefore, 0×12 is the same as 18 is the same as 022.
The octal notation can be confusing. For example, 08 and 09 are not valid
numbers because 8 and 9 are not valid octal digits.

.........................................................................................


IfPerforms conditional processing in batch programs.

Syntax
if [not] errorlevel Number Command [else Expression]

if [not] String1==String2 Command [else Expression]

if [not] exist FileName Command [else Expression]

If command extensions are enabled, use the following syntax:

if [/i] String1 CompareOp String2 Command [else Expression]

if cmdextversion Number Command [else Expression]

if defined Variable Command [else Expression]

Parameters
not
Specifies that the command should be carried out only if the condition is false.
errorlevel Number
Specifies a true condition only if the previous program run by Cmd.exe returned
an exit code equal to or greater than Number.
Command
Specifies the command that should be carried out if the preceding condition is
met.
String1==String2
Specifies a true condition only if String1 and String2 are the same. These
values can be literal strings or batch variables (for example, %1). You do not
need to use quotation marks around literal strings.
exist FileName
Specifies a true condition if FileName exists.
CompareOp
Specifies a three-letter comparison operator. The following table lists valid
values for CompareOp. Operator Description
EQU equal to
NEQ not equal to
LSS less than
LEQ less than or equal to
GTR greater than
GEQ greater than or equal to

/i
Forces string comparisons to ignore case. You can use /i on the String1==String2
form of if. These comparisons are generic, in that if both String1 and String2
are both comprised of all numeric digits, the strings are converted to numbers
and a numeric comparison is performed.
cmdextversion Number
Specifies a true condition only if the internal version number associated with
the Command Extensions feature of Cmd.exe is equal to or greater than Number.
The first version is 1. It is incremented by one when significant enhancements
are added to the command extensions. The cmdextversion conditional is never true
when command extensions are disabled (by default, command extensions are
enabled).
defined Variable
Specifies a true condition if Variable is defined.
Expression
Specifies a command-line command and any parameters to be passed to the command
in an else clause.
/?
Displays help at the command prompt.
Remarks
If the condition specified in an if command is true, the command that follows
the condition is carried out. If the condition is false, the command in the if
clause is ignored and the command executes any command in the else clause (that
is, if you specify a command in the else clause).
When a program stops, it returns an exit code. To use exit codes as conditions,
use errorlevel.
Using defined Variable
If you use defined Variable, the following three variables are added:
%errorlevel%, %cmdcmdline%, and %cmdextversion%.

%errorlevel% expands into a string representation of the current value of
errorlevel, provided that there is not an existing environment variable with the
name ERRORLEVEL, in which case you get the ERRORLEVEL value instead. The
following example illustrates how you can use errorlevel after running a batch
program:

goto answer%errorlevel%
:answer0
echo Program had return code 0
:answer1
echo Program had return code 1
goto end
:end
echo done!

You can also use the CompareOp comparison operators as follows:

if %errorlevel% LEQ 1 goto okay
%cmdcmdline% expands into the original command line passed to Cmd.exe prior to
any processing by Cmd.exe, provided that there is not an existing environment
variable with the name CMDCMDLINE, in which case you get the CMDCMDLINE value
instead.

%cmdextversion% expands into the a string representation of the current value of
cmdextversion, provided that there is not an existing environment variable with
the name CMDEXTVERSION, in which case you get the CMDEXTVERSION value instead.

Using the else clause
You must use the else clause on the same line as the command after the if. For
example:

IF EXIST FileName. (
del FileName.
) ELSE (
echo FileName. missing.
)
The following code does not work because you must terminate the del command by a
new line:

IF EXIST FileName. del FileName. ELSE echo FileName. missing
The following code does not work because you must use the else clause on the
same line as the end of the if command:

IF EXIST FileName. del FileName.
ELSE echo FileName. missing
If you want to format it all on a single line, use the following form of the
original statement:

IF EXIST FileName. (del FileName.) ELSE echo FileName. missing
Examples
To display the message "Cannot find data file" if the file Product.dat cannot be
found, type:

if not exist product.dat echo Cannot find data file

If an error occurs during the formatting of the disk in drive A, the following
example displays an error message:

:begin
@echo off
format a: /s
if not errorlevel 1 goto end
echo An error occurred during formatting.
:end
echo End of batch program.

If no error occurs, the error message does not appear.

You cannot use the if command to test directly for a directory, but the null
(NUL) device does exist in every directory. As a result, you can test for the
null device to determine whether a directory exists. The following example tests
for the existence of a directory:

if exist c:mydir\nul goto process



Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
Can anyone point me to some good primers on the operators available
for set (/a) and if (/i)? I know /? gives me usage - but im must be
too think to get the full jist of it.....

There probably isn't any; it pretty much assumes that you are
a "programmer" (in some language).

So just try them:
set /a 4 * 5
20
set /a X=4 * 5
20
set x
20
set /a "X<<= 2"
80

(Use quotes when you have < or > or | in the expression)
Use parens if you need to affect the ORDER of evaulation.
Use "," if you want to set multiple variables (X=2, Y=4)

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
 
Back
Top