Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 11th, 2007, 12:49 AM   #1
cueballr
Newbie
 
Join Date: Oct 2007
Posts: 16
Rep Power: 0 cueballr is on a distinguished road
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. =]
cueballr is offline   Reply With Quote
Old Nov 11th, 2007, 5:46 AM   #2
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 864
Rep Power: 3 lectricpharaoh is on a distinguished road
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.
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp
lectricpharaoh is offline   Reply With Quote
Old Nov 11th, 2007, 8:59 AM   #3
cueballr
Newbie
 
Join Date: Oct 2007
Posts: 16
Rep Power: 0 cueballr is on a distinguished road
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. =]
cueballr is offline   Reply With Quote
Old Nov 11th, 2007, 9:15 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 9 DaWei is on a distinguished road
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?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 11th, 2007, 9:35 AM   #5
cueballr
Newbie
 
Join Date: Oct 2007
Posts: 16
Rep Power: 0 cueballr is on a distinguished road
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. =]
cueballr is offline   Reply With Quote
Old Nov 11th, 2007, 10:43 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 9 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 11th, 2007, 10:55 AM   #7
cueballr
Newbie
 
Join Date: Oct 2007
Posts: 16
Rep Power: 0 cueballr is on a distinguished road
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?
cueballr is offline   Reply With Quote
Old Nov 11th, 2007, 11:24 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 9 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 11th, 2007, 6:28 PM   #9
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 864
Rep Power: 3 lectricpharaoh is on a distinguished road
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++.
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp
lectricpharaoh is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Batch File - Running a exe and closing the winodw mark84 Bash / Shell Scripting 2 Dec 20th, 2006 12:22 AM
Windows batch file script chronosoft Other Scripting Languages 6 Oct 10th, 2006 9:43 AM
Embedding a batch file into a C++ app vynkz C++ 22 May 18th, 2005 8:59 PM
Batch file to expand and minimize start menu Josef_Stalin Other Programming Languages 10 May 11th, 2005 2:26 PM
Update registry key value via batch file dpasswat Other Scripting Languages 1 Mar 17th, 2005 10:45 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:48 AM.

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