Solved File Explorer Restart (Tabs & Windows)


dacrone

Well-known member
Guru
VIP
Local time
7:16 AM
Posts
4,600
OS
Windows 11 Pro
This ahk (compiled exe attached - may have to allow it on defender or av) will allow you to assign a hotkey that can be used to save open explorer instances, restart file explorer and relaunch the instances as they were (ie - if you had downloads & pictures in one window and documents in a separate window, that is how they will be restored). runs in the tray.

1758250283078.webp

AHK Code:
Code:
#Requires AutoHotkey v2.0

TraySetIcon("imageres.dll", 266)
A_TrayMenu.Delete()
A_TrayMenu.Add("Restart Explorer", (*) => RestartExplorer())
A_TrayMenu.Add("Assign Hotkey", (*) => ShowHotkeyGui())
A_TrayMenu.Add("Exit", (*) => ExitApp())

global currentHotkey := ""
global hotkeyGui := ""
global capturedKey := ""

RestartExplorer()
{
    explorerGroups := Map()
    shellApp := ComObject("Shell.Application")

    for win in shellApp.Windows
    {
        if !InStr(win.FullName, "explorer.exe")
            continue

        hwnd := win.HWND
        path := win.Document.Folder.Self.Path

        if explorerGroups.Has(hwnd)
            explorerGroups[hwnd].Push(path)
        else
            explorerGroups[hwnd] := [path]
    }

    Run "taskkill /f /im explorer.exe", , "Hide"
    Sleep 1000
    Run "explorer.exe"
    Sleep 3000

    for hwnd, paths in explorerGroups
    {
        if paths.Length = 1
        {
            Run "explorer.exe " paths[1]
            Sleep 800
        }
        else
        {
            Run "explorer.exe " paths[1]
            Sleep 2000
            WinActivate "ahk_class CabinetWClass"
            Sleep 1000

            Loop paths.Length - 1
            {
                path := paths[A_Index + 1]
                Send "^t"
                Sleep 400
                Send "!d"
                Sleep 400
                A_Clipboard := path
                Sleep 100
                Send "^v"
                Sleep 200
                Send "{Enter}"
                Sleep 600
            }
        }
    }
}

ShowHotkeyGui()
{
    global hotkeyGui, currentHotkey, capturedKey

    if IsObject(hotkeyGui)
        hotkeyGui.Destroy()

    capturedKey := ""

    hotkeyGui := Gui("+AlwaysOnTop -MinimizeBox", "Assign Hotkey")
    hotkeyGui.Add("Text", , "Choose a hotkey:")
    
    ; Set the current hotkey as the default value
    hotkeyGui.Add("Hotkey", "vHotkeyInput w150")
    hotkeyGui["HotkeyInput"].Value := currentHotkey

    hotkeyGui.Add("Text", "vHotkeyDisplay", "Current Hotkey: " currentHotkey)
    applyBtn := hotkeyGui.Add("Button", "Default", "Apply")
    applyBtn.OnEvent("Click", ApplyHotkey)

    hotkeyGui.Show()
}

GuiKeyHandler(GuiObj, Info)
{
    global capturedKey
    key := FormatKey(Info.Name)
    capturedKey := key
    GuiObj["HotkeyInput"].Value := key
    GuiObj["HotkeyDisplay"].Text := "Current Hotkey: " key
}

UpdateCapturedKey()
{
    global capturedKey, hotkeyGui

    ih := InputHook("L1")
    ih.KeyOpt("{All}", "E")
    ih.Start()
    ih.Wait()

    capturedKey := FormatKey(ih.EndKey)
    hotkeyGui["HotkeyDisplay"].Text := "Current Hotkey: " capturedKey
}

ApplyHotkey(btn, *)
{
    global currentHotkey, capturedKey, hotkeyGui

    newHotkey := hotkeyGui["HotkeyInput"].Value
    if newHotkey = ""
        return

    ; Safely remove the old hotkey if it exists
    if currentHotkey != ""
        try Hotkey(currentHotkey, "")

    currentHotkey := newHotkey
    Hotkey(currentHotkey, (*) => RestartExplorer())

    hotkeyGui.Destroy()
}

FormatKey(key)
{
    ; Normalize key names if needed
    if key ~= "^[A-Z0-9]$"
        return key
    if key = "Escape"
        return "Esc"
    if key = "Space"
        return "Space"
    if key = "Enter"
        return "Enter"
    return key
}

CaptureKey()
{
    global capturedKey, hotkeyGui

    ih := InputHook("L1")
    ih.KeyOpt("{All}", "E")
    ih.Start()
    ih.Wait()

    key := ih.EndKey
    modifiers := ""

    if GetKeyState("Ctrl")
        modifiers .= "Ctrl+"
    if GetKeyState("Alt")
        modifiers .= "Alt+"
    if GetKeyState("Shift")
        modifiers .= "Shift+"

    capturedKey := modifiers . FormatKey(key)
    hotkeyGui["HotkeyDisplay"].Text := "Current Hotkey: " capturedKey
}

; Keep the script running indefinitely
Loop
    Sleep 1000

EDIT:
I added the version without hotkey too. simply double click it to execute. can use for context menu or whatever other use...

EDIT2:
i updated the code to paste paths when reopening tabs instead of sending keys. didnt work earlier but figured it out. updated exes are attached and code above is updated
 

Attachments

Last edited:

My Computer

System One

  • OS
    Windows 11 Pro
Back
Top Bottom