how to randomly select a fixed number of files in the current directory in windows explorer using a shortcut key Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Moaz93
Posts: 16
Joined: 05 Aug 2018, 12:32

how to randomly select a fixed number of files in the current directory in windows explorer using a shortcut key

17 Jan 2020, 10:34

Hi every one,

how to randomly select a fixed number of files in the current directory in windows explorer using a shortcut key

thanks for your time...
User avatar
telanx
Posts: 68
Joined: 10 Jan 2020, 14:31

Re: how to randomly select a fixed number of files in the current directory in windows explorer using a shortcut key

17 Jan 2020, 10:46

My suggestion is to number the file name, randomly select the number from the numbered array, and then remove the number to get the file name. It is best to make it with a batch script.
Moaz93
Posts: 16
Joined: 05 Aug 2018, 12:32

Re: how to randomly select a fixed number of files in the current directory in windows explorer using a shortcut key

17 Jan 2020, 11:15

found this in the forum

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%
can you help?
Moaz93
Posts: 16
Joined: 05 Aug 2018, 12:32

Re: how to randomly select a fixed number of files in the current directory in windows explorer using a shortcut key

17 Jan 2020, 11:16

and this

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
}

;==================================================
but I don't know how to configur theme to my needs
User avatar
flyingDman
Posts: 2837
Joined: 29 Sep 2013, 19:01

Re: how to randomly select a fixed number of files in the current directory in windows explorer using a shortcut key

17 Jan 2020, 15:22

This would randomly select 10 files in the current folder in the active Explorer window:

Code: Select all

#\::
list := newlist := ""
if !path := GetActiveExplorerPath()
	return
loop, files, % path "\*.*"
	list .= (a_index = 1 ? "" : "`n") . A_LoopFileFullPath
sort,list, Random
for x,y in strsplit(list,"`n")
	newlist .= a_index = 1 ? y : (x <= 10 ? "`n" y  : "")                                       ; 10 <<<<
msgbox % newlist
return

GetActiveExplorerPath(){
	if ID := WinActive("ahk_class CabinetWClass")
		for window in ComObjCreate("Shell.Application").Windows
			if (window.hwnd==ID)
				return window.Document.Folder.Self.Path
	}
Batch script???
14.3 & 1.3.7
Moaz93
Posts: 16
Joined: 05 Aug 2018, 12:32

Re: how to randomly select a fixed number of files in the current directory in windows explorer using a shortcut key

18 Jan 2020, 03:30

I don't know if it is a batch script, I'm a total noob, so, yeah, but I guess it's an AHK script...
or do you mean you can do a batch script for me?
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: how to randomly select a fixed number of files in the current directory in windows explorer using a shortcut key  Topic is solved

18 Jan 2020, 03:35

Moaz93 wrote:
17 Jan 2020, 11:16
.... but I don't know how to configur theme to my needs
Try this:

Code: Select all

+^1::
+^2::
+^3::
+^4::
+^7::
keys := {1:1,2:25,3:35,4:40,7:70}
choice := keys[SubStr(A_ThisHotkey,3)]
str := sellist := ""
loop, Files, %  JEE_ExpWinGetDir() "\*.*", F ; loops in current explorer folder
	str .= A_LoopFilename "`n" 
sort, str, Random
Loop, Parse, str, `n, `r
{
	If (a_index > choice)
		break
	sellist .= A_LoopField "`n" 
}
JEE_ExpWinSetSel(0,sellist) ; select the desired number of files in the current explorer folder
return

JEE_ExpWinGetDir(hWnd:=0)
{
	local oWin, oWindows, vDir, vWinClass
	DetectHiddenWindows, On
	(!hWnd) && hWnd := WinExist("A")
	WinGetClass, vWinClass, % "ahk_id " hWnd
	if (vWinClass = "CabinetWClass") || (vWinClass = "ExploreWClass")
	{
		for oWin in ComObjCreate("Shell.Application").Windows
			if (oWin.HWND = hWnd)
				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)
	}
	else
		return
	vDir := oWin.Document.Folder.Self.Path
	oWindows := oWin := ""
	return vDir
}

JEE_ExpWinSetSel(hWnd, vList, vSep:="`n", vFlagsF:=0x1D, vFlagsS:=0x1)
{
	local oItems, oWin, oWindows, vFlags, vName, vWinClass
	DetectHiddenWindows, On
	(!hWnd) && hWnd := WinExist("A")
	WinGetClass, vWinClass, % "ahk_id " hWnd
	if (vWinClass = "CabinetWClass") || (vWinClass = "ExploreWClass")
	{
		for oWin in ComObjCreate("Shell.Application").Windows
			if (oWin.HWND = hWnd)
				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)
	}
	else
		return

	oItems := oWin.Document.Folder.Items
	Loop, Parse, vList, % vSep
	{
		;SVSI_FOCUSED = 0x10 ;SVSI_ENSUREVISIBLE := 0x8
		;SVSI_DESELECTOTHERS := 0x4 ;SVSI_EDIT := 0x3
		;SVSI_SELECT := 0x1 ;SVSI_DESELECT := 0x0
		vFlags := (A_Index = 1) ? vFlagsF : vFlagsS
		if !InStr(A_LoopField, "\")
			oWin.Document.SelectItem(oItems.Item(A_LoopField), vFlags)
		else
		{
			SplitPath, A_LoopField, vName
			oWin.Document.SelectItem(oItems.Item(vName), vFlags)
		}
	}
	oWindows := oWin := oItems := ""
}
Esc::ExitApp
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Moaz93
Posts: 16
Joined: 05 Aug 2018, 12:32

Re: how to randomly select a fixed number of files in the current directory in windows explorer using a shortcut key

18 Jan 2020, 03:38

flyingDman wrote:
17 Jan 2020, 15:22
This would randomly select 10 files in the current folder in the active Explorer window:

Code: Select all

#\::
list := newlist := ""
if !path := GetActiveExplorerPath()
	return
loop, files, % path "\*.*"
	list .= (a_index = 1 ? "" : "`n") . A_LoopFileFullPath
sort,list, Random
for x,y in strsplit(list,"`n")
	newlist .= a_index = 1 ? y : (x <= 10 ? "`n" y  : "")                                       ; 10 <<<<
msgbox % newlist
return

GetActiveExplorerPath(){
	if ID := WinActive("ahk_class CabinetWClass")
		for window in ComObjCreate("Shell.Application").Windows
			if (window.hwnd==ID)
				return window.Document.Folder.Self.Path
	}
Batch script???
it gave an error: call to non existent function
Specifically GetActiveExplorerPath()
gregster
Posts: 9095
Joined: 30 Sep 2013, 06:48

Re: how to randomly select a fixed number of files in the current directory in windows explorer using a shortcut key

18 Jan 2020, 03:42

Moaz93 wrote:
18 Jan 2020, 03:38
it gave an error: call to non existent function
Specifically GetActiveExplorerPath()
That function is actually included in the code box. Please copy all the code in the code box, not only the immediately visible part.
Click Expand View to see the whole code (or scroll down in the box), click Select All to select all the code in the code box, ready to be copied.

Change the hotkey (currently #\), if you have other preferences.
Moaz93
Posts: 16
Joined: 05 Aug 2018, 12:32

Re: how to randomly select a fixed number of files in the current directory in windows explorer using a shortcut key

18 Jan 2020, 03:54

Odlanir wrote:
18 Jan 2020, 03:35
Moaz93 wrote:
17 Jan 2020, 11:16
.... but I don't know how to configur theme to my needs
Try this:

Code: Select all

+^1::
+^2::
+^3::
+^4::
+^7::
keys := {1:1,2:25,3:35,4:40,7:70}
choice := keys[SubStr(A_ThisHotkey,3)]
str := sellist := ""
loop, Files, %  JEE_ExpWinGetDir() "\*.*", F ; loops in current explorer folder
	str .= A_LoopFilename "`n" 
sort, str, Random
Loop, Parse, str, `n, `r
{
	If (a_index > choice)
		break
	sellist .= A_LoopField "`n" 
}
JEE_ExpWinSetSel(0,sellist) ; select the desired number of files in the current explorer folder
return

JEE_ExpWinGetDir(hWnd:=0)
{
	local oWin, oWindows, vDir, vWinClass
	DetectHiddenWindows, On
	(!hWnd) && hWnd := WinExist("A")
	WinGetClass, vWinClass, % "ahk_id " hWnd
	if (vWinClass = "CabinetWClass") || (vWinClass = "ExploreWClass")
	{
		for oWin in ComObjCreate("Shell.Application").Windows
			if (oWin.HWND = hWnd)
				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)
	}
	else
		return
	vDir := oWin.Document.Folder.Self.Path
	oWindows := oWin := ""
	return vDir
}

JEE_ExpWinSetSel(hWnd, vList, vSep:="`n", vFlagsF:=0x1D, vFlagsS:=0x1)
{
	local oItems, oWin, oWindows, vFlags, vName, vWinClass
	DetectHiddenWindows, On
	(!hWnd) && hWnd := WinExist("A")
	WinGetClass, vWinClass, % "ahk_id " hWnd
	if (vWinClass = "CabinetWClass") || (vWinClass = "ExploreWClass")
	{
		for oWin in ComObjCreate("Shell.Application").Windows
			if (oWin.HWND = hWnd)
				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)
	}
	else
		return

	oItems := oWin.Document.Folder.Items
	Loop, Parse, vList, % vSep
	{
		;SVSI_FOCUSED = 0x10 ;SVSI_ENSUREVISIBLE := 0x8
		;SVSI_DESELECTOTHERS := 0x4 ;SVSI_EDIT := 0x3
		;SVSI_SELECT := 0x1 ;SVSI_DESELECT := 0x0
		vFlags := (A_Index = 1) ? vFlagsF : vFlagsS
		if !InStr(A_LoopField, "\")
			oWin.Document.SelectItem(oItems.Item(A_LoopField), vFlags)
		else
		{
			SplitPath, A_LoopField, vName
			oWin.Document.SelectItem(oItems.Item(vName), vFlags)
		}
	}
	oWindows := oWin := oItems := ""
}
Esc::ExitApp
woooooow man, you nailed it!!!! you did it like a Ninja!
THaaaaaaaaanks,

just a small question, when I add it to my AHK script file with other scripts, it gives an error (duplicate hotkey) even thought I have no other hotkeys like it in the file, when I run a search, I get only one Instance!
Moaz93
Posts: 16
Joined: 05 Aug 2018, 12:32

Re: how to randomly select a fixed number of files in the current directory in windows explorer using a shortcut key

18 Jan 2020, 04:00

gregster wrote:
18 Jan 2020, 03:42
Moaz93 wrote:
18 Jan 2020, 03:38
it gave an error: call to non existent function
Specifically GetActiveExplorerPath()
That function is actually included in the code box. Please copy all the code in the code box, not only the immediately visible part.
Click Expand View to see the whole code (or scroll down in the box), click Select All to select all the code in the code box, ready to be copied.

Change the hotkey (currently #\), if you have other preferences.
Oh, right, I did, but it gives me a list not a selection in the actual explorer window
User avatar
flyingDman
Posts: 2837
Joined: 29 Sep 2013, 19:01

Re: how to randomly select a fixed number of files in the current directory in windows explorer using a shortcut key

18 Jan 2020, 15:31

Selected as in highlighted, focused. OK, try this then: (based on the same principle, but a bit more compact)

Code: Select all

#\::
list := "", WinDoc := GetActiveWinDoc()
if !path := WinDoc.Folder.Self.Path
	return
loop, files, % path "\*.*"
	list .= (a_index = 1 ? "" : "`n") . A_LoopFileFullPath
sort,list, Random
for x,y in strsplit(list,"`n")
	if (x <= 4) 
		try WinDoc.SelectItem(y, (x = 1 ? 0x1D : 0x1))
return

GetActiveWinDoc(){
	if ID := WinActive("ahk_class CabinetWClass")
		for window in ComObjCreate("Shell.Application").Windows
			if (window.hwnd==ID)
				return window.Document
	}
The question is obviously why do you want to have these files "selected" and don't you want these files in a list or an array eventually...?
14.3 & 1.3.7
Moaz93
Posts: 16
Joined: 05 Aug 2018, 12:32

Re: how to randomly select a fixed number of files in the current directory in windows explorer using a shortcut key

19 Jan 2020, 09:07

flyingDman wrote:
18 Jan 2020, 15:31
Selected as in highlighted, focused. OK, try this then: (based on the same principle, but a bit more compact)

Code: Select all

#\::
list := "", WinDoc := GetActiveWinDoc()
if !path := WinDoc.Folder.Self.Path
	return
loop, files, % path "\*.*"
	list .= (a_index = 1 ? "" : "`n") . A_LoopFileFullPath
sort,list, Random
for x,y in strsplit(list,"`n")
	if (x <= 4) 
		try WinDoc.SelectItem(y, (x = 1 ? 0x1D : 0x1))
return

GetActiveWinDoc(){
	if ID := WinActive("ahk_class CabinetWClass")
		for window in ComObjCreate("Shell.Application").Windows
			if (window.hwnd==ID)
				return window.Document
	}
The question is obviously why do you want to have these files "selected" and don't you want these files in a list or an array eventually...?
Oh, the reason is that I'm a teacher at 3 universities, so every one of theme has their own template for paper based multi-choice exam, like 25 questions or 70.... so I have a bank of questions, 345 and growing.... so I need to have a way to randomly select files from the bank folder, because every file is a question.... it's like a lottery of questions if you want to call it like that LOL....

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], mikeyww and 105 guests