Solved Create Batch file to randomly open file

January 26, 2012 at 20:04:48
Specs: Windows 7
I need to create a batch file that opens a random file from a text file containing numerous filepaths.
The list reads as following:
Can anyone help me figure this out please?

See More: Create Batch file to randomly open file

Report •

January 26, 2012 at 23:44:37
1 Get line count from the text file. (There must be no blank lines in the file.)

2. Enable Command Extensions and use the %random% dynamic variable to generate a random number and mod it by the line count +1 to give a text file line number..

3. Extract the path\filename corresponding to the text file line number then open it.

Sounds easy. Hope this helps.

Please come back & tell us if your problem is resolved.

Report •

January 27, 2012 at 06:33:21
Thanks wahine,
Is there any way to do this without a set line number, or what would be the modifier to do that? I'd like to be able to use this on different text files

Report •

January 27, 2012 at 11:04:15
✔ Best Answer
Am I confused about what you want to achieve?

You stated I need to create a batch file that opens a random file from a text file from which I took it that you wanted to open any file at random from a list of files. The %random% variable is used to generate a random number within a range, that number will be used to identify which line the filename to be opened exists on in the text file. The line number will change every time the script is run (almost) therefore a new random file will be opened each time.

The script produced can be used with any file which contains filenames where the extension is associated with a program which will open it.

Here's a random number generator which will display random numbers in the range 1 thru' 42 (as if 42 was the number of lines in the text file)

@echo off


set lines=42

set /a file=%random% %% %lines%+1
if %file% lss 10 set file= %file%
echo The file shown on line %file% of the text file will be selected

set /a cnt+=1
if %cnt% gtr 20 (exit /b
   ) else (
     goto loop

Obviously the script is incomplete and needs coding added to count the number of lines in the text file and to open the appropriate file.

Please come back & tell us if your problem is resolved.

Report •

Related Solutions

January 27, 2012 at 11:14:26
well you can do this for starters:
make batch file called dothis.cmd, like so:
@echo off
if "%1"=="" goto help
if exist "%1" goto continue
if not exist "%1" goto notexist
echo enter filename.ext of the file to convert
goto end
echo filename.ext ("%1") does not exist, enter valid path and filename
goto end
if exist filelist2.txt del filelist2.txt>nul
for /f "skip=1" %%a in (' type %1 ') do (echo %%a>>filelist2.txt)
echo filelist2.txt has been prepared for line searching
goto end
call DisplayRandomFilenameInList.cmd

now that will build the filelist2.txt for searching (and execute the following batch file). so now create another .cmd called DisplayRandomFilenameInList. in it put the following:
@echo off
setlocal EnableExtensions
rem lets get the last line number of filelist2.txt for max random number
for /f "" %%a in (' type filelist2.txt ') do echo %%a > fil3.tmp
for /f "delims=[]" %%b in (' type fil3.tmp ') do set maxrand=%%b
del fil3.tmp
set rand=%random%
if %rand% GTR %maxrand% goto loop else set goodrnd=%rand%
for /f "tokens=2 delims=[]" %%c in (' find "[%goodrnd%]" filelist2.txt ') do echo Random File to display is: %%c

now run dothis.cmd with a parameter (either a local filename.ext or PATH\filename.ext) suchas:
DoThis FileDir1.lst

and you are off to the races (after the first dothis, you can run DisplayRandomFilenameInList.cmd to display the next random file.

please note that you probably need to path all of the filenames is use, so use %temp%\ for the temp stuff and your path for filenames that you want to keep for later use (like filelist2.txt).

Report •

January 27, 2012 at 12:31:36
Thanks for the clarification Wahine, I was confused at how to utilize the %random% variable - My batch file skills are less than admirable.

That script works perfectly, and with some help & tweaking I've gotten it to both find the number of lines in my text file and open the line, but now I'm struggling because whenever I rerun the batch on my txt file the line it decides to open is always a short number of lines ahead of the last line it opened.

For example, if it opens line 11024, the next time i run it it opens line 11161.

This is the code:

@echo off & setlocal

set "txtfile=fileslist.txt"

for /f %%C in ('Find /V /C "" ^< "%txtfile%"') do set Count=%%C

set lines=%Count%

set /a file=%random% %% %lines%+1
if %file% lss 10 set file= %file%
echo File #%file% will be selected

<"%txtfile%" (
for /l %%i in (1 1 %file%) do set /p "ln="

start "" "%ln%"

Any ideas/tips/modifications?

Report •

January 27, 2012 at 12:35:20
Also, thanks skorpio - that script worked great in finding a random line in my file, but I actually need it to open the file. Also, I don't really need to worry about converting the file because I know they will always be named fileslist.txt and contain no blank lines

Report •

Ask Question