Page 1 of 1
Help with set /a
Posted: 03 Jul 2018 21:57
by J'aitreschaud
I have another question. Hopefully this gets answered...
I have this:
Code: Select all
FOR /L %%G IN (1,1,6) DO (
SET /A rand%%G=%RANDOM% * (5 - 1 + 1) / 32768 + 1
)
But for the life of me, I can't figure out why it doesn't work. It displays the error message :"/ was unexpected at the time". I really see no error however. Any help would be lovely

Re: Help with set /a
Posted: 03 Jul 2018 22:22
by Squashman
If you happened to read the help file for the SET command you may have noticed this comment.
If you use any of the logical or modulus operators, you will need to enclose the expression string in quotes.
Code: Select all
setlocal enabledelayedexpansion
FOR /L %%G IN (1,1,6) DO (
SET /A "rand%%G=!RANDOM! * (5 - 1 + 1) / 32768 + 1"
)
Re: Help with set /a
Posted: 04 Jul 2018 08:03
by dbenham
Of course the help is incomplete, misleading, and in some ways simply wrong. The OP did not use modulus or logical operators, so it wouldn't be obvious that quotes are needed. Beyond that, quotes wouldn't help with modulus, which must be escaped in a batch file as %%, with or without quotes.
And negation must be escaped as ^! if the expression is quoted and delayed expansion is enabled. Without the quotes it would be escaped as ^^!.
And quotes are never actually needed - an alternative for most problem characters is to escape the character with a preceding caret (^).
The OPs issue is the ) which prematurely closes the FOR loop unless it is quoted or escaped.
Here is a complete list of SET /A operators that require special handling:
Code: Select all
Operator Name Required Special Handling
-------- ------------ --------------------------------------------------
% modulus Must be escaped as %% within a batch file
! not Only if delayed expansion is enabled, then must be
escaped as ^^! without quotes, or ^! within quotes
& and Must be quoted or escaped as ^&
| or Must be quoted or escaped as ^|
^ exclusive or Must be quoted or escaped as ^^
<< left shift Must be quoted or escaped as ^<^<
>> right shift Must be quoted or escaped as ^>^>
) close group Only if appears within a parenthesized block of
code, as often occurs with FOR loops and IF
statements, then must be quoted or escaped as ^)
Dave Benham
Re: Help with set /a
Posted: 04 Jul 2018 09:14
by J'aitreschaud
Thanks for the reply dbenham! However, it still auto-closes with the same message:"/ was unexpected at the time"
I've tried putting a caret before the ), but it doesn't work.
Code: Select all
FOR /L %%G IN (1,1,6) DO (
SET /A rand%%G=%RANDOM% * (5 - 1 + 1^) / 32768 + 1
^)
Neither does this
Code: Select all
FOR /L %%G IN (1,1,6) DO (
SET /A rand%%G=%RANDOM% * (5 - 1 + 1) / 32768 + 1
^)
Any thoughts?
Re: Help with set /a
Posted: 04 Jul 2018 09:56
by Squashman
The code I gave you worked just fine when I tested it. I also fixed the problem with your results not actually being random! Not sure why you haven't tried to use it.
Regardless of that you literally took Dave's advice and attempted to do it both ways you could possibly do it wrong. You need to escape the parentheses that is
CAUSING the FOR command to be closed.
Code: Select all
FOR /L %%G IN (1,1,6) DO (
SET /A rand%%G=%RANDOM% * (5 - 1 + 1^) / 32768 + 1
)
Re: Help with set /a
Posted: 04 Jul 2018 13:11
by J'aitreschaud
@Squashman Sorry for ignoring your answer, its just that the time I saw your answer was on mobile; while when I saw Dave's answer I was on my PC, so I looked at his advice. Thank you a lot for posting a reply, it works!
