Page 1 of 1

Get current explorer window path

Posted: 17 Nov 2019, 05:51
by AlexV
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.

Re: Get current explorer window path

Posted: 17 Nov 2019, 22:54
by boiler
Thanks for sharing this.

Re: Get current explorer window path

Posted: 07 Dec 2019, 09:56
by godofOOF
how do you use this

Re: Get current explorer window path

Posted: 07 Dec 2019, 11:11
by boiler
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()