Thursday, February 3, 2011

Retrieving Random String from Batch File

The task to be discussed in this post is retrieving a random string from the given text file and then printing it on the screen. When is it useful? For example, you can create the list of user's addresses and send someone a message via 'net send' command as a simple joke. Or even automate the creation of SEO texts with the help of batch files. Enjoy the code:
@echo off
setlocal
set "lines=0"
for /f "tokens=*" %%a in (items.txt) do set /a "lines+=1"
set /a "skip=%random% %% lines"
if %skip% lss 1 (set "skip=") else (set "skip=skip=%skip%")
for /f "%skip% tokens=*" %%a in (items.txt) do set "item=%%a"&goto display
:display
echo %item%
Hope it'll be useful, or at least just amazing for you.

No comments:

Post a Comment

Translate