当路径包含哈希时,导航Shell命令不起作用

时间:2014-04-04 16:48:33

标签: windows shell autohotkey windows-shell

当路径包含#符号时,我在使用Navigate Shell命令时遇到问题。

; 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效果很好。但是,使用Alt-2,Navigate命令将返回“file:/// C:/ delete#me /»not found。”。

如果“#”后面没有“/”(例如myPath:=“C:\ delete#me”),它就可以了。但这不是一个解决方案,因为目标路径可以在子文件夹中更深(例如“C:\ delete#me \ xyz”)。

我尝试编码“#”,将其替换为“%23”,但没有成功。在网络或MSDN上没有发现任何相关信息。有什么想法吗?

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

5 个答案:

答案 0 :(得分:0)

不幸的是,似乎没有解决方案。如果路径包含散列(在C:\ C#Projects中为#),则Shell.Application Navigate命令失败。

使用AutoHotkey,解决方法是依靠"第二好的"通过此主题中的测试确定的方法:http://ahkscript.org/boards/viewtopic.php?f=5&t=526

run, Explorer.exe
Sleep, 500
strFolder := A_ScriptDir
Send, {F4}{Esc}
Sleep, 500
ControlSetText, Edit1, C:\delete#me, A
ControlSend, Edit1, {Enter}, A

答案 1 :(得分:0)

如果你想打开一个新窗口,不需要COM或不可靠的解决方法:只需运行该文件夹。

Run C:\delete#me

如果要在已经激活的现有窗口中打开路径,最简单且最有效的解决方法是:

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

因此,在脚本的上下文中,您可以使用以下函数解决#时的问题:

Navigate(pExp, myPath2)

;...

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

答案 2 :(得分:0)

当我看到Navigate无法处理哈希时,我很震惊, 但果然我复制了这个错误。 我以为我会尝试一下简短的路径以防万一。它有效!

if vDir contains #
Loop, %vDir%, 2, 0 ;(0/1/2=files/both/folders, 0/1=recurse no/yes)
vDir := A_LoopFileShortPath

答案 3 :(得分:0)

  • 以下方法不需要可见的地址栏或SendInput, 还保留了以前的导航历史记录。
  • 在目标目录上方的dir的短格式路径中的哈希的最坏情况下, 使用了导航到的文件夹。
  • 在那里创建,调用和删除链接。
  • 下面,缩进代码是为了将其与标准代码分开。
  • ctrl + q的热键,当资源管理器窗口处于活动状态时,启动脚本。

-

^q:: ;explorer - navigate to directory (use go-between dir if short-form path of dir above target contains #)
WinGet, hWnd, ID, A
WinGetClass, vWinClass, ahk_id %hWnd%
if vWinClass not in CabinetWClass,ExploreWClass
Return

vDir2 = %A_Desktop%\Go-Between ;go-between dir
vDir3 = C:\delete#me ;target dir

if (SubStr(vDir3, 1-1) = "\")
vDir3 := SubStr(vDir3, 1, -1)
if !InStr(FileExist(vDir3), "D")
Return

    vPathLnk := ""

    if vDir3 contains #
    Loop, %vDir3%, 2, 0 ;(0/1/2=files/both/folders, 0/1=recurse no/yes)
    vDir3 := A_LoopFileShortPath

    ;vDir4 is the short-form path of the dir above the target
    ;paths of problem target dirs are of the form: *#*\*
    ;where there is at least one hash with a backslash to its right
    SplitPath, vDir3, , vDir4
    if vDir4 contains #
    {
    if !InStr(FileExist(vDir2), "D")
    FileCreateDir, %vDir2%
    if !InStr(FileExist(vDir2), "D")
    {
    MsgBox error`, go-between dir not found:`r`n%vDir2%
    Return
    }
    vNameLnk = Go-Between.lnk
    vPathLnk = %vDir2%\%vNameLnk%
    FileCreateShortcut, %vDir3%, %vPathLnk%
    }

for oWin in ComObjCreate("Shell.Application").Windows
if (hWnd = oWin.Hwnd)
{
vDir1 := oWin.Document.Folder.Self.Path
if (vDir1 = vDir3)
break

    if vDir3 contains #
    {
    if !(vDir1 = vDir2)
    oWin.Navigate(vDir2)
    while !(oWin.ReadyState = 4)
    Sleep 10

    oItem := oWin.Document.Folder.Items.Item(vNameLnk)
    oItem.InvokeVerbEx("open")
    break
    }

oWin.Navigate(vDir3)
break
}

oWin := ""

    if !(vPathLnk = "")
    FileRecycle, %vPathLnk% ;send to recycle bin
    ;if !(vPathLnk = "")
    ;FileDelete, %vPathLnk% ;delete
Return

答案 4 :(得分:0)

我有一个看起来像这样的工作解决方案,我也在这里发布:

在Windows资源管理器中更改当前文件夹的4个选项 - 第3页 - AutoHotkey社区
https://autohotkey.com/boards/viewtopic.php?f=5&t=526&p=153676#p153676

;links:
;Explorer Windows Manipulations - Page 5 - Scripts and Functions - AutoHotkey Community
;https://autohotkey.com/board/topic/19039-explorer-windows-manipulations/page-5#entry297581
;Navigate2 Method (IWebBrowser2)
;https://msdn.microsoft.com/en-us/library/aa752134(v=vs.85).aspx
;4 options to change the current folder in Windows Explorer - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=5&t=526
;windows - Navigate Shell command not working when the path includes an hash - Stack Overflow
;https://stackoverflow.com/questions/22868546/navigate-shell-command-not-working-when-the-path-includes-an-hash

;an AutoHotkey v1.1 script
;note: will create folder: %A_Desktop%\abc#def\abc#def
;q:: ;explorer - navigate to folder (tested on Windows 7)
WinGet, hWnd, ID, A
WinGetClass, vWinClass, % "ahk_id " hWnd
if !(vWinClass = "CabinetWClass") && !(vWinClass = "ExploreWClass")
    return

vDir = %A_Desktop%\abc#def\abc#def
;vDir = %A_Desktop%\abc def\abc def
if !FileExist(vDir)
    FileCreateDir, % vDir

DllCall("shell32\SHParseDisplayName", WStr,vDir, Ptr,0, PtrP,vPIDL, UInt,0, Ptr,0)
for oWin in ComObjCreate("Shell.Application").Windows
    if (oWin.HWND = hWnd)
    {
        if !InStr(vDir, "#")
            oWin.Navigate(vDir)
        else
        {
            VarSetCapacity(SAFEARRAY, A_PtrSize=8?32:24, 0)
            NumPut(1, SAFEARRAY, 0, "UShort")
            NumPut(1, SAFEARRAY, 4, "UShort")
            NumPut(vPIDL, SAFEARRAY, A_PtrSize=8?16:12, "Ptr")
            NumPut(DllCall("shell32\ILGetSize", Ptr,vPIDL, UInt), SAFEARRAY, A_PtrSize=8?24:16, "Int")
            oWin.Navigate2(ComObject(0x2011,&SAFEARRAY))
            DllCall("shell32\ILFree", Ptr,vPIDL)
        }
        break
    }
return