Display random file

 avatar
jbcooper
lua
a year ago
651 B
8
Indexable
math.randomseed(os.time())  --seed the random number generator

directory_file_object=io.popen("ls")  --folder may be needed e.g. "ls /home/james/images"

file_list=directory_file_object:read("a")  --read the file object into a string

file_table={}

for file_name in string.gmatch(file_list,"%S+") do table.insert(file_table,file_name) end  --read the words in the string into a table

random_file_number=math.random(1,#file_table)  --pick a random number between 1 and number of files

name_of_random_file=file_table[random_file_number]  --read the filename from the table

os.execute("xdg-open "..name_of_random_file)  --open it with default viewer
Editor is loading...
Leave a Comment