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

Numeric range in cmd

54 views
Skip to first unread message

teebe

unread,
Jun 12, 2011, 1:48:47 PM6/12/11
to
Hi,

suppose in <for /f %%g in (file) do (set /a MyVar=%%G)> the <file>
contains non text values, what could be the result in MyVar? In other
words, is <for in do> handling the complete 256 range or it is limited
to ascii text characters?

If, as I suspect this command is meant for text evaluation only, how
to handle numeric values in cmd?

T

billious

unread,
Jun 12, 2011, 2:58:16 PM6/12/11
to

First little point : %%g and %%G are different variables in a FOR loop. It's
one of the few places where batch is case-sensitive.

SET /A sets a variable to the arithmetic result of expression, and a for /f
will present each line in the file in turn to the SET /A

If the line contains a pure numeric string, MyVar will be set to its value -
UNLESS the string starts '0' when MyVar will be set to the value interpreted
as OCTAL or '0x' where the value is interpreted as HEX.

If the line contains an EXPRESSION like '3*41' or '12+6' then myvar will be
set the the result of evaluating that expression, ie 123 or 18 for the
examples.

The limits are those of a signed 32-bit number.

If the line contains any invalid characters, a message will be displayed,
ERRORLEVEL set non-zero and myvar will be unchanged.

SET /A does not convert characters as you appear to believe. If you have
VAR1 and VAR2 set to '7' and '51' respectively (without the quotes) then

SET /A MyVar=%var1% * %var2%

will set Myvar to '357'

You should note that ALL environment variables are STRINGS; SET /A merely
instructs CMD to interpret the strings as integers and perform BODMAS
INTEGER operations on any expressions it finds - producing a numeric string
(if all goes without error)


teebe

unread,
Jun 12, 2011, 3:11:22 PM6/12/11
to

Thanks for explaining the rule of set /a, actually what I need is to
handle a file without strings (of chr), like binary executable. From
your reply it looks this is not possible in nt cmd, at least not the
way I suppose. Any further suggestion?
Cheers.

foxidrive

unread,
Jun 12, 2011, 4:58:36 PM6/12/11
to
On 13/06/2011 05:11, teebe wrote:
> Thanks for explaining the rule of set /a, actually what I need is to
> handle a file without strings (of chr), like binary executable. From
> your reply it looks this is not possible in nt cmd, at least not the
> way I suppose. Any further suggestion?

What do you need to do with the binary file?


--
Regards,
Mic

0 new messages