Get a random file from directory with no file naming conventions?

266 views
Skip to first unread message

Paul [sabret00the]

unread,
Jul 21, 2012, 3:38:22 PM7/21/12
to tas...@googlegroups.com
Is this at all possible? I have a lot of randomly named files and I'd like to grab a random one each time the screen turns off? The event screen off trigger is the easy part and I know I can delete the previous file, and then write the new files to where I want the random file to be. The problem I face however is that I simply don't know the best way to grab a file randomly. Anyone tried anything like this?

UncleMike

unread,
Jul 21, 2012, 6:25:13 PM7/21/12
to tas...@googlegroups.com
I'm not sure what you're talking about with "write the new files to where I want the random file to be".  But to randomly select a file from a specific directory I would do something like this:

List Files [Dir:Download Variable:%files]   <--- get a list of files in the Download folder and store it in the array named %files
Variable Randomize [Name:%random Min:1 Max:%files(#)]   <--- pick a random integer between (inclusive) 1 and the number of files in the Download folder
Variable Set [Name:%randomfile %files(%random)]   <--- get the name of the file at the random position in the array

Paul [sabret00the]

unread,
Jul 23, 2012, 6:13:31 AM7/23/12
to tas...@googlegroups.com
UncleMike. Thank you so so much. You've helped me out a ton. This got me in the right direction and everything is working perfectly. Thank you.

One question though, If I keep the dir list in memory and only clear it every fifteen minutes (we're talking around 5000 files), would I kill performance on my device? This is compared to manually fetching the list and clearing it every time the screen turns off. Basically I'm asking if it's cheaper on the devices resources to store the list in memory or to create a new list every time?
--
Until Next Time, Make Me A Marvel!

UncleMike

unread,
Jul 23, 2012, 7:59:28 AM7/23/12
to tas...@googlegroups.com
I realized after my original post that you might want to clear the array after choosing a file.  I have no idea which method would be more efficient, though I'm surprised it works with 5000 files (no offense, Pent!).  If the content of the folder won't be changing regularly, you might want to look at writing the list of files to another file (in a separate directory) and then clearing the array, and use a single variable to store the number of files (or even hard code that number in the task).  You could then use the Read Line action to randomly read a line from the list of files, which would basically be doing the same things you're doing now, except that the list of files is stored on the sdcard instead of in memory.


On Monday, July 23, 2012 6:13:31 AM UTC-4, Paul [sabret00the] wrote:
UncleMike. Thank you so so much. You've helped me out a ton. This got me in the right direction and everything is working perfectly. Thank you.

One question though, If I keep the dir list in memory and only clear it every fifteen minutes (we're talking around 5000 files), would I kill performance on my device? This is compared to manually fetching the list and clearing it every time the screen turns off. Basically I'm asking if it's cheaper on the devices resources to store the list in memory or to create a new list every time?

Paul [sabret00the]

unread,
Jul 23, 2012, 1:12:02 PM7/23/12
to tas...@googlegroups.com
Thanks again UncleMike. I've been looking into it. Can I simply WRITE FILE and use the array as the content for file or do I need to do anything to the variable first? I can't seem to find an implode function.

UncleMike

unread,
Jul 23, 2012, 2:08:10 PM7/23/12
to tas...@googlegroups.com
I think you would need to make a loop that would write each element of the array followed by a new-line.

By "implode" I think you mean to create a single variable with the contents of the entire array.  If so, assuming your array elements are named %array1, %array2 and so on, you can get all element values (comma-delimited) using %array().  But I don't think this will help with writing the data to a file.


On Monday, July 23, 2012 1:12:02 PM UTC-4, Paul [sabret00the] wrote:
Thanks again UncleMike. I've been looking into it. Can I simply WRITE FILE and use the array as the content for file or do I need to do anything to the variable first? I can't seem to find an implode function.

Paul [sabret00the]

unread,
Jul 23, 2012, 3:54:26 PM7/23/12
to tas...@googlegroups.com
Ah, I was hoping it's be as simply as
%filecontents = implode('\\n', $array);
writefile(%filecontents, %filelocation);

Would you advise making Tasker do the loop via a Goto Action or should I attempt to use the FOR task? If I do, the FORs in Tasker confuse the hell out of me, what happened to a simple
foreach ($array AS $key => $value){

or

for ($key = 1, $key <= $filecount, $key++){

Paul [sabret00the]

unread,
Jul 23, 2012, 5:55:49 PM7/23/12
to tas...@googlegroups.com


A2: For [ Variable:%FILETMP Items:%FILES() ]
A3: Write File [ File:%LSTLOC Text:%FILETMP Append:On ]
A4: Write File [ File:%LSTLOC Text:\n Append:Off ]
A5: End For
A6: Array Clear [ Name:%FILES ]
A7: Variable Clear [ Name:%FILEID Pattern Matching:Off ]

UncleMike

unread,
Jul 23, 2012, 8:21:38 PM7/23/12
to tas...@googlegroups.com
I'm assuming you posted that because it worked.  Good deal.
Reply all
Reply to author
Forward
0 new messages