Get filepath of the file currently selected in Explorer

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
SPrityPh
Posts: 4
Joined: 24 Mar 2023, 02:50

Get filepath of the file currently selected in Explorer

Post by SPrityPh » 25 Mar 2023, 07:02

Hi guys, is there a way to do this with Windows 10 Explorer?

The flow is:

1. (Manually) Explorer is opened and a certain file is highlighted.
2. Hotkey is pressed (LShift in my case)
3. AHK script gets the absolute file path of the file

ntepa
Posts: 406
Joined: 19 Oct 2022, 20:52

Re: Get filepath of the file currently selected in Explorer

Post by ntepa » 25 Mar 2023, 07:37

try this:

Code: Select all

#HotIf WinActive("ahk_class CabinetWClass ahk_exe explorer.exe")

LShift::
{
    items := ExplorerGetSelection()
    MsgBox "selected items:`n" items
}

#HotIf

ExplorerGetSelection() {
    if !hwnd := WinActive("ahk_class CabinetWClass ahk_exe explorer.exe")
        return
    try activeTab := ControlGetHwnd("ShellTabWindowClass1", hwnd)
    for w in ComObject("Shell.Application").Windows {
        if w.hwnd != hwnd
            continue
        if IsSet(activeTab) {
            static IID_IShellBrowser := "{000214E2-0000-0000-C000-000000000046}"
            shellBrowser := ComObjQuery(w, IID_IShellBrowser, IID_IShellBrowser)
            ComCall(3, shellBrowser, "uint*", &thisTab:=0)
            if thisTab != activeTab
                continue
        }
        foundWin := w
        break
    }
    str := ""
    for i in foundWin.Document.SelectedItems
        str .= i.Path "`n"
    return str
}

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Get filepath of the file currently selected in Explorer

Post by boiler » 25 Mar 2023, 07:40

This uses @teadrinker’s Explorer_GetSelection(), modified for v2 (you posted in the v2 section).

Code: Select all

#Requires AutoHotkey v2.0

LShift::MsgBox Explorer_GetSelection()

Explorer_GetSelection() {
   result := ""
   winClass := WinGetClass("ahk_id" . hWnd := WinExist("A"))
   if !(winClass ~= "^(Progman|WorkerW|(Cabinet|Explore)WClass)$")
      Return
   
   shellWindows := ComObject("Shell.Application").Windows
   if (winClass ~= "Progman|WorkerW")  ; IShellWindows::Item:    https://goo.gl/ihW9Gm
                                       ; IShellFolderViewDual:   https://goo.gl/gnntq3
      shellFolderView := shellWindows.Item( ComObject(VT_UI4 := 0x13, SWC_DESKTOP := 0x8) ).Document
   else {
      for window in shellWindows       ; ShellFolderView object: https://tinyurl.com/yh92uvpa
         if (hWnd = window.HWND) && (shellFolderView := window.Document)
            break
   }
   for item in shellFolderView.SelectedItems
      result .= (result = "" ? "" : "`n") . item.Path
   ;~ if !result
      ;~ result := shellFolderView.Folder.Self.Path
   Return result
}

Edit: Posted before seeing ntepa’s post.

WKen
Posts: 182
Joined: 21 Feb 2023, 00:01

Re: Get filepath of the file currently selected in Explorer

Post by WKen » 25 Mar 2023, 10:58

Both scripts get Error: (0x8000FFFF) here. :o Windows 11.
This is the scripts I got from asking in the forum before: viewtopic.php?f=83&t=114431

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Get filepath of the file currently selected in Explorer

Post by mikeyww » 25 Mar 2023, 11:28

It worked here.

blue_fields
Posts: 20
Joined: 16 Feb 2023, 11:07

Re: Get filepath of the file currently selected in Explorer

Post by blue_fields » 25 Mar 2023, 19:49

A maybe more inferior approach, but simpler, is just to have your hotkey send a ctrl+c command which would both copy the file and the path, which is what youre after. You can put the A_Clipboard value into a variable and you can either keep the double quotes that are a part of the string, or remove them.

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Get filepath of the file currently selected in Explorer

Post by flyingDman » 25 Mar 2023, 20:47

Both scripts work here. You choice is whether you want all selected files returned - including in other than the active tab - or just those in the active tab ...
14.3 & 1.3.7

metallizer
Posts: 31
Joined: 13 Aug 2021, 13:34

Re: Get filepath of the file currently selected in Explorer

Post by metallizer » 22 Apr 2023, 21:35

For everyone looking for a quick fix:

In Windows 11, Ctrl+Shift+C would copy the entire path.

SPrityPh
Posts: 4
Joined: 24 Mar 2023, 02:50

Re: Get filepath of the file currently selected in Explorer

Post by SPrityPh » 24 Apr 2023, 23:56

I went the context menu route (it's the first thing I tried just now):

Code: Select all

Enter::{
    Send '+{AppsKey}'
    Send 'A'
}
It works, and it's so fast that the context menu isn't displayed on screen.

First command clicks Shift+Menu, second command "A" which is a shortcut for "Copy as path" menu option.

Thanks, ya'll!

SPrityPh
Posts: 4
Joined: 24 Mar 2023, 02:50

Re: Get filepath of the file currently selected in Explorer

Post by SPrityPh » 24 Apr 2023, 23:59

Implemented this solution

Code: Select all

Enter::{
    Send '+{AppsKey}' ; Click Shift+Menu
    Send 'A' ; Click 'A' which is "copy as path" menu shortcut
}
Thanks ya'll

Post Reply

Return to “Ask for Help (v2)”