4 options to change the current folder in Windows Explorer

Get help with using AutoHotkey and its commands and hotkeys
User avatar
fischgeek
Posts: 405
Joined: 29 Jan 2014, 21:39

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

07 Feb 2014, 12:42

Works great on 8.1. No modifications.
User avatar
JnLlnd
Posts: 227
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada

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

07 Feb 2014, 18:55

Azevedo wrote:Hi.
It works fine on a regular windows explorer window but it won't work on Open/Save dialogs.
Any help?

Thanks


The Shell.Application COM object is only opening or controling Explorr windows. For dialog boxes, you have to send commands to dialog box control and Send some keys.

See this script and search for NavigateDialog(strPath, strWinId, strClass) close to the end of the script.

Here is an abstract of the code (this will not work by itself but you have the ingredients):

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



See also this thread viewtopic.php?f=5&t=526&start=20#p4673

Good luck!
Author of freeware apps Quick Access Popup (http://www.quickaccesspopup.com),
FoldersPopup and CSV Buddy (http://code.jeanlalonde.ca)
Azevedo
Posts: 81
Joined: 07 Feb 2014, 11:35

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

07 Feb 2014, 19:57

The Shell.Application COM object is only opening or controling Explorr windows. For dialog boxes, you have to send commands to dialog box control and Send some keys.

See this script and search for NavigateDialog(strPath, strWinId, strClass) close to the end of the script.

Here is an abstract of the code (this will not work by itself but you have the ingredients):


Oh I see... well ok, I'll do with the send commands then!

Thanks a lot!
User avatar
JnLlnd
Posts: 227
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada

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

09 Feb 2014, 10:54

Just to make my comment more clear, you can use command like ControlGetText and ControlSetText to save and change the current dialog box folder. The Send key itself is used only to press "Enter". This could be relatively simple. But the longer approach described by Learning One in viewtopic.php?f=5&t=526&start=20#p4673 is much safer.

Of course, if you are doing something for yourself (I mean, you don't plan to distribute your code to other users), you could work this out using only Send and Sleep commands.
Author of freeware apps Quick Access Popup (http://www.quickaccesspopup.com),
FoldersPopup and CSV Buddy (http://code.jeanlalonde.ca)
Azevedo
Posts: 81
Joined: 07 Feb 2014, 11:35

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

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.
User avatar
JnLlnd
Posts: 227
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada

Navigate Shell command not working when the path includes ha

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!
Author of freeware apps Quick Access Popup (http://www.quickaccesspopup.com),
FoldersPopup and CSV Buddy (http://code.jeanlalonde.ca)
User avatar
JnLlnd
Posts: 227
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada

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

23 Apr 2014, 16:53

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

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

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] [Download] (Script.ahk)GeSHi © Codebox Plus

Send, {F4}{Esc}
Sleep, 500 ; long delay for safety
Send, %strFolder%{Enter}
Author of freeware apps Quick Access Popup (http://www.quickaccesspopup.com),
FoldersPopup and CSV Buddy (http://code.jeanlalonde.ca)
User avatar
JnLlnd
Posts: 227
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada

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

23 Apr 2014, 19:14

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

This works:

Code: [Select all] [Download] (Script.ahk)GeSHi © Codebox Plus

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] [Download] (Script.ahk)GeSHi © Codebox Plus

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] [Download] (Script.ahk)GeSHi © Codebox Plus

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.
Last edited by JnLlnd on 06 Sep 2014, 22:23, edited 1 time in total.
Author of freeware apps Quick Access Popup (http://www.quickaccesspopup.com),
FoldersPopup and CSV Buddy (http://code.jeanlalonde.ca)
lexikos
Posts: 5410
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

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

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] [Download] GeSHi © Codebox Plus

; 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")
User avatar
JnLlnd
Posts: 227
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada

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

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] [Expand] [Download] GeSHi © Codebox Plus

Author of freeware apps Quick Access Popup (http://www.quickaccesspopup.com),
FoldersPopup and CSV Buddy (http://code.jeanlalonde.ca)
lexikos
Posts: 5410
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

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

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.
lexikos
Posts: 5410
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

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

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] [Expand] [Download] GeSHi © Codebox Plus


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] [Expand] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

Navigate(Exp, Path)
{
if RegExMatch(Path, "#.*\\")
SendInput {F4}{Esc}{Raw}%Path%`n
else
Exp.Navigate(Path)
}
User avatar
JnLlnd
Posts: 227
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada

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

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 :-)
Author of freeware apps Quick Access Popup (http://www.quickaccesspopup.com),
FoldersPopup and CSV Buddy (http://code.jeanlalonde.ca)
User avatar
jeeswg
Posts: 2232
Joined: 19 Dec 2016, 01:58
Location: UK

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

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] [Download] GeSHi © Codebox Plus

if vDir contains #
Loop, %vDir%, 2, 0 ;(0/1/2=files/both/folders, 0/1=recurse no/yes)
vDir := A_LoopFileShortPath
User avatar
jeeswg
Posts: 2232
Joined: 19 Dec 2016, 01:58
Location: UK

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

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?
Last edited by jeeswg on 27 Feb 2017, 11:31, edited 2 times in total.
User avatar
jeeswg
Posts: 2232
Joined: 19 Dec 2016, 01:58
Location: UK

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

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
User avatar
jeeswg
Posts: 2232
Joined: 19 Dec 2016, 01:58
Location: UK

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

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/22868546/navigate-shell-command-not-working-when-the-path-includes-an-hash

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Last edited by jeeswg on 25 Jul 2017, 15:37, edited 1 time in total.
qwerty12
Posts: 421
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

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

13 Jun 2017, 16:00

Interesting... Thanks, jeeswg
User avatar
JnLlnd
Posts: 227
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada

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

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?
Author of freeware apps Quick Access Popup (http://www.quickaccesspopup.com),
FoldersPopup and CSV Buddy (http://code.jeanlalonde.ca)
User avatar
jeeswg
Posts: 2232
Joined: 19 Dec 2016, 01:58
Location: UK

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

25 Jul 2017, 15:44

[SHParseDisplayName gets the PIDL for the file:]
SHParseDisplayName function (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/bb762236(v=vs.85).aspx

[PIDLs:]
Introduction to the Shell Namespace (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/cc144090(v=vs.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/19039-explorer-windows-manipulations/page-5#entry297581

[Navigate2 expects a SAFEARRAY, so we prepare the SAFEARRAY struct:]
SAFEARRAY structure (Automation)
https://msdn.microsoft.com/en-us/library/windows/desktop/ms221482(v=vs.85).aspx

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

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], sexy69, Yahoo [Bot] and 46 guests