Solved

Help with random text in a batch logon scritp

Posted on 2004-09-17
12
691 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
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
 
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 85

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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 83

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now