Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Random file selection


  • Please log in to reply
7 replies to this topic
Rajat
  • Members
  • 1904 posts
  • Last active: Jul 17 2015 07:45 AM
  • Joined: 28 Mar 2004
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.

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%

MIA

CleanNews.in : Bite sized latest news headlines from India with zero bloat


Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Nice, I might use that to play a random song file.

john tuomi
  • Members
  • 150 posts
  • Last active: Dec 08 2011 06:38 PM
  • Joined: 22 May 2006
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?

john tuomi
  • Members
  • 150 posts
  • Last active: Dec 08 2011 06:38 PM
  • Joined: 22 May 2006
How would hotstrings work on this list?

someone...
  • Guests
  • Last active:
  • Joined: --
For the names selection you could do something like this, as long as they're stored in a file with 1 name per line.

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%
}


engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
someone..., john tuomi already has an ask for help thread on this: Random Name Selector Help please!

soggos
  • Members
  • 129 posts
  • Last active: Nov 30 2012 10:35 AM
  • Joined: 27 Mar 2008
Hello,
i like it, very kool, and it's run very good
But i don't understand the variable:

UsedNames

in

StringSplit, UsedNames, UsedNames, |


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

.

Oberon
  • Members
  • 442 posts
  • Last active: Jul 03 2008 11:34 PM
  • Joined: 18 Feb 2008
StringSplit and multiple directory loops are very inefficient for different reasons, this should be much easier to understand:

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%