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!
Select a random file inside the current open folder?
- divanebaba
- Posts: 805
- Joined: 20 Dec 2016, 03:53
- Location: Diaspora
Re: Select a random file inside the current open folder?
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.
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.
Einfach nur ein toller Typ.
Re: Select a random file inside the current open folder?
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 :Cdivanebaba 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.
Re: Select a random file inside the current open folder?
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
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
Re: Select a random file inside the current open folder?
Method 1: Was not made by me
Method 2: You need to click on the bottom folder in file explorer first (rubbish, do not reccomend)
if you need help with this just let me know!
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%
Code: Select all
Random, rand, 1, 3 ;replace 3 with number of files in folder
Loop, %rand%{
Send {Up}
}
return
Re: Select a random file inside the current open folder?
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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Select a random file inside the current open folder?
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
Re: Select a random file inside the current open folder?
Hi nilsso,
Below is a short version of jeeswg's script.Cheers!
- iPhilip
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
}
}
- iPhilip
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
Re: Select a random file inside the current open folder?
Thanks guys! Seems to be working great
Re: Select a random file inside the current open folder?
when I run jeeswg and iPhilip code I get this error message
If I click yes the script works as expected.
How to avoid showing the message.
I tried #Warn All, Off without luck.
Cheers
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
Re: Select a random file inside the current open folder?
@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)
Re: Select a random file inside the current open folder?
Works now. Thanks a lot iPhilip.
-
- Posts: 2
- Joined: 31 Mar 2024, 10:55
Re: Select a random file inside the current open folder?
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?
.
.
Re: Select a random file inside the current open folder?
My tests reveal that the ShellFolderView.Folder.Items.Item(Index) method crashes Explorer when Index > ~3100.swingking03 wrote: ↑31 Mar 2024, 10:58I 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?
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
}
}
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
}
}
}
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
-
- Posts: 2
- Joined: 31 Mar 2024, 10:55
Re: Select a random file inside the current open folder?
Sorry for the late reply. This worked perfectly. Thanks so much for taking the time to help me.iPhilip wrote: ↑31 Mar 2024, 20:20Code: 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 } } }
Re: Select a random file inside the current open folder?
You are welcome.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
Who is online
Users browsing this forum: mebelantikjaya and 302 guests