 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Rajat
Joined: 28 Mar 2004 Posts: 1687
|
Posted: Fri Apr 23, 2004 5:03 am Post subject: Random file selection |
|
|
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 |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10666
|
Posted: Fri Apr 23, 2004 5:33 am Post subject: |
|
|
| Nice, I might use that to play a random song file. |
|
| Back to top |
|
 |
john tuomi
Joined: 22 May 2006 Posts: 132
|
Posted: Thu Aug 16, 2007 4:19 pm Post subject: Random file selecter |
|
|
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 |
|
 |
john tuomi
Joined: 22 May 2006 Posts: 132
|
Posted: Thu Aug 16, 2007 6:27 pm Post subject: |
|
|
| How would hotstrings work on this list? |
|
| Back to top |
|
 |
someone... Guest
|
Posted: Fri Aug 17, 2007 7:11 am Post subject: Random name selection |
|
|
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
|
Posted: Fri Aug 17, 2007 5:03 pm Post subject: |
|
|
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 |
|
 |
soggos
Joined: 27 Mar 2008 Posts: 97 Location: France
|
Posted: Wed Apr 23, 2008 11:50 am Post subject: |
|
|
Hello,
i like it, very kool, and it's run very good
But i don't understand the variable:
in
| Quote: | | StringSplit, UsedNames, UsedNames, | |
Where she comes from???
it's a variable autohotkey?
(i don't show him anywhere)
. |
|
| Back to top |
|
 |
Oberon
Joined: 18 Feb 2008 Posts: 442
|
Posted: Wed Apr 23, 2008 12:01 pm Post subject: |
|
|
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 |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|