Home All Groups Group Topic Archive Search About


Author
10 Oct 2007 12:30 PM
bvb47 via WinServerKB.com
Hello!,

I'm trying to obtain a random number in batch file.

if I type
> set /a X=%random%%108
in command prompt work ok, but if I put this instruction in batch file, don't
work!

anybody know why?

thanks!

--
Message posted via http://www.winserverkb.com

Author
10 Oct 2007 4:22 PM
McKirahan
"bvb47 via WinServerKB.com" <u37442@uwe> wrote in message
news:797b38778d19c@uwe...
> Hello!,
>
> I'm trying to obtain a random number in batch file.
>
> if I type
> > set /a X=%random%%108
> in command prompt work ok, but if I put this instruction in batch file,
don't
> work!
>
> anybody know why?

Try doubling the use of %:

http://www.aumha.org/a/batches.php

"NOTE: The %% is necessary because this is in
a batch file which, otherwise, would give a special
meaning to a single %. "
Author
10 Oct 2007 4:24 PM
Richard Mueller [MVP]
"bvb47 via WinServerKB.com" <u37442@uwe> wrote in message
news:797b38778d19c@uwe...
Show quote
> Hello!,
>
> I'm trying to obtain a random number in batch file.
>
> if I type
>> set /a X=%random%%108
> in command prompt work ok, but if I put this instruction in batch file,
> don't
> work!
>
> anybody know why?
>
> thanks!
>
> --
> Message posted via http://www.winserverkb.com
>

At a command prompt I get:

nnnnn%108

where nnnnn is a random integer. In a batch file I get:

nnnnnn08

This is because %1 is blank. If I pass "Test" to the batch file as the first
argument, I get an error because "Test" is not numeric. If I pass 23 as the
argument, I get:

nnnnnn2308

Seems to work for me. Of course the environment variable only lasts until I
exit the command prompt.

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--
Author
10 Oct 2007 5:07 PM
bvb47 via WinServerKB.com
the solution is written by BSOD in nabble forum:
----------------
BSOD wrote:
Cos in a batch %108 is interpreted as a command line parameter passed to the .
bat, the same as %0 and %1 etc etc.

use this :

SET /A X=%RANDOM%%%108%
---------------

thanks to all!.

bvb47 wrote:
>Hello!,
>
>I'm trying to obtain a random number in batch file.
>
>if I type
>> set /a X=%random%%108
>in command prompt work ok, but if I put this instruction in batch file, don't
>work!
>
>anybody know why?
>
>thanks!

--
Message posted via http://www.winserverkb.com
Author
10 Oct 2007 5:08 PM
bvb47 via WinServerKB.com
the solution is written by BSOD in nabble forum:
----------------
BSOD wrote:
Cos in a batch %108 is interpreted as a command line parameter passed to the .
bat, the same as %0 and %1 etc etc.

use this :

SET /A X=%RANDOM%%%108%
---------------

thanks to all!.

bvb47 wrote:
>Hello!,
>
>I'm trying to obtain a random number in batch file.
>
>if I type
>> set /a X=%random%%108
>in command prompt work ok, but if I put this instruction in batch file, don't
>work!
>
>anybody know why?
>
>thanks!

--
Message posted via http://www.winserverkb.com

AddThis Social Bookmark Button