Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Bash / Shell Scripting (http://www.programmingforums.org/forum26.html)
-   -   RND batch file example (http://www.programmingforums.org/showthread.php?t=14413)

cueballr Nov 11th, 2007 12:49 AM

RND batch file example
 
Hello, Could some one please help me out and post an example Randomize function in batch? One that produces random text or numbers like the RND lib function in VB.

Thanks in advance.

cueballr. =]

lectricpharaoh Nov 11th, 2007 5:46 AM

Re: RND batch file example
 
Why not just write a small program, in whatever language, that generates random numbers and/or text, and then call it from the batch file? You also never said how you want the numbers/text. Are you wanting them to be displayed on the screen? This is quite simple. Do you want to use the output of the program as input into another program? This is also petty simple, as the program itself will be the same, but you will use pipes in the batch file. Do you want something else entirely? Your post was pretty vague, so you'll either need to give more information, or ask a psychic person for assistance.

cueballr Nov 11th, 2007 8:59 AM

Re: RND batch file example
 
If i would have wanted to call a VB RND program i would have done so, but i want an example in batch code, and no i dont want to use the out put as the input of another program, i simply want to use the function to name temp files, or anything else for that matter.Assistance would be apreciated.

thanks in advance.

cueballr. =]

DaWei Nov 11th, 2007 9:15 AM

Re: RND batch file example
 
Whether or not your particular shell/batch facility has a randomize feature depends on your system and any utilities that have been added. Generally, you will find you need to invoke another program (and use redirection, temp files, etc.), whether it was written by you or someone else.

The response you got was perfectly sensible. Perhaps you'd prefer Google?

cueballr Nov 11th, 2007 9:35 AM

Re: RND batch file example
 
I googled it before i made the first post and found an example, but it was not the type i needed, also it flashed an error while running the script, i just wanted another one.:)

If someone could point out the error in that script it would be appreciated.

:

@ECHO OFF
FOR %%v IN (1 2 3 4 5 6 7 8 9) DO SET R%%v=%%v
%1 %0 :: 1 %R1% 2 %R2% 3 %R3% 4 %R4% 5 %R5% 6 %R6% 7 %R7% 8 %R8% 9 %R9%
ECHO. | TIME > RAND.DAT
:Loop1
FIND ".%2" RAND.DAT >NUL
IF ERRORLEVEL 1 FOR %%v IN (SET SHIFT GOTO:Loop1) DO %%v R%2=%3
DEL RAND.DAT
SET RANDOM=%3
SET TANDOM=%7
ECHO %RANDOM%%TANDOM% >rresult.txt
GOTO END
:END


the error flashed while running the above is

"Invalid Parameter to SHIFT command"

and FYI it does write the file "rresult.txt", but its the same data written into the file
everytime, and if im not mistaken thats not what it should be doing.

any help would be nice. =]

thanks.

cueballr. =]

DaWei Nov 11th, 2007 10:43 AM

Re: RND batch file example
 
This doesn't make any sense:
FOR %%v IN (SET SHIFT GOTO:Loop1) DO %%v R%2=%3
That's going to try to generate some like:
:

SET R1=1
SHIFT R1=1
GOTO:Loop1 R1=1

Presumably, the GOTO will short-circuit and survive that extra parameter. SHIFT apparently won't. SHIFT, if command extensions are on, will accept a parameter in the form of "/n", where n is the parameter number to begin the shift from. R1=1 won't qualify.

cueballr Nov 11th, 2007 10:55 AM

Re: RND batch file example
 
Could you suggest a fix to that code?, is that possibly why it always writes "13" to the "rresult.txt" file?:-/

DaWei Nov 11th, 2007 11:24 AM

Re: RND batch file example
 
It turns out that command extensions are on by default, so the SHIFT with a parameter will definitely fail.

Explain what you're trying to accomplish, and what parameters you will be passing and what they're for.

Your original post asks for a random number and doesn't explain your goal. Your script seems to be wanting to use TIME for random generation. Given the output of TIME, it seems like a relatively poor choice for anything but the ugliest generation of a variable file name.

lectricpharaoh Nov 11th, 2007 6:28 PM

Re: RND batch file example
 
Quote:

Originally Posted by cueballr
i simply want to use the function to name temp files, or anything else for that matter.Assistance would be apreciated.

Are you meaning you want to generate unique names for files, so you can create a file that is guaranteed not to exist, or are you meaning something else? If so, there are functions to accomplish this in various languages, such as tmpnam() in C/C++.


All times are GMT -5. The time now is 9:08 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC