1.

Solve : Random words in batch??

Answer»

Hey ppl,
I have a little question, is it possible to create a BATCH file in witch the program chooses a random word from a list of words that you write in the txt file?
And what about doing the same but with words and numbers all together?
I hope u get my POINT, and yeah, im a noob, so i beg your patience
Thx!Ok. This is what you can use.

@echo off
set /a rnd=%random%%%10
for /f "tokens=1,2" %%a in (list.txt) do if %rnd%==%%a echo %%b
pause
That will get a random number from 1-10 and display a corresponding word.Im sorry but could u please explain it? For example, what i am supposed to write in list.txt? And how does it work?
Sorry but im completely lost here
Thanks for answering anyway.Quote from: Batch on FEBRUARY 07, 2010, 04:28:33 PM

Im sorry but could u please explain it? For example, what i am supposed to write in list.txt? And how does it work?
Sorry but im completely lost here
Thanks for answering anyway.
So, I will start off explaining what NEEDS to be in list.txt. This is what you could put in list.txt

Quote
1 Word1
2 Word2
3 Word3
4 Anotherword
5 ...
6 get_the_picture?

Now, I will re-post the script, with comments explaining how the code works.

@echo off
rem Turns echoing commands on-screen to off.
set /a rnd=%random%%%10
rem Gets a random number from 1-10.
for /f "tokens=1,2" %%a in (list.txt) do if %rnd%==%%a echo %%b
rem Taking the first two tokens of each line in list.txt delimited by spaces,
rem it will check if the first word is the same as the random number, and if it is,
rem it will display the SECOND word.
rem
pauseOh, ok I get it now.
Thanks a lot, problem solved!



Discussion

No Comment Found