Get current explorer window path

Post your working scripts, libraries and tools
AlexV
Posts: 1
Joined: 17 Nov 2019, 05:42

Get current explorer window path

17 Nov 2019, 05:51

I've been trying to find a good script to cleanly and reliably get the current path of an explorer window without tricks like reading it from the address bar or knowing the user profile name and so on. Having finally found the right interfaces and properties, I thought I'd share it for others:

Code: Select all

GetActiveExplorerPath()
{
	explorerHwnd := WinActive("ahk_class CabinetWClass")
	if (explorerHwnd)
	{
		for window in ComObjCreate("Shell.Application").Windows
		{
			if (window.hwnd==explorerHwnd)
			{
				return window.Document.Folder.Self.Path
			}
		}
	}
}
If the string it returns starts with :: then it's a special folder of some sort, and doesn't really have a path as such.
User avatar
boiler
Posts: 3468
Joined: 21 Dec 2014, 02:44

Re: Get current explorer window path

17 Nov 2019, 22:54

Thanks for sharing this.
godofOOF
Posts: 26
Joined: 22 Dec 2018, 06:03

Re: Get current explorer window path

07 Dec 2019, 09:56

how do you use this
User avatar
boiler
Posts: 3468
Joined: 21 Dec 2014, 02:44

Re: Get current explorer window path

07 Dec 2019, 11:11

Activate the file explorer window (via your script or manually), then call the function. If you are activating it manually, then you’ll need some way to trigger the function, like make it part of a hotkey subroutine. One way to call the function is to assign the returned value of the function to a variable:

Code: Select all

Path := GetActiveExplorerPath()

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 42 guests