Solved

Help with random text in a batch logon scritp

Posted on 2004-09-17
12
701 Views
Last Modified: 2012-05-05
Im just gonna give 500pts because every time I ask a question it tends to be much harder to do then what I though it would.

BASICLY I want a .bat file

In it I wnat to have say 3 lines of text

Text 1
Text 2
Text 3

The long.bat will call this RANDOM TEXT.bat file.

I want it to AT RANDOM pick 1 line of text and ECHO it and then END that bat file.. I don't want all the Text files listed each log on. just 1 from the list at random.

OK... any body have any thing like this?
0
Comment
Question by:mrchaos101
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 1

Author Comment

by:mrchaos101
ID: 12088449
Possable VB SCRIPT Item? or can ti be done in batch?
0
 
LVL 22

Expert Comment

by:cookre
ID: 12088887
Will the number of text lines vary?
If so, is there a maximum number of lines it will have?
0
 
LVL 22

Expert Comment

by:cookre
ID: 12088895
Oops, and/or minimum number of lines?
0
Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

 
LVL 1

Author Comment

by:mrchaos101
ID: 12088960
well no it will be a static number but i think I can change say if I want to add 1 more line of text so there wre 4 I could probly figure out how to edit the bat to make it  work.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12089275
Here is a vb script (.vbs file) that will read a text file and display a random line from it in a msgbox:

Dim i, entireFile, lines
Dim fso, f, fsoStream

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile("c:\test.txt")
Set fsoStream = f.OpenAsTextStream(1)
entireFile = fsoStream.ReadAll
fsoStream.Close
Set fsoStream = Nothing
Set fso = Nothing

lines = Split(entireFile, vbCrLf)
Randomize
i = Int((ubound(lines) + 1) * Rnd)
MsgBox lines(i)
0
 
LVL 22

Expert Comment

by:cookre
ID: 12089346
Change "c:\lines.txt" to wherever the file is that has the lines to be selected from.

(uncommented just to maintain the mystique)

@echo off
for /f %%a in ('time /t') do set zD1=%%a
set zD2=%zD1:~4,1%
if not defined zNUM1 set /a zNUM1=%zD2%
set /a zNUM1=zNUM1 * 214013 + 2531011
set /a zNUM2=zNUM1 ^>^> 16 ^& 0x7fff
set zIDX=%zNUM2:~0,1%
set /a zIDX=zIDX/3
set /a zIIDX=0
set zLO=
set zLL=""
for /f %%a in (c:\lines.txt) do call :zoo  %zIDX%  %%a
if {%zLO%}=={} set zLO=%zLL%
goto awdun
:zoo
set zLL=%2
if {%1}=={%zIIDX%} set zLO=%2
::echo =%1=%2=%zIIDX%=
set /a zIIDX=%zIIDX% + 1
goto :eof
:awdun
echo %zLO%
0
 
LVL 2

Expert Comment

by:mishagale
ID: 12090179
Cookre: Coding a random number generator in batch commands? I wouldn't have thought it possible, but kudos for suceeding. The "mystique" is well preserved, by your use of wonderfully incomprehensible variable names, but this does make debugging rather tricky; when I run your batch file, on windows 2000, it only outputs the first word of each line in the file. A kludge to fix this is to change the second for loop from:

for /f %%a in (c:\lines.txt) do call :zoo  %zIDX%  %%a

to:

for /f "tokens=*" %%a in (c:\lines.txt) do call :zoo  %zIDX%  "%%a"

This does have to side effect of enclosing your output in double-quotes, but perhaps cookre can offer a more elegant solution - this kind of batch programming is black magic as far as I'm concerned.
0
 
LVL 22

Expert Comment

by:cookre
ID: 12090380
That's what I get for testing agains single word lines.

Let's see if I can fix it.
0
 
LVL 22

Accepted Solution

by:
cookre earned 500 total points
ID: 12090510
A wee fix.
Presuming there's no '@' in the text file.
If there is, change the delims="@" below to some char not in the file.
The var names were chosen because:
1) I'm lazy
2) To avoid stepping on extant names.  

@echo off

:: Get time (alas, it's only HH:MM xM
for /f %%a in ('time /t') do set zD1=%%a

:: Get last digit of MM
set zD2=%zD1:~4,1%

:: Seed the randomizer, if needed
if not defined zNUM1 set /a zNUM1=%zD2%

:: Get a kinda random number
set /a zNUM1=zNUM1 * 214013 + 2531011
set /a zNUM2=zNUM1 ^>^> 16 ^& 0x7fff

:: Pull off the first digit
:: (Last digit would be better, but it's late, and I'm tired)
set zIDX=%zNUM2:~0,1%

:: Map it down to 0-3
set /a zIDX=zIDX/3

:: Finally, we can set do some proper initialization
set /a zIIDX=0
set zLO=
set zLL=""

:: Step through each line in the file, looking for line zIDX
for /f "delims=@" %%a in (c:\lines.txt) do call :zoo  %zIDX%  %%a

:: If line zIDX wasn't found, we'll settle for zee LastLine
if "%zLO%"=="" set zLO=%zLL%
goto awdun

:: See if the current line is line zIDX
:zoo

:: Save string of all parms
set zALL=%*

:: Strip off the first parm (sure hope lines aren't longer than 254 chars)
set zWORDS=%zALL:~2,255%

:: Make this line zee LastLine
set zLL=%zWORDS%

:: If this is the line we're looking for, make it zee LineOut
if {%1}=={%zIIDX%} set zLO=%zWORDS%

:: Keep track of line numbers
set /a zIIDX=%zIIDX% + 1
goto :eof

:awdun
echo ==%zLO%==

:: Be socially responsible
set zALL=
set zD1=
set zD2=
set zIDX=
set zIIDX=
set zLL=
set zLO=
:: But don't mess with seed
::set zNUM1=
set zNUM2=
set zWORDS=
0
 
LVL 84

Expert Comment

by:oBdA
ID: 12091731
Assuming W2k or later, this should do it (number of lines is flexible):

@echo off
setlocal
set TextFile=D:\Temp\test.txt
:: *** Get the number of lines in the file:
for /f "delims=" %%a in ('find /v /c "" "%Textfile%"') do for %%i in (%%a) do set NumLines=%%i
:: *** Create a random number in the range of the text file:
set /a RandomLine = (%Random% %% NumLines) + 1
:: *** And print the line:
for /f "tokens=1* delims=[]" %%a in ('find /v /n "" "%TextFile%"') do if %%a EQU %RandomLine% echo %%b

Some remarks:
setlocal makes sure the used variables are only existing during the run of the batch.
%Random% only exists in W2k and later.
The "number of lines" construction may seem a bit odd, but find's count output can't easily be handled with for /f, since the number of lines is always the last token, but there's no usable delimiter in the output, as there may be spaces as well as an addiitonal colon in the file name.
0
 
LVL 5

Expert Comment

by:ITcrow
ID: 12094441

# find a random number:
Eg. assumes you have five lines:
set /a x=(%random%%5)+1

# number your file, if you don't already have it that way:
type YourMessageFile.txt | find /v /n "&$&$&$123" > YourNewMessageFile.txt

# Find the message according to random number given in Step 1
type YourNewMessageFile.txt | find "[%x%]"
0
 
LVL 3

Expert Comment

by:Statick001
ID: 12101555
wow this is a fascinating thread!
surprised anyone can still code like that in batch commands :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Data center, now-a-days, is referred as the home of all the advanced technologies. In-fact, most of the businesses are now establishing their entire organizational structure around the IT capabilities.
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question