There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Batch File -- Read Random Line from External File into an Environment Variable


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
encryptedbytes's Avatar
Junior Member with 2 posts.
 
Join Date: Sep 2007
Experience: Advanced
27-Sep-2007, 04:28 PM #1
Batch File -- Read Random Line from External File into an Environment Variable
I've been searching the web on how to accomplish this but am coming up empty handed so I am hoping someone here can help...

I want my batch file to read a random line from a text file into an environment variable - can this be done?

I am using Windows 2000.
JohnWill's Avatar
Computer Specs
Moderator with 77,418 posts.
 
Join Date: Oct 2002
Location: South Eastern PA, USA
Experience: Advanced age & experience
27-Sep-2007, 04:37 PM #2
How does it find this "random" line in the file?
encryptedbytes's Avatar
Junior Member with 2 posts.
 
Join Date: Sep 2007
Experience: Advanced
27-Sep-2007, 04:48 PM #3
I guess I am not sure...

The file I want to read from will have a list of "agents" like so:

Agent #1
Agent #2
Agent #3
Agent #4
Agent #5 (and so on...)

I want to select one of them randomly and shove that line into an environment variable. I am thinking maybe there's a way to read the number of lines in the file into a variable (X), then generate a random integer between 1 and X (Y), and then read line Y into another variable %AGENT% that I can use as a paramenter in a program the batch file is running.

I wish I knew more about this stuff
JohnWill's Avatar
Computer Specs
Moderator with 77,418 posts.
 
Join Date: Oct 2002
Location: South Eastern PA, USA
Experience: Advanced age & experience
28-Sep-2007, 08:47 AM #4
I don't know how to do this with a plain batch file, I'd probably write a 20 line C++ program to do it.
ghostdog74's Avatar
Member with 90 posts.
 
Join Date: Dec 2005
28-Sep-2007, 10:26 PM #5
Quote:
Originally Posted by encryptedbytes
I've been searching the web on how to accomplish this but am coming up empty handed so I am hoping someone here can help...

I want my batch file to read a random line from a text file into an environment variable - can this be done?

I am using Windows 2000.
i won't do the batch for you, but some suggestions
1) use %random% variable to generate the random number
2) use a for loop to iterate the file, then count the number of lines in the file. store in variable
3) find a way to "normalize" the random number to fit inside the range from 1 to the number of lines found in 2)
eg set /a getline=%random%%10 . (for 10 lines). You can derive your own way to come up with a number in the range
4) then iterate the file again, but this time when the counter hits getline, print the line out and exit. remember to store this to whatever environment variable you are using...
Reply


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

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
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 06:37 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.