Windows Batch script to pick a random section of text from a source file?
November 14, 2008 10:43 AM   Subscribe

What's the easiest way to make a Windows Batch file that will randomly choose one section from a file and copy it to a new file?

Basically, I want to be able to add sections to one file, ideally being able to put some kind of delimiter between sections, and have a Windows batch file (XP-era, if that matters) pull one of those sections out at random and place it into a separate file, replacing what was there from the last run.

Side notes:
I know "random" isn't all that random on a computer. If it's pretty close, that's fine.

I'd prefer being able to have some kind of delimiter separate them rather than have it go by line breaks, as I'd like to use line breaks inside sections.

I don't mind if I have to update the batch file to explicitly say how many sections are in the source file. If I don't have to, that'd be shiny, but it won't change often enough for me to care too much.
posted by John Kenneth Fisher to Computers & Internet (10 answers total) 3 users marked this as a favorite
 
Could your batch file include the use of Unix-tools (via cygwin) like awk, sed, or perl, or are you trying to stay 100% windows?
posted by jquinby at 10:56 AM on November 14, 2008


Response by poster: Pretty much straight Windows.

If I could bring in *nix tools, I'd have it done already ;-)
posted by John Kenneth Fisher at 10:58 AM on November 14, 2008


I don't have an answer for you, but I would suggest that you take it to Usenet. The batch-related newsgroups are relatively low traffic, and there are a few folks who hang out there who live for challenges like this. Assuming this is for a recent flavor of Windows, ask at alt.msdos.batch.nt; if it's for Win98 or below take it to alt.msdos.batch. Like most geeks they can be prickly about poorly-asked questions, but if you do a little bit of homework first, ask a specific question, and show some indications that you've given a try already they can be very helpful.
posted by harkin banks at 11:23 AM on November 14, 2008


The "fortune algorithm" from fortune.c is really quite simple: read each fortune in turn (the first fortune is #1). If you're reading fortune N, then 1/N of the time replace the fortune-to-print with this fortune. When you reach the end of the fortunes, print the fortune-to-print.

Unix fortune either used one-liners (like that fortune.c) or used a file with separators -- I think a line with only "%%" was the traditional separator.

It's doing the file reading and random number generation that looks like the challenge under windows.
posted by jepler at 11:46 AM on November 14, 2008


Yeah, this smells like a batch implementation of fortune in a motd. Win2k and newer includes the %RANDOM% env var, so you can use that with some math to put your string at the end of the other file. Scroll down for the answers in the experts-exchange links, you don't need a subscription or anything. Or you can use `more +%NUMBER%` to get at a random line in the file.
posted by rhizome at 12:00 PM on November 14, 2008


Best answer: I think I finally got it, this was sort of an exercise in how many different DOS commands could I abuse in a single batch file. Try it out here:

http://pastebin.com/f39b08cce

It assumes you have a file called input.txt that is separated by newlines and outputs to a file called output.txt
posted by bertrandom at 12:14 PM on November 14, 2008


Best answer: Not a batch file, but I'd be surprised to find a Windows installation that doesn't have Windows Script Host, even locked-down corporate PCs.

Set fs = CreateObject("Scripting.FileSystemObject")
Set txt = fs.OpenTextFile(WScript.Arguments(0))

Sections = Split(txt.ReadAll, vbCrLf & "$" & vbCrLf)

Randomize Timer
WScript.Echo Sections(Round(Rnd * UBound(Sections)))


Save as fortune.vbs, then run from the command line using cscript fortune.vbs //nologo input.txt > output.txt.
posted by zixyer at 2:31 PM on November 14, 2008


Oh yeah, and in case you can't tell from the code, the input sections are separated by a $ on a line by itself.
posted by zixyer at 2:32 PM on November 14, 2008


Response by poster: excellent, many thanks. I'm going to play with these on Monday, but several of the answers here seem to be exactly what I needed.

You guys? awesome.
posted by John Kenneth Fisher at 5:35 AM on November 16, 2008


Response by poster: I can confirm that zixyer's script works beautifully. Thank you.
posted by John Kenneth Fisher at 9:09 AM on November 23, 2008


« Older blackjack II, electric boogaloo   |   Region 2 Playback on a Region 1 iMac Newer »
This thread is closed to new comments.