AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Random file selection

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Rajat



Joined: 28 Mar 2004
Posts: 1687

PostPosted: Fri Apr 23, 2004 5:03 am    Post subject: Random file selection Reply with quote

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%

_________________
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10666

PostPosted: Fri Apr 23, 2004 5:33 am    Post subject: Reply with quote

Nice, I might use that to play a random song file.
Back to top
View user's profile Send private message Send e-mail
john tuomi



Joined: 22 May 2006
Posts: 132

PostPosted: Thu Aug 16, 2007 4:19 pm    Post subject: Random file selecter Reply with quote

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?
Back to top
View user's profile Send private message
john tuomi



Joined: 22 May 2006
Posts: 132

PostPosted: Thu Aug 16, 2007 6:27 pm    Post subject: Reply with quote

How would hotstrings work on this list?
Back to top
View user's profile Send private message
someone...
Guest





PostPosted: Fri Aug 17, 2007 7:11 am    Post subject: Random name selection Reply with quote

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%
}
Back to top
engunneer



Joined: 30 Aug 2005
Posts: 7703
Location: Madison, WI

PostPosted: Fri Aug 17, 2007 5:03 pm    Post subject: Reply with quote

someone..., john tuomi already has an ask for help thread on this: Random Name Selector Help please!
_________________
Unless noted, all code is UNTESTED.
Answers Here: 1.(Loops, Viruses, etc.) 2.Search 3.RTFM 4.Ask for Help.
PMs will be ignored unless you are hiring me.
Back to top
View user's profile Send private message Visit poster's website
soggos



Joined: 27 Mar 2008
Posts: 97
Location: France

PostPosted: Wed Apr 23, 2008 11:50 am    Post subject: Reply with quote

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)

.
Back to top
View user's profile Send private message Visit poster's website
Oberon



Joined: 18 Feb 2008
Posts: 442

PostPosted: Wed Apr 23, 2008 12:01 pm    Post subject: Reply with quote

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%
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group