Help with set /a

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
J'aitreschaud
Posts: 11
Joined: 01 Jul 2018 21:35

Help with set /a

#1 Post by J'aitreschaud » 03 Jul 2018 21:57

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 :twisted:

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Help with set /a

#2 Post by Squashman » 03 Jul 2018 22:22

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"
)

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Help with set /a

#3 Post by dbenham » 04 Jul 2018 08:03

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

J'aitreschaud
Posts: 11
Joined: 01 Jul 2018 21:35

Re: Help with set /a

#4 Post by J'aitreschaud » 04 Jul 2018 09:14

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?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Help with set /a

#5 Post by Squashman » 04 Jul 2018 09:56

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
)

J'aitreschaud
Posts: 11
Joined: 01 Jul 2018 21:35

Re: Help with set /a

#6 Post by J'aitreschaud » 04 Jul 2018 13:11

@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! :)

Post Reply