Select a random file inside the current open folder?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
nilsso
Posts: 4
Joined: 22 Jun 2018, 08:56

Select a random file inside the current open folder?

22 Jun 2018, 09:08

So lets say I have folder open infront of me, is there a way to make a script that chooses a random file inside that folder?

Thanks!
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Select a random file inside the current open folder?

22 Jun 2018, 13:21

Hello and welcome to the AHK-forum.

Your task is solvable.

With a special Loop and a random number you will get what you desire.

If you have further questions or trouble, don't hesitate. :mrgreen: :mrgreen:
Einfach nur ein toller Typ. :mrgreen:
nilsso
Posts: 4
Joined: 22 Jun 2018, 08:56

Re: Select a random file inside the current open folder?

03 Jul 2018, 22:37

divanebaba wrote:Hello and welcome to the AHK-forum.

Your task is solvable.

With a special Loop and a random number you will get what you desire.

If you have further questions or trouble, don't hesitate. :mrgreen: :mrgreen:
I dont even know where to begin, haha. There are so many commands, which ones are the right ones and how do i proceed? I dont understand it sorry :C
Guest

Re: Select a random file inside the current open folder?

04 Jul 2018, 08:35

Look at example 3 here https://autohotkey.com/docs/commands/LoopFile.htm - the first three lines build a file list which is what you want, now use the Random option of the Sort commend (in the example it is the Reverse option) to sort the file list randomly. So copy the first four lines of the example, change the R option of the Sort command.

Now all you have to do is grab the first line of the sorted file list which will be a random file. You can do so with StrSplit or StringSplit (split on `n)

So you will have your random file in 5 lines of code ;)
User avatar
zliyr
Posts: 36
Joined: 30 Jun 2018, 14:22
Contact:

Re: Select a random file inside the current open folder?

04 Jul 2018, 16:56

Method 1: Was not made by me

Code: Select all

	SetWorkingDir,	;Folder
	total = 0
	Loop, *.	;File extension
	{
      	  total += 1
	}
	Random, select, 1, %total%
	Loop, *.	;File extension
	{
	        IfEqual, A_Index, %select%, Setenv, file, %A_LoopFileFullPath%
	}
	MsgBox, %file%
Method 2: You need to click on the bottom folder in file explorer first (rubbish, do not reccomend)

Code: Select all

Random, rand, 1, 3 ;replace 3 with number of files in folder
Loop, %rand%{
	Send {Up}
	}
	return
if you need help with this just let me know! :thumbup:
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Select a random file inside the current open folder?

04 Jul 2018, 17:30

Perhaps this does what you want. Cheers.

Code: Select all

q:: ;Explorer window/Desktop - focus a random file
;get Explorer object for active window:
if !oWin := JEE_ExpWinGetObj()
	return
vCount := oWin.Document.Folder.Items.Count
Random, vNum, 1, % vCount

;SVSI_FOCUSED = 0x10 ;SVSI_ENSUREVISIBLE := 0x8
;SVSI_DESELECTOTHERS := 0x4 ;SVSI_EDIT := 0x3
;SVSI_SELECT := 0x1 ;SVSI_DESELECT := 0x0
oItems := oWin.Document.Folder.Items
if vCount
	oWin.Document.SelectItem(oItems.Item(vNum-1), 0x1D)
oWin := oItems := ""
return

;==================================================

JEE_ExpWinGetObj(hWnd:=0)
{
	DetectHiddenWindows, On
	(!hWnd) && hWnd := WinExist("A")
	WinGetClass, vWinClass, % "ahk_id " hWnd
	if (vWinClass = "CabinetWClass") || (vWinClass = "ExploreWClass")
	{
		for oWin2 in ComObjCreate("Shell.Application").Windows
			if (oWin2.HWND = hWnd)
			{
				oWin := oWin2
				break
			}
	}
	else if (vWinClass = "Progman") || (vWinClass = "WorkerW")
	{
		oWindows := ComObjCreate("Shell.Application").Windows
		VarSetCapacity(hWnd, 4, 0)
		;SWC_DESKTOP := 0x8 ;VT_BYREF := 0x4000 ;VT_I4 := 0x3 ;SWFO_NEEDDISPATCH := 0x1
		oWin := oWindows.FindWindowSW(0, "", 8, ComObject(0x4003, &hWnd), 1)
	}
	return oWin
}

;==================================================
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
nilsso
Posts: 4
Joined: 22 Jun 2018, 08:56

Re: Select a random file inside the current open folder?

05 Jul 2018, 17:37

I will def. be trying all of these! The codes look very complicated to me, Im still a beginner with this. Ive only coded some very basic stuff like an autoclicker, text writer, hotkey binder. All of this is way beyond me. @zliyr, how do I bind that code to a key, is that possible? I will be trying all of them :)
iPhilip
Posts: 814
Joined: 02 Oct 2013, 12:21

Re: Select a random file inside the current open folder?

05 Jul 2018, 18:44

Hi nilsso,

Below is a short version of jeeswg's script.

Code: Select all

F3::SelectRandomItem(WinExist("A"))

; http://msdn.microsoft.com/en-us/library/windows/desktop/bb774047

SelectRandomItem(hwnd) {
   for window in ComObjCreate("Shell.Application").Windows
      if (window.hwnd = hwnd) {
         sfv := window.Document
         Random, item, 1, sfv.Folder.Items.Count
         sfv.SelectItem(sfv.Folder.Items.Item(item-1), 1|4|8|16)
         Return
      }
}
Cheers!

- iPhilip
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
nilsso
Posts: 4
Joined: 22 Jun 2018, 08:56

Re: Select a random file inside the current open folder?

16 Jul 2018, 06:37

Thanks guys! Seems to be working great :)
n00b
Posts: 29
Joined: 24 May 2016, 16:42

Re: Select a random file inside the current open folder?

07 Oct 2022, 11:33

when I run jeeswg and iPhilip code I get this error message
Spoiler
I copied the error message via OCR.

If I click yes the script works as expected.
How to avoid showing the message.
I tried #Warn All, Off without luck.

Cheers
iPhilip
Posts: 814
Joined: 02 Oct 2013, 12:21

Re: Select a random file inside the current open folder?

08 Oct 2022, 12:15

@n00b, You might try try:

Code: Select all

F3::SelectRandomItem(WinExist("A"))

; http://msdn.microsoft.com/en-us/library/windows/desktop/bb774047

SelectRandomItem(hwnd) {
   for window in ComObjCreate("Shell.Application").Windows
      try if (window.hwnd = hwnd) {
         sfv := window.Document
         Random, item, 1, sfv.Folder.Items.Count
         sfv.SelectItem(sfv.Folder.Items.Item(item-1), 1|4|8|16)
         Return
      }
}
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
n00b
Posts: 29
Joined: 24 May 2016, 16:42

Re: Select a random file inside the current open folder?

08 Oct 2022, 15:09

Works now. Thanks a lot iPhilip.
swingking03
Posts: 2
Joined: 31 Mar 2024, 10:55

Re: Select a random file inside the current open folder?

31 Mar 2024, 10:58

I know this is a really old topic but does exactly what I need. However, on folders with a large number of files (ex. 30k files), it crashes. Is there a solution for this?
.
iPhilip
Posts: 814
Joined: 02 Oct 2013, 12:21

Re: Select a random file inside the current open folder?

31 Mar 2024, 20:20

swingking03 wrote:
31 Mar 2024, 10:58
I know this is a really old topic but does exactly what I need. However, on folders with a large number of files (ex. 30k files), it crashes. Is there a solution for this?
My tests reveal that the ShellFolderView.Folder.Items.Item(Index) method crashes Explorer when Index > ~3100.
If you know the name of the file or can derive it from an index, you can use the ShellFolderView.Folder.ParseName(Name) method to get the item and select it.
For example, if the files in the folder are named "1.txt", "2.txt", etc. the following function will work (tested with a folder containing 50k files):

Code: Select all

F3::SelectRandomItem(WinExist("A"), ".txt")

; https://learn.microsoft.com/en-us/windows/win32/shell/shellfolderview

SelectRandomItem(hwnd, ext) {
   for window in ComObjCreate("Shell.Application").Windows
      try if (window.hwnd = hwnd) {
         sfv := window.Document
         Random, item, 1, sfv.Folder.Items.Count
         sfv.SelectItem(sfv.Folder.ParseName(item ext), 1|4|8|16)
         Return
      }
}
If the files in the folder are not named in an ordered way, the following function will work as well, though a bit slower:

Code: Select all

F3::SelectRandomItem(WinExist("A"))

; http://msdn.microsoft.com/en-us/library/windows/desktop/bb774047

SelectRandomItem(hwnd) {
   for window in ComObjCreate("Shell.Application").Windows
      try if (window.hwnd = hwnd) {
         sfv := window.Document
         Random, item, 1, sfv.Folder.Items.Count
         Loop, Files, % sfv.Folder.Items.Item().Path "\*", FD
            if (A_Index = item) {
               sfv.SelectItem(sfv.Folder.ParseName(A_LoopFileName), 1|4|8|16)
               Return
            }
      }
}
I hope this helps.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
swingking03
Posts: 2
Joined: 31 Mar 2024, 10:55

Re: Select a random file inside the current open folder?

08 Apr 2024, 09:41

iPhilip wrote:
31 Mar 2024, 20:20

Code: Select all

F3::SelectRandomItem(WinExist("A"))

; http://msdn.microsoft.com/en-us/library/windows/desktop/bb774047

SelectRandomItem(hwnd) {
   for window in ComObjCreate("Shell.Application").Windows
      try if (window.hwnd = hwnd) {
         sfv := window.Document
         Random, item, 1, sfv.Folder.Items.Count
         Loop, Files, % sfv.Folder.Items.Item().Path "\*", FD
            if (A_Index = item) {
               sfv.SelectItem(sfv.Folder.ParseName(A_LoopFileName), 1|4|8|16)
               Return
            }
      }
}
Sorry for the late reply. This worked perfectly. Thanks so much for taking the time to help me. :bravo:
iPhilip
Posts: 814
Joined: 02 Oct 2013, 12:21

Re: Select a random file inside the current open folder?

08 Apr 2024, 10:37

You are welcome. :)
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: robodesign and 248 guests