Random number/file display

Unitech Batch no laser blue tooth enable...
March 3, 2010 at 13:27:51
Specs: Windows XP
Hey guys.

I'm trying to get a random Thought of the Day type deal to pop into the stat box that my users see when they log in. I've got 80 "Thoughts" that were provided to me to get started. I'd rather not put them into separate .txt files if I can get away with it but I will if I have to.

Also I'm rather new to programming in general and Batch in specific so if a solution is long and rambling can I get a bit of explanation?

Thanks.


See More: Random number/file display

Report •


#1
March 3, 2010 at 13:34:39
"so if a solution is long and rambling can I get a bit of explanation?"

maybe

I don't know what a stat box is but I can probably slog through the BAT script.

Is your file one "thought" per line? Or what?


=====================================
Helping others achieve escape felicity

M2


Report •

#2
March 3, 2010 at 13:38:13
Sorry. The "stat box" is just what my boss calls the cmd display that pops up when using the startup script he's already wrote that shows our users how long they've been logged on etc.

The text file is one "Thought" per line with eighty lines.

Edit: And I'll take that maybe. I've been told by my boss to learn it myself and that he won't be giving me help.


Report •

#3
March 3, 2010 at 13:53:51
Short script, short explanation. It generates a number between 1 and 80 and grabs that line.

-----------------------------------
@echo off & setLocal EnableDELAYedeXpansion

set /a N=!random!%%80+1
set T=

for /f "tokens=* delims= " %%a in (myfile) do (
set /a T+=1
if !T! equ !N! echo.%%a
)


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
March 3, 2010 at 14:04:49
That goes so far over my head it might as well have wings, heh.

Now I have the file in c:\thoughts\thoughts.txt how do I slide that in? I tried putting it into (myfile) but it doesn't seem to want to work.


Report •

#5
March 3, 2010 at 14:14:28
Where I have:

(myfile)

you need:

(c:\thoughts\thoughts.tx)


=====================================
Helping others achieve escape felicity

M2


Report •

#6
March 3, 2010 at 14:19:09
Ah! Awesome! You sir are a gentleman and a scholar and I salute and thank you.

If it's possible can I get a Why it works? If not I'll just slowly figure it out on my own by breaking down the little bits and tossing them into google with "what is" posted in front of it.


Report •

#7
March 3, 2010 at 14:28:29
set /a N=!random!%%80+1

Does an 'arithmetic set. NT/XP has a built in RANDOM.

The %%80 means to do a modulo 80, which without the +1 would yeild 0 to 79.
------------------------
set /a T+=1

Tracks line numbers in the file.

-------------------------
if !T! equ !N! echo.%%a

Puts out the line num corresponding to the num generated on this run.


=====================================
Helping others achieve escape felicity

M2


Report •

#8
March 3, 2010 at 14:30:05
Thanks again brother.

Report •

Ask Question