

Software Software such as operating systems like Windows XP, Windows Vista, Windows 7 etc., or specific programs. 

Thread Tools  Display Modes 
#1




Open a random file batch
I really need help creating a batch file that would select a random line from a .txt containing files and their paths and open it.
The text file reads as follows: C:\Users\Pictures\asdf.gif C:\Users\sdfasdf\Pictures\Art\Animation\asdfad.jpg C:\Users\AppData\Local\Microsoft\sound\asda.wav etc... Any ideas of how this can be done? 
Sponsored links 

#2




Re: Open a random file batch
Untested:
Code:
@echo off :: name of the txt file set "txtfile=xyz.txt" :: count the lines of the txt file for /f %%i in ('type "%txtfile%"^find /c /v ""') do set /a "n = %%i" :: quit if the file was empty if !n!==0 goto :eof :: calculate a random number between 1 and the number of lines set /a "n = %random% %% %n% + 1" :: read line n <"%txtfile%" ( for /l %%i in (1 1 %n%) do set /p "ln=" ) :: open the file start "" "%ln%" GermanOne 
The Following User Says Thank You to GermanOne For This Useful Post:  
brunchmiracle (012712) 
#3




Re: Open a random file batch
Thanks GermanOne, your code works great!
The only problem I'm having is that when I rerun the batch on the same .txt file it always gives me a file that is between 20 and 200 lines after the one it just opened. For example, if it opens line 11024, the next time i run it it opens line 11161. Do you know if there is any way to switch it up some more? Thanks again Last edited by brunchmiracle; 012712 at 16:38.. Reason: typo 
#4




Re: Open a random file batch
Well, normally this should not happen because of the modulo operation. But the value in %random% is calculated from the current system time. For that reason %random% stays similar as long as the time has changed only slightly. My own experience is that it differs much more if it is used twice in a batch file. Include that line in the beginning of the code:
Code:
>nul echo %random% Regards GermanOne 
#5




Re: Open a random file batch
you're the best  that both explains and helps my problem, if i wait about 23 minutes i get a completely different file so that definitely goes along with the current system time you were talking about. Also, the code i entered at the beginning of the batch helps with this

Sponsored links 

#6




Re: Open a random file batch
Another possible way is to multiply two random numbers before you apply the modulo operation. That could be a good choice especially if there is a high amount of lines in the txt file.
Some information for your understanding:  the value in %random% will expand to a value between 0 and 32767  the greatest positive number batch can calculate with is 2^31  1 (2147483647)  %random% * %random% equals a value between 0 and 1073676289 (which is always less than the greatest numeric value) What is this information good for? Well, the modulo operation (operator % in a command line or %% in a batch file) returns the remainder of a divission. That means 5 %% 3 = 2 (because 5 / 3 = 1, remainder 2). So far obviously clear. But if the first operator is less than the second then the remainder is alwais the same as the first operator. 3 %% 5 = 3 (because 3 / 5 = 0, remainder 3). In your case that means  the random number should reach a greater value than the number of lines in your file to NOT getting exactly the random number  %random% * %random% means that you use the variable twice. For that reason you don't need my former suggestion. Code:
:: calculate a random number between 1 and the number of lines set /a "n = (%random% * %random%) %% %n% + 1" GermanOne 
#7




Re: Open a random file batch
GermanOne is a total master of batch files, and I hate to critique him, (as he usually pwns me,) but I thought I should mention that to get the purest randomness (most evenly distributed probabilities) out of the equation, (using modulus,) it should be:
Code:
:: calculate a random number between 1 and the number of lines set /a "n = (%random% * 32768 + %random%) %% %n% + 1" I recommend the following: You can double the input range by taking one bit from another %random% variable: Code:
:: calculate a random number between 1 and the number of lines set /a "n = (((%random% & 1) * 1073741824) + (%random% * 32768) + %random%) %% %n% + 1" Modulus may vary probabilities, too, but so does any other choosing method, given the same input range (entropy.) All equal probability choosing methods divide the probability as evenly. Their differences are in how they distribute any remainder. Modulus gives it to the lowest numbers. Many try to space it evenly among the choices, but it all boils down to the order the choices happen to be in. The higher the fraction (random number range/things to choose from) the less significant this is. The purest randomness happens when that fraction equates to a positive whole number. If the fraction is less than 1, some things will never be chosen, no matter which method is used, (unless that method gains more entropy to increase the fraction.)
__________________
I.think(code); I.eat(code.spaghetti); Last edited by PCXT; 012812 at 21:40.. 
The Following 2 Users Say Thank You to PCXT For This Useful Post:  
brunchmiracle (012912), GermanOne (012912) 
#8




Re: Open a random file batch
Hi PCXT.
My example above was a simplified calculation to reach a bigger range. You're absolutely right that some of the primes are never calculated. I like the way you enlarged it to the greatest possible value in your 2nd example. Another possibility could be the bitwise calculation in a loop ... Code:
:: calculate a random number between 1 and the number of lines setlocal EnableDelayedExpansion for /l %%i in (0,1,30) do set /a "i = (i << 1) + (!random! & 1)" endlocal &set /a "n = %i% %% %n% + 1" Regards GermanOne 
#9




Re: Open a random file batch
I am eternally indebted to you guys,
all 4 of these methods work perfectly, and as you said, the two that maximize the random range give me the most varied results. I think I can grasp the concept of random number multiplication before the modulo that GermanOne was talking about, as much as I am struggling to understand your code PCXT, I can definitely see the implications of allowing for a larger range of choices through dissolution of the random numbers involved in the equation. Thanks again to both of you for helping me with the code and helping me understand how it works! 
#10




Re: Open a random file batch
You might understand my code better written this way:
Code:
:: calculate a random number between 1 and the number of lines set /a "n = (((%random% & 1) << 30)  (%random% << 15)  %random%) %% %n% + 1"
__________________
I.think(code); I.eat(code.spaghetti); 
Sponsored links 

Tags 
batch, random 
Thread Tools  
Display Modes  


Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Batch program  How could i open a random image from a specified folder?  syeuk2002  Software  3  042914 15:05 
batch file  medic42  Software  13  082511 12:48 
Excel File Windows Open at Random Sizes  GatorDave  Software  2  062311 09:14 
Batch File time stamp and appending to the end of a file  question2ask  Software  2  061804 08:28 
Batch File to remove a space at the end of a charcter in a txt file  rcmathen  Software  2  051404 10:04 