Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Batch arithmetic with the Win95cmd Batch enhancer

6 views
Skip to first unread message

William Allen

unread,
Dec 14, 2001, 2:38:44 PM12/14/01
to
For Windows 95/98/ME users, the Microsoft Win95cmd.exe Batch
enhancer can be used to perform Batch arithmetic in a Win95/98/ME
Batch file. Win95cmd enables the SET /A arithmetic switch to be used.

This article documents the details of syntax to use the arithmetic
operations from a normal Batch file running in Windows 95/98/ME.

This is an extract from the SET /? brief help from Win95cmd:

=====Extract
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:

* / % - arithmetic operators
+ - - arithmetic operators
<< >> - logical shift
& - bitwise and
^ - bitwise exclusive or
| - bitwise or
= *= /= %= += -= - assignment
&= ^= |= <<= >>=
=====Extract ends

This demo shows how to do Batch arithmetic in a normal Batch file
running under COMMAND.COM by calling a Subroutine within a
Win95cmd child shell to do the arithmetic. The demo stores the results
in RESULTS.BAT in the child shell, then recovers them by CALLing the
RESULTS.BAT file in the main routine. Some details required to get the
syntax working are _not_ obvious, and are annotated in detail in the demo.

Particularly note:
(a) the need to insert [Space]s before some redirection commands to
ensure correct redirection into RESULTS.BAT in the child shell.
(b) Some expressions need to be enclosed in "quotes" to avoid
special interpretation of arithmetic operators.

====Begin cut-and-paste (omit this line)
@ECHO OFF
IF (GOTO:)==(%1) %1%2 (Subroutine handler)

:: Execute Win95cmd Subroutine to do the arithmetic
win95cmd /c %0 GOTO: _MATHS

FOR %%C IN (CALL DEL) DO %%C RESULTS.BAT
ECHO. Product 123 x 456 = %PRODUCT%
ECHO. Integer quotient 365/7 = %WEEKS%
ECHO. Remainder on division 365 %% 7 = %REMBY%
ECHO. Sum/difference 123+456-200 = %ADDSUB%
ECHO. Shift 1024 by 2 bits right = %SHIFTR2%
ECHO. Shift 1024 by 2 bits left = %SHIFTL2%
ECHO. BitwiseAND 4 AND 5 = %BITAND%
ECHO. BitwiseOR 3 OR 2 = %BITOR%
ECHO. BitwiseExclusiveOR 3 XOR 2 = %BITEXOR%
ECHO. Sum of results = %SUM%

FOR %%V IN (PRODUCT WEEKS REMBY ADDSUB SHIFTR2 SHIFTL2 BITAND BITOR BITEXOR SUM) DO SET %%V=

GOTO EOF (=Subroutine code follows=)
:_MATHS

:: RESULTS.BAT is a file to pass results back to main code
:: Variables or literals may be used in expressions
:: SUM is simply a demo sum variable to show += assignment
SET SUM=0

:: Example of product 123 x 456
SET Y=456
SET /a PRODUCT=123*%Y%
ECHO.SET PRODUCT=%PRODUCT%>RESULTS.BAT

:: Accumulate sum of results
SET /a SUM+=%PRODUCT%

:: Example of (integer quotient) division 365 / 7
SET X=365
SET /a WEEKS=%X%/7
ECHO.SET WEEKS=%WEEKS%>>RESULTS.BAT

:: Accumulate sum of results
SET /a SUM+=%WEEKS%

:: Example of remainder on division by 365 % 7
:: Note need to double the % sign to avoid parametric interpretation
:: Note that the [Space] before the >> in ECHO. line is needed
SET /a REMBY=365%%7
ECHO.SET REMBY=%REMBY% >>RESULTS.BAT

:: Accumulate sum of results
SET /a SUM+=%REMBY%

:: Example of addition and subtraction 123+456-200
SET X=123
SET Y=456
SET /a ADDSUB=%X%+%Y%-200
ECHO.SET ADDSUB=%ADDSUB%>>RESULTS.BAT

:: Accumulate sum of results
SET /a SUM+=%ADDSUB%

:: Example of bitwise logical shift right 1024>>2
:: (=shift by 2 binary places to right, 1024 becomes 256)
:: For this operator, enclose expression in "quotes" to avoid
:: interpretation of >> as output redirection
SET /a "SHIFTR2=1024>>2"
ECHO.SET SHIFTR2=%SHIFTR2%>>RESULTS.BAT

:: Accumulate sum of results
SET /a SUM+=%SHIFTR2%

:: Example of bitwise logical shift left 1024<<2
:: (=shift by 2 binary places to left, 1024 becomes 4096)
:: For this operator, enclose expression in "quotes" to avoid
:: interpretation of << as input redirection
SET X=1024
SET /a "SHIFTL2=%X%<<2"
ECHO.SET SHIFTL2=%SHIFTL2%>>RESULTS.BAT

:: Accumulate sum of results
SET /a SUM+=%SHIFTL2%

:: Example of BitwiseAND
:: Note that the [Space] before the >> in ECHO. line is needed
:: For this operator, enclose expression in "quotes" to avoid
:: special interpretation of & character
SET /a "BITAND=4&5"
ECHO.SET BITAND=%BITAND% >>RESULTS.BAT

:: Accumulate sum of results
SET /a SUM+=%BITAND%

:: Example of BitwiseOR
:: Note that the [Space] before the >> in ECHO. line is needed
:: For this operator, enclose expression in "quotes" to avoid
:: interpretation of | as pipe character
SET /a "BITOR=3|2"
ECHO.SET BITOR=%BITOR% >>RESULTS.BAT

:: Accumulate sum of results
SET /a SUM+=%BITOR%

:: Example of BitwiseExclusiveOR
:: Note that the [Space] before the >> in ECHO. line is needed
:: For this operator, enclose expression in "quotes" to avoid
:: special interpretation of ^ character
SET /a "BITEXOR=3^2"
ECHO.SET BITEXOR=%BITEXOR% >>RESULTS.BAT

:: Accumulate sum of results and pass back SUM in RESULTS.BAT
SET /a SUM+=%BITEXOR%
ECHO.SET SUM=%SUM%>>RESULTS.BAT

:EOF (End-of-file)

====End cut-and-paste (omit this line)
To use the code above, cut-and-paste the text between the ==== lines
into a file with extension .BAT and base name of your choice. Lines
that don't begin with two spaces have wrapped in transmission. The
Batch code above was written and tested in the Win9x GUI with
Win95cmd.exe in the system path.

--
(pp) William Allen

Win95cmd.exe is a free, downloadable Microsoft Batch enhancer that
can be used in Windows 95/98/ME to add some (but not all) of the
enhanced batch language from Windows 2000. For further details
(including instructions to make a custom Win95cmd DVM) see:
http://groups.google.com/groups?selm=9u5ame$69j47$1...@ID-55970.news.dfncis.de

Download Win95cmd.exe from:
Win95cmd.exe file is available as a single small ZIP from:
http://www.neuro.gatech.edu/users/cwilson/cygutils/unversioned/consize/


Charles Dye

unread,
Dec 14, 2001, 5:14:33 PM12/14/01
to
"William Allen" <NGr...@mayfly13.fsnet.co.uk> wrote in message news:<9vdkh4$efe02$1...@ID-55970.news.dfncis.de>...

> For Windows 95/98/ME users, the Microsoft Win95cmd.exe Batch
> enhancer can be used to perform Batch arithmetic in a Win95/98/ME
> Batch file. Win95cmd enables the SET /A arithmetic switch to be used.
>
> This article documents the details of syntax to use the arithmetic
> operations from a normal Batch file running in Windows 95/98/ME.

A few oddball facts about SET /A:

* Alphabetic environment variable names don't need to be
enclosed in percent signs; the following two commands
have the same effect:

SET /A AREA= %WIDTH% * %HEIGHT%
SET /A AREA= WIDTH * HEIGHT

* The result is not only stashed in the target environment
variable, it is also written to stdout. This might provide
a less clumsy way of getting the resulting values back to
the parent shell...?

* 4DOS has completely undocumented, and somewhat imperfect,
support for SET /A. Variable names must have at least the
leading percent sign, and I think some operators are not
implemented. (In most cases, the 4DOS @EVAL function will
be more useful.) 4NT and Take Command/32, both of which
will run under Windows 95/98/Me, have more complete and
documented support for SET /A.

--
Charles Dye ras...@highfiber.com

William Allen

unread,
Dec 20, 2001, 1:52:46 AM12/20/01
to
"Charles Dye" wrote in message
> "William Allen" wrote in message

>
> > For Windows 95/98/ME users, the Microsoft Win95cmd.exe Batch
> > enhancer can be used to perform Batch arithmetic in a Win95/98/ME
> > Batch file. Win95cmd enables the SET /A arithmetic switch to be used.
> >
> > This article documents the details of syntax to use the arithmetic
> > operations from a normal Batch file running in Windows 95/98/ME.
>
> A few oddball facts about SET /A:
...snip

> * The result is not only stashed in the target environment
> variable, it is also written to stdout. This might provide
> a less clumsy way of getting the resulting values back to
> the parent shell...?

Although Win95cmd ECHOes the value to STDOUT when
run from the command line, the ECHOing is suppressed when
the SET /A command is run from a Batch file. Extract from
SET /? documentation in Win95cmd:
"If SET /A is executed from the command line outside of a
command script, then it displays the final value of the expression."

All the necessary arithmetic stages can be carried out in the child
shell. Then only the required final result need be captured to a
temporary script file for return to the parent shell.

Further points on SET /A:
Handles hexadecimal numbers and arithmetic, translating
them to decimal values in the process:
set /a result=0xff
places 255 in RESULT and:
set /a result=0xd*0xd
places 169 in RESULT.

This can be useful in association with Debug, which operates
with hexadecimal values.

--
(pp) William Allen


Charles Dye

unread,
Dec 20, 2001, 1:53:15 PM12/20/01
to
On Thu, 20 Dec 2001 06:52:46 -0000, "William Allen" <NGr...@mayfly13.fsnet.co.uk> wrote:

>"Charles Dye" wrote in message
>>

>> A few oddball facts about SET /A:
>...snip
>> * The result is not only stashed in the target environment
>> variable, it is also written to stdout. This might provide
>> a less clumsy way of getting the resulting values back to
>> the parent shell...?
>
>Although Win95cmd ECHOes the value to STDOUT when
>run from the command line, the ECHOing is suppressed when
>the SET /A command is run from a Batch file. Extract from
>SET /? documentation in Win95cmd:
>"If SET /A is executed from the command line outside of a
>command script, then it displays the final value of the expression."

Running a batch file in the ancillary shell is the sort of
complication that I want to avoid....

win95cmd /c set /a foo="6 * 9" | nset result=$0
rem * Horst Schaeffer's NSET

win95cmd /c set /a foo="128 + 37" | stow result > nul
rem * John Stockton's STOW

win95cmd /c set /a foo="1 << 13" | sed "s/^/set result=/" > $set.bat
call $set.bat
rem * any appropriate port of sed

win95cmd /c set /a foo="1000 - 217" | input %%result
rem * any INPUT which accepts text from stdin; syntax will vary

set number=16
set weight=3
set container=2
win95cmd /c set /a foo=" number * weight " , " foo + container " | nset result=$0
echo 16 three-pound blivets in a two-pound box: %result% pounds
rem * multiple calculations in one call; why the extra spaces
rem * are required is a mystery to me


I'll admit that in cases involving many or very complex
calculations with lots of intermediate steps, a batch file in
the ancillary shell may indeed be the best way to go. I'd expect
such cases to be rare, though.

Since the ancillary shell's environment is lost when it exits,
the actual variable name used in the SET /A matters very little.
The SET /A doesn't affect the calling shell's environment; PATH
would work just as well as FOO in the above examples.

Regarding your note that some operators require the expression to
be enclosed in double quotes, caret and ampersand may also have
special meaning to the calling shell. Probably the best advice
would be to always use double quotes around the SET /A expression.
Sometimes you must space before and after the quotes -- the Reason
for this Remains Obscure.

Win95Cmd appears to support only integer math; 4DOS and 4NT permit
floating-point calculations.

Apologies for the neologisms "calling shell" and "ancillary shell";
I thought some new terminology was needed, and "primary / secondary
shell" are already taken.

--
Charles Dye ras...@highfiber.com

0 new messages