Using .bat files for random actions

From ETCwiki
Revision as of 16:08, 25 July 2015 by Ddxfish (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

This code, if pasted into a .bat file will allow you to execute random programs (easy to modify for random sounds, or other random actions).

Usage of .bat random launcher

make a directory to put it in, for example c:\random\ make a new text file, rename it random.bat , and edit it Paste the code below into your .bat file Create a new file named random.txt in the same directory. Add a list of random actions you want your computer to perform, each on its own line like the examples below. NOTE: The .bat file must be executed from the directory random.txt is in. If you want to change that, add its path on your computer in the bat before the filename random.txt. (e.g. c:\random\random.txt instead of random.txt)

You can have random.txt execute any action you want really, and if you are savvy with Windows Command Line, you might want to check out the list of things you can do with this at the bottom of this article.


Random program launch batch code

random.bat -- code is below, or download it here: random.bat (right click and 'Save Link As')

Pulls a random program from random.txt, writes which one it picked to a file, then launches the program.

@Echo Off
For /F "tokens=2 delims=:" %%j in ('Find /C /V "" random.txt') Do (
Set MOD=%%j
)
Set MOD=%MOD:~1%

:LOOP
Set N=%random%
If %N% gtr 99 Set N= %N:~-2,2%
Set N=%N: 0=%
Set /A N=%N%%%%MOD%+1

Set GRTS=
For /F "tokens=1-2 delims=[]" %%j in ('Find /N /V "" random.txt') Do (
If "%%j"=="%N%" If not "%%k"=="" Set GRTS=%%k
)
If not defined GRTS GoTo :LOOP

Echo. %GRTS% > lastactionlaunched.txt
start %GRTS%


Set MOD=
Set N=
Set GRTS=
:: End_Of_Batch


random.txt - make yours to do what you want it to do

List of programs, or values that can be filled in for %GRTS% variable in the batch script.

c:\windows\notepad.exe
c:\myplaylist.m3u
echo outputting to text file > c:\textfile.txt

Important Notes

  • You must launch the .bat file from the same directory, or else you have to change the code to reflect the absolute path of the text file e.g. (c:\random\random.txt)
  • Max lines is 99 in the random action file

What you can do with random .bat files

  • I use this mainly with Text-To-Speech programs. My setup is advanced, and when I say something to my computer, it can respond from a list of responses, all meaning the same thing but said in a different way. In other words, it doesnt have to say OK all the time, it can say OK, Affirmative, Roger that, etc so you dont get really bored with your TTS voices.
  • Random Playlist Launch - you can easily just setup playlists in your random.txt file to have it launch one of your favorites at random.
  • Set files to run at random times (advanced users) by using the at command and the variable %GRTS% from the code, schedule a task from a random list of times contained in random.txt.