4 options to change the current folder in Windows Explorer

Post a reply


In an effort to prevent automatic submissions, we require that you complete the following challenge.
Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :| :mrgreen: :geek: :ugeek: :arrow: :angel: :clap: :crazy: :eh: :lolno: :problem: :shh: :shifty: :sick: :silent: :think: :thumbup: :thumbdown: :salute: :wave: :wtf: :yawn: :facepalm: :bravo: :dance: :beard: :morebeard: :xmas: :HeHe: :trollface: :cookie: :rainbow: :monkeysee: :monkeysay: :happybday: :headwall: :offtopic: :superhappy: :terms: :beer:
View more smilies

BBCode is ON
[img] is OFF
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: 4 options to change the current folder in Windows Explorer

Re: 4 options to change the current folder in Windows Explorer

by Eureka » 03 Apr 2018, 17:50

JnLlnd wrote:Hi Eureka,

Please read this post and the following:
https://autohotkey.com/boards/viewtopic ... 199#p25199

For me, it still does not work if there is a slash after the hash. My message and example should have been: "This will fail if myPath includes a hash and has a slash after it (as in C:\C#Projects\)."
Ah, I see. Same here ...
That's probably because this method uses the URL syntax. And a hash/pound/number sign has special meaning in that case.
I tried to replace it with it's URL escape character: %23 (*), but in that case the % was replaced by it's URL escape character (%25).
It should be possible to feed a literal % by escaping it in AHK, but I'm not (yet) experienced enough in AHK to get that working.


(*) Drag the foldername to the address bar of your browser to see the converted filename

Re: 4 options to change the current folder in Windows Explorer

by JnLlnd » 03 Apr 2018, 16:44

Hi Eureka,

Please read this post and the following:
https://autohotkey.com/boards/viewtopic ... 199#p25199

For me, it still does not work if there is a slash after the hash. My message and example should have been: "This will fail if myPath includes a hash and has a slash after it (as in C:\C#Projects\)."

Re: 4 options to change the current folder in Windows Explor

by Eureka » 03 Apr 2018, 13:59

Just registered to say THANK YOU!! for all this fundamental research you all have done.
Very valuable, as I''m writing my first AHK (to navigate to folders using Everything, a very reliable and fast file/folder search utility). This thread helps enormously with that :)

BTW:
JnLlnd wrote:Unfortunately, there does not seem to be a solution to this:

Code: Select all

For pExp in ComObjCreate("Shell.Application").Windows
    if (pExp.hwnd = strWinId)
        try pExp.Navigate(myPath)
This will fail if myPath includes a hash (# as in C:\C#Projects).
No (longer?) such problems with AHK 1.1.27.07 @ Win10 1709 (both x64)

Re: 4 options to change the current folder in Windows Explorer

by JnLlnd » 26 Jul 2017, 15:51

Thanks for the references, jeeswg. I used Navigate2 with an integer parameter (for example 17 = My Computer) but I did not know about object parameters. Interesting.

Re: 4 options to change the current folder in Windows Explorer

by jeeswg » 25 Jul 2017, 15:44

[SHParseDisplayName gets the PIDL for the file:]
SHParseDisplayName function (Windows)
https://msdn.microsoft.com/en-us/librar ... s.85).aspx

[PIDLs:]
Introduction to the Shell Namespace (Windows)
https://msdn.microsoft.com/en-us/librar ... s.85).aspx

[This link suggests that Navigate2 expects a SAFEARRAY:]
Explorer Windows Manipulations - Page 5 - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/1903 ... ntry297581

[Navigate2 expects a SAFEARRAY, so we prepare the SAFEARRAY struct:]
SAFEARRAY structure (Automation)
https://msdn.microsoft.com/en-us/librar ... s.85).aspx

Yeah, it's crazy when it just works. Been a long time, this problem. Cheers.

Re: 4 options to change the current folder in Windows Explorer

by JnLlnd » 25 Jul 2017, 08:52

Thanks, jeeswg. Sorry, I have not see it before. It works well. Could you explain what shell32\SHParseDisplayName and the following NumPut are doing?

Re: 4 options to change the current folder in Windows Explorer

by qwerty12 » 13 Jun 2017, 16:00

Interesting... Thanks, jeeswg

Re: 4 options to change the current folder in Windows Explorer

by jeeswg » 13 Jun 2017, 15:54

I have what looks to be a working solution for this, which I've also posted here:

windows - Navigate Shell command not working when the path includes an hash - Stack Overflow
https://stackoverflow.com/questions/228 ... es-an-hash

Code: Select all

;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") ;cDims
			NumPut(1, SAFEARRAY, 4, "UInt") ;cbElements
			NumPut(vPIDL, SAFEARRAY, A_PtrSize=8?16:12, "Ptr") ;pvData
			NumPut(DllCall("shell32\ILGetSize", Ptr,vPIDL, UInt), SAFEARRAY, A_PtrSize=8?24:16, "Int") ;rgsabound[1]
			oWin.Navigate2(ComObject(0x2011,&SAFEARRAY))
			DllCall("shell32\ILFree", Ptr,vPIDL)
		}
		break
	}
return

Re: 4 options to change the current folder in Windows Explorer

by jeeswg » 30 Dec 2016, 00:58

you can navigate from folder 1 to 2 by creating and invoking a lnk file inside folder 1,
if you can't create a lnk in the protected/special folder,
you could navigate to a go-between folder and create the lnk there,
I don't know of a way to check whether the current folder is protected/special or not,
so I could just always use the go-between folder

anyway you've got me hooked on this problem,
I posted some code at the Stack Overflow link

anyway this Navigate instruction, they really made a hash of it

Re: 4 options to change the current folder in Windows Explorer

by jeeswg » 27 Dec 2016, 13:44

OK, unfortunately still a problem if the folder has a # in the first 6 characters of any of the subfolder names. Is it possible to assign a special folder ID to the folder with the hash, and then put that into Navigate()?

e.g. shell:Desktop

[EDIT:]
Or perhaps IShellBrowser::BrowseObject with a PIDL?

Re: 4 options to change the current folder in Windows Explorer

by jeeswg » 27 Dec 2016, 00:32

When I saw that Navigate couldn't handle hash, I was shocked,
but sure enough I replicated the error.
I thought I'd try the short form path just in case. It works!

Code: Select all

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

Re: 4 options to change the current folder in Windows Explor

by JnLlnd » 06 Sep 2014, 22:47

Hum... Interesting. So much work for something that should just be working in the first time... I suppose this is not the first time you have to work that hard around a Windows bug :-)

I adapted my code to use SendInput command with {Raw} option instead of the ControlSetText approach that is now irrelevant. I also use your regex instead of a simple InStr. This will limit the use of the workaround to the cases where there is a \ after the #.

Being on StackOverflow, I would definitively + your answer :-)

Re: 4 options to change the current folder in Windows Explor

by lexikos » 06 Sep 2014, 19:56

So, one limited workaround is to open a base folder and navigate to each sub folder. For example (call this instead of pExp.Navigate()):

Code: Select all

Navigate(Exp, Path)
{
    if !RegExMatch(Path, "^.*#[^\\]*(?=\\|$)", safe_path)
        safe_path := Path
    Exp.Navigate(safe_path)
    rem_path := SubStr(Path, StrLen(safe_path) + 2)  ; Could be "", which is okay
    Loop, Parse, rem_path, \
    {
        while Exp.ReadyState != 4
            Sleep 10
        item := Exp.Document.Folder.Items.Item(A_LoopField)
        if !item || !item.IsFolder
            throw Exception("Folder not found", -1, A_LoopField)
        item.InvokeVerbEx("open")
    }
}
Unfortunately, this won't work as intended if the user has Explorer set to open each folder in a separate window. For that case, you could Run %Path% and close the old window:

Code: Select all

Navigate(Exp, Path)
{
    if RegExMatch(Path, "#.*\\")
    {
        ; Workaround required
        WinGetPos x, y, w, h
        WinClose
        Run %Path%
        WinWait %Path% ahk_exe explorer.exe
        WinMove,,, x, y, w, h
    }
    else
        Exp.Navigate(Path)
}
This would need adjustment for systems without the "Display the full path in the title bar" option enabled.

Although Run %Path% should be reliable, it discards the (back/forward) history. Assuming you already have an Explorer window in the foreground that you want to open the folder in, this approach seems to be fairly reliable:

Code: Select all

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

Re: 4 options to change the current folder in Windows Explor

by lexikos » 06 Sep 2014, 18:47

Apparently it only works if there is no slash after the hash. (Edit: Reading your Stack Overflow post, I see you already figured that out.)
Would it be related to # being a special AHK char?
# is special only for Send and hotkeys. No character in a quoted literal string has special meaning, except ` and ".

When a message box from "Windows Explorer" appears saying that it cannot find "file:///C:/delete#me/", that clearly shows that Windows Explorer is showing the error message, not AutoHotkey, and that Windows Explorer received the proper file path, since it is shown in the error message. One can therefore conclude that the problem lies with Windows Explorer.

Re: 4 options to change the current folder in Windows Explor

by JnLlnd » 05 Sep 2014, 07:23

lexikos wrote:Your Send code fails because # is the modifier symbol for the Win key. You can use {#} or SendRaw c:\delete#me`n (not {Enter}) instead.
Thanks for jumping in Lekikos. Yes, I realized that later in my AHK learning curve :-) . Just forgot to update this thread about it.
lexikos wrote:However, window.Navigate("C:\C#Projects") works just fine for me on Windows 7.
You script also worked for me (after I adapted window.LocationName = "Computer" to my environment. But could you try the following and tell me if Alt-2 is running well for you? For me, it still produced an error. Would it be related to # being a special AHK char?

Code: Select all

; 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%
run, Explorer.exe
return

!1::
!2::
strWinId := WinExist("A")
TrayTip, %myPath1%, %strWinId%
For pExp in ComObjCreate("Shell.Application").Windows
    if (pExp.hwnd = strWinId)
        try pExp.Navigate(A_ThisHotkey = "!1" ? "C:\delete_me\" : "C:\delete#me\")
return

Re: 4 options to change the current folder in Windows Explor

by lexikos » 05 Sep 2014, 05:39

JnLlnd, I came across your posts while searching for something else, and thought I'd look into your issue. However, window.Navigate("C:\C#Projects") works just fine for me on Windows 7. It also works with "file:///C:/C#Projects". Your Send code fails because # is the modifier symbol for the Win key. You can use {#} or SendRaw c:\delete#me`n (not {Enter}) instead.

Code: Select all

; Test script: navigate the "Computer" window to a path containing #.
FileCreateDir C:\C#Projects
for window in ComObjCreate("Shell.Application").Windows
try
    if window.LocationName = "Computer"
        window.Navigate("C:\C#Projects")

Re: 4 options to change the current folder in Windows Explor

by JnLlnd » 23 Apr 2014, 19:14

Trying this with a folder name including # did not work! (EDIT: See foot note)

This works:

Code: Select all

run, Explorer.exe
Sleep, 500
Send, {F4}{Esc}
Sleep, 500
Send, c:\delete_me{Enter}
But this does not work: (EDIT: See foot note)

Code: Select all

run, Explorer.exe
Sleep, 500
Send, {F4}{Esc}
Sleep, 500
Send, c:\delete#me{Enter}
The only difference is the _ replaced by # ! It looks like Windows Explorer really have problem with hash in folder names.

Finally, the only way I found to make this work is:

Code: Select all

run, Explorer.exe
Sleep, 500 ; long delay for safety
strFolder := A_ScriptDir
Send, {F4}{Esc}
Sleep, 500 ; long delay for safety
ControlSetText, Edit1, C:\delete#me, A
ControlSend, Edit1, {Enter}, A
I could not give the rational behind this...

EDIT (2014-09-06): Send, c:\delete#me does not work only because # is a AHK special character when used with Send. The command works if you add the {Raw} option: Send, {Raw}c:\delete#me. See comments from Lexikos belo for more about this.

Re: 4 options to change the current folder in Windows Explor

by JnLlnd » 23 Apr 2014, 16:53

Unfortunately, there does not seem to be a solution to this:

Code: Select all

For pExp in ComObjCreate("Shell.Application").Windows
    if (pExp.hwnd = strWinId)
        try pExp.Navigate(myPath)
This will fail if myPath includes a hash (# as in C:\C#Projects).

The workaround would be to rely on the "second best" approach as identified by the tests in this thread: Explorer_F4Esc. For example (when Explorer is the active window):

Code: Select all

Send, {F4}{Esc}
Sleep, 500 ; long delay for safety
Send, %strFolder%{Enter}

Navigate Shell command not working when the path includes ha

by JnLlnd » 04 Apr 2014, 11:57

Hello!

I'm having problem using the Navigate Shell command when the path include an # sign (search keywords: hashtag, number sign or pound).

I posted the question on StackOverflow:
http://stackoverflow.com/questions/2286 ... es-an-hash

You could reply here or on StackOverflow.

Thanks!

Re: 4 options to change the current folder in Windows Explor

by Azevedo » 09 Feb 2014, 13:56

Yes thanks,

Although I'm building it to myself I do want to be reliable and steady.
So I'm using ControlGetText and ControlSetText. I really do the trick!

Now the problem is with old Open/Save dialog boxes (which have simple combo box to navigate). It won't allow directly changing to a folder that is not in that combo box.

Top