Search code examples
windowsshellautohotkeywindows-shell

Navigate Shell command not working when the path includes an hash


I'm having problem using the Navigate Shell command when the path include an # sign.

; this will create 2 folders at the root of your C: drive
myPath1 := "C:\delete_me\"
myPath2 := "C:\delete#me\"
if !FileExist(myPath1)
    FileCreateDir, %myPath1%
if !FileExist(myPath2)
    FileCreateDir, %myPath2%
; make an Explorer active and press Alt-1 and Alt-2
return

!1::
strWinId := WinExist("A")
TrayTip, %myPath1%, %strWinId%
For pExp in ComObjCreate("Shell.Application").Windows
    if (pExp.hwnd = strWinId)
        try pExp.Navigate(myPath1)
return

!2::
strWinId := WinExist("A")
TrayTip, %myPath2%, %strWinId%
For pExp in ComObjCreate("Shell.Application").Windows
    if (pExp.hwnd = strWinId)
        try pExp.Navigate(myPath2)
return

Alt-1 works well. But, with Alt-2, the Navigate command returns "file:///C:/delete#me/ » not found.".

If there is no "/" after the "#" (eg myPath := "C:\delete#me"), it works. But this cannot be a solution because the destination path can be deeper in a subfolder (eg. "C:\delete#me\xyz").

I tried to encode the "#", replacing it with "%23", without success. Found nothing on the web or MSDN about that. Any idea?

[keywords: haskmark, hashtag, number sign or pound]


Solution

  • If you want to open a new window, there's no need for COM or unreliable workarounds: just run the folder.

    Run C:\delete#me
    

    If you want to open the path in an existing window which is already active, the simplest and most effective workaround is this:

    SendInput {F4}{Esc}{Raw}C:\delete#me`n
    

    So in the context of your script, you could use the following function to work around the # when it is present:

    Navigate(pExp, myPath2)
    
    ;...
    
    Navigate(Exp, Path)
    {
        if RegExMatch(Path, "#.*\\")
            SendInput {F4}{Esc}{Raw}%Path%`n
        else
            Exp.Navigate(Path)
    }