AutoHotkey Community

It is currently May 27th, 2012, 7:15 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Random file selection
PostPosted: April 23rd, 2004, 5:03 am 
Offline

Joined: March 28th, 2004, 3:53 pm
Posts: 1870
here's a function that'll select a random file from user defined file types... comes in handy for many tasks like wallpaper changing etc.

Code:
total = 0
Loop, %a_scriptdir%\*.bmp
{
        total += 1
}

Random, select, 1, %total%

Loop, %a_scriptdir%\*.bmp
{
        IfEqual, A_Index, %select%, Setenv, file, %A_LoopFileFullPath%
}

msgbox, %file%

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 23rd, 2004, 5:33 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Nice, I might use that to play a random song file.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Random file selecter
PostPosted: August 16th, 2007, 4:19 pm 
Offline

Joined: May 22nd, 2006, 2:12 pm
Posts: 150
Do you know of an easy way to make this file select entries from a list instead? I have a need for a script to select 5 random names daily from a set list of 493 names. Additionally, it will have to avoid repeat use of the same names.

Any ideas?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 16th, 2007, 6:27 pm 
Offline

Joined: May 22nd, 2006, 2:12 pm
Posts: 150
How would hotstrings work on this list?


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Random name selection
PostPosted: August 17th, 2007, 7:11 am 
For the names selection you could do something like this, as long as they're stored in a file with 1 name per line.

Code:
NumberOfNames = 3          ;Number of names to choose
InputFile     = Names.txt  ;Name of the file where names are stored
OutputFile    = Chosen.txt ;Name of the file where chosen names are stored

; This loop finds out how many names are in the file,
; if you don't need to do this, just replace this whole
; section with:
; TotalNames = # of names
Loop
{
 FileReadLine, TestSize, %InputFile%, %A_Index%
 If ErrorLevel
 {
  TotalNames := A_Index-1
  break
 }
}

; Chooses the names, and writes them to %OutputFile%
UsedNames =
Loop, %NumberOfNames%
{
 StringSplit, UsedNames, UsedNames, |
 Loop
 {
  LineNum =
  NewName =
  Random, LineNum, 1, %TotalNames%
  FileReadLine, NewName, %InputFile%, %LineNum%
  Loop, %UsedNames0%
  {
   If (UsedNames%A_Index% = NewName)
    Continue
  }
  Break
 }
 UsedNames = %UsedNames%|%NewName%
 FileAppend, %NewName%, %OutputFile%
}


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 17th, 2007, 5:03 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8667
Location: Salem, MA
someone..., john tuomi already has an ask for help thread on this: Random Name Selector Help please!

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 23rd, 2008, 11:50 am 
Offline

Joined: March 27th, 2008, 7:46 pm
Posts: 129
Location: France
Hello,
i like it, very kool, and it's run very good
But i don't understand the variable:
Quote:
UsedNames

in
Quote:
StringSplit, UsedNames, UsedNames, |


Where she comes from???
it's a variable autohotkey?
(i don't show him anywhere)

.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 23rd, 2008, 12:01 pm 
Offline

Joined: February 18th, 2008, 8:26 pm
Posts: 442
StringSplit and multiple directory loops are very inefficient for different reasons, this should be much easier to understand:

Code:
files = `;
Loop, *.ahk
{
   files = %files%%A_LoopFileFullPath%;
   total = %A_Index%
}
Random, r, 1, total
StringGetPos, p, files, `;, L%r%
StringMid, file, files, p += 2, InStr(files, ";", "", p) - p
MsgBox, %file%


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 22 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group