How to navigate to next sibling folder from Windows Explorer?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
kai
Posts: 18
Joined: 30 Aug 2017, 01:31

How to navigate to next sibling folder from Windows Explorer?

19 Nov 2017, 04:05

I am searching for a way to navigate from folder A to folder B from inside folder A:

- Top Folder
-- A (I am here)
-- B

AHK-Shortcut, then:

- Top Folder
-- A
-- B (now I am here)

Can somebody help?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to navigate to next sibling folder from Windows Explorer?

19 Nov 2017, 10:35

Here's a way. I've been using something like this for a while, I just tidied it up now for sharing. I wonder, how many people around the world have been using a handy 'navigate to sibling folder' method before now.

Code: Select all

;based on:
;Explorer window interaction (folder windows/Desktop, file/folder enumeration/selection/navigation/creation) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=35041

#IfWinActive, ahk_class CabinetWClass
^PgUp:: ;explorer - navigate to sibling folder
^PgDn:: ;explorer - navigate to sibling folder
#IfWinActive, ahk_class ExploreWClass
^PgUp:: ;explorer - navigate to sibling folder
^PgDn:: ;explorer - navigate to sibling folder
vGetNext := !!InStr(A_ThisHotkey, "Dn")
WinGet, hWnd, ID, A
WinGetClass, vWinClass, % "ahk_id " hWnd
if !(vWinClass = "CabinetWClass") && !(vWinClass = "ExploreWClass")
	return
for oWin2 in ComObjCreate("Shell.Application").Windows
	if (oWin2.HWND = hWnd)
	{
		vDir2 := oWin2.Document.Folder.Self.Path
		vDir2 := RTrim(vDir2, "\")
		oWin := oWin2
		break
	}
oWin2 := ""
if !FileExist(vDir2)
{
	oWin := ""
	return
}
SplitPath, vDir2, vName1, vDir1
vIsMatch := 0
vName := ""
Loop, Files, % vDir1 "\*", D
{
	if vIsMatch
	{
		vName := A_LoopFileName
		break
	}
	if (A_LoopFileName = vName1)
		if vGetNext
			vIsMatch := 1
		else
			break
	vName := A_LoopFileName
}
if (vName = vName1) || (vName = "")
{
	oWin := ""
	return
}
vDir := vDir1 "\" vName
;MsgBox, % vDir

if !InStr(vDir, "#") ;folders that don't contain #
	oWin.Navigate(vDir)
else ;folders that contain #
{
	DllCall("shell32\SHParseDisplayName", WStr,vDir, Ptr,0, PtrP,vPIDL, UInt,0, Ptr,0)
	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), 0)
	DllCall("shell32\ILFree", Ptr,vPIDL)
}
oWin := ""
return
#IfWinActive
Last edited by jeeswg on 06 Mar 2018, 14:50, edited 2 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
teadrinker
Posts: 4325
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to navigate to next sibling folder from Windows Explorer?

19 Nov 2017, 15:35

It's a bit confusing. :)
For me this works:

Code: Select all

#If WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass")
^PgUp::
^PgDn:: NavigateToSiblingDir( InStr(A_ThisHotkey, "Up") )

NavigateToSiblingDir(UpDown)  {
   oShell := ComObjCreate("Shell.Application")
   WinGet, hWnd,, A
   for oWin in oShell.Windows  {
      if (hWnd = oWin.hwnd)  {
         oFolder := oWin.Document.Folder
         startDirPath  := oFolder.Self.Path
         parentDirPath := oFolder.ParentFolder.Self.Path
         break
      }
   }
   
   for item in oShell.Namespace(parentDirPath).Items  {
      if !item.IsFolder
         continue
      if (found && nextSiblingPath := item.Path)
         break
      if (item.Path = startDirPath && found := true)
         prevSiblingPath := prev
      prev := item.Path
   }
   
   if (UpDown && prevSiblingPath)
      oWin.Navigate(prevSiblingPath)
   if (!UpDown && nextSiblingPath)
      oWin.Navigate(nextSiblingPath)
}
kai
Posts: 18
Joined: 30 Aug 2017, 01:31

Re: How to navigate to next sibling folder from Windows Explorer?

20 Nov 2017, 01:37

Thanks both of you!!

I tried both scripts and they work great. THIS is awesomeness!!

Note (bug): Both scripts do not jump in folders with a underline as it seems, e.g. "_files"

@teadrinker's script: Nicely short!

It is so handy and saves soooo much time! Gosh, unbelievable that I never used this in my life.

@jeeswg: Everybody should use it! It is such a simple feature but so powerful.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to navigate to next sibling folder from Windows Explorer?

20 Nov 2017, 10:14

- On testing, I didn't see any problems with folders that contain underscore. However you can get different sort orders relating to underscore i.e. Explorer displays folders in one order, the file loop retrieves them in another.
- In this list of characters sorted by their Unicode number:
@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
`abcdefghijklmnopqrstuvwxyz{|}~
To do a case insensitive comparison, you either convert to lowercase or uppercase, and then you do a case sensitive comparison. If you convert to uppercase as the intermediary stage, then letters are always before underscore (e.g. file loop), if you convert to lowercase as the intermediary stage, then letters are always after underscore (e.g. Explorer windows). E.g. create a folder called 'a' and a folder called '_', and compare where they appear in an Explorer window, and in a file loop.
- My script is longer because I included some code to handle folders that contain #, which you could also use in teadrinker's script.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
teadrinker
Posts: 4325
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to navigate to next sibling folder from Windows Explorer?

20 Nov 2017, 10:34

jeeswg wrote:I included some code to handle folders that contain #
I tested my script with folders called like "#files", it works. Do I misunderstand something?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to navigate to next sibling folder from Windows Explorer?

20 Nov 2017, 10:40

kai mentioned _, I mentioned #.

Re. #:
4 options to change the current folder in Windows Explorer - Page 3 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 161#p16161
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to navigate to next sibling folder from Windows Explorer?

20 Nov 2017, 11:15

Your link is to reformatcode.com? What is that, a clone of the Stack Overflow link? Is the link safe?

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

Btw vafylec's Navigate2 solution is the same as mine, our code is often very similar.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
teadrinker
Posts: 4325
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to navigate to next sibling folder from Windows Explorer?

20 Nov 2017, 13:04

Ok, but your approach opens a new window when navigates to folder which contains "#". Some improvements:

Code: Select all

#If WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass")
^PgUp::
^PgDn:: NavigateToSiblingDir( InStr(A_ThisHotkey, "Up") )

NavigateToSiblingDir(UpDown)  {
   oShell := ComObjCreate("Shell.Application")
   WinGet, hWnd,, A
   for oWin in oShell.Windows  {
      if (hWnd = oWin.hwnd)  {
         oFolder := oWin.Document.Folder
         startDirPath  := oFolder.Self.Path
         parentDirPath := oFolder.ParentFolder.Self.Path
         break
      }
   }
   
   for item in oShell.Namespace(parentDirPath).Items  {
      if !item.IsFolder
         continue
      if (found && nextSiblingPath := item.Path)
         break
      if (item.Path = startDirPath && found := true)
         prevSiblingPath := prev
      prev := item.Path
   }
   
   ( (UpDown && (navigatePath := prevSiblingPath)) || (!UpDown && (navigatePath := nextSiblingPath)) )
   if navigatePath  {
      DllCall("shell32\SHParseDisplayName", WStr, navigatePath, Ptr,0, PtrP,vPIDL, UInt,0, Ptr,0)
      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), navVirtualTab := 14)
      DllCall("shell32\ILFree", Ptr,vPIDL)
   }
}
Last edited by teadrinker on 20 Nov 2017, 14:42, edited 1 time in total.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to navigate to next sibling folder from Windows Explorer?

20 Nov 2017, 13:09

Which OS do you use? Did you change the Explorer settings? My script does not open any new windows AFAIK (tested on Windows 7).

So what have you changed, you've added navVirtualTab := 14 to Navigate2, which seems like a good idea. Anything else?
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
teadrinker
Posts: 4325
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to navigate to next sibling folder from Windows Explorer?

20 Nov 2017, 13:29

Tested on Windows 7 too. My folder path is:

Image

When I try to navigate between those folders, your script always opens a new window for me. Of course IE settings were changed for a long time using, but I don't know what exactly causes such behavior.
Anything else?
I thought, if navigate method not always works, it makes sense using navigate2 in any case.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to navigate to next sibling folder from Windows Explorer?

26 Nov 2017, 00:48

@teadrinker:
- You specified 'navVirtualTab := 14', which is 0xE. Where did you get that figure from? Because according to ExDisp.h and:
BrowserNavConstants Enumeration
https://msdn.microsoft.com/en-us/librar ... s.85).aspx

Code: Select all

;navNoWriteToCache := 0x8
;navNoReadFromCache := 0x4
;navNoHistory := 0x2
2|4|8 = 14 = 0xE

;navVirtualTab := 0x4000
- Could you try that script again, but specifying 0 or 2 or 4 or 8 or combinations, to see if you don't actually need 14. Thanks.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
teadrinker
Posts: 4325
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to navigate to next sibling folder from Windows Explorer?

26 Nov 2017, 13:21

Hi, jeeswg. Yes, you are right, navVirtualTab is not 14. I looked constants here and I thought for some reason that it is a 0-based enumerator: navOpenInNewWindow = 0, navNoHistory = 1 and so on.

Now I've tested the script once again and found out that if I don't specify any flags, the script does open new windows, but if I specify 0 (as well as 2, 4, or 8), the script does not.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to navigate to next sibling folder from Windows Explorer?

26 Nov 2017, 14:55

@teadrinker:
Many thanks, I'll change my script from:
oWin.Navigate2(ComObject(0x2011, &SAFEARRAY))
to
oWin.Navigate2(ComObject(0x2011, &SAFEARRAY), 0)
Plus, it's good to know where the number came from.
Btw it's odd these links, one has the constants, one doesn't. Hmm. :think:
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
JoPo
Posts: 25
Joined: 04 Feb 2018, 09:03

Re: How to navigate to next sibling folder from Windows Explorer?

06 Mar 2018, 08:37

Hi !
I bring back this thread with this useful script because I'd like to add a little bit different behavior : I wish that if the parent folder is alone (so there is no down or up folder to go, for the moment, in this case, nothing happens) , it would go to its parent untill it finds the first parent folder where there are other (up or down, depending if user choose to go up or down) folder. :?

And I must confess & admit that I don't find any solution ! :roll: :crazy: I haven't got yet enough knowledge about Autohotkey to achieve this. But I like to learn ! Some help would be very very appreciate ! Thanks a lot to any help ! :)
> > > > > > > > > > > > --- Musica --> here ! ---< < < < < < < < < < < <
teadrinker
Posts: 4325
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to navigate to next sibling folder from Windows Explorer?

06 Mar 2018, 14:29

Hi, like this?

Code: Select all

#If WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass")
^PgUp::
^PgDn:: NavigateToSiblingDir( InStr(A_ThisHotkey, "Up") )

NavigateToSiblingDir(UpDown)  {
   oShell := ComObjCreate("Shell.Application")
   WinGet, hWnd,, A
   for oWin in oShell.Windows  {
      if (hWnd = oWin.hwnd)  {
         oFolder := oWin.Document.Folder
         startDirPath  := oFolder.Self.Path
         parentDirPath := oFolder.ParentFolder.Self.Path
         break
      }
   }
   
   for item in oShell.Namespace(parentDirPath).Items  {
      if !item.IsFolder
         continue
      if (found && nextSiblingPath := item.Path)
         break
      if (item.Path = startDirPath && found := true)
         prevSiblingPath := prev
      prev := item.Path
   }
   
   if !(prevSiblingPath || nextSiblingPath)
      prevSiblingPath := nextSiblingPath := oFolder.ParentFolder.Self.Path
   
   ( (UpDown && (navigatePath := prevSiblingPath)) || (!UpDown && (navigatePath := nextSiblingPath)) )
   if navigatePath  {
      DllCall("shell32\SHParseDisplayName", WStr, navigatePath, Ptr,0, PtrP,vPIDL, UInt,0, Ptr,0)
      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), 0)
      DllCall("shell32\ILFree", Ptr,vPIDL)
   }
}
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to navigate to next sibling folder from Windows Explorer?

06 Mar 2018, 14:54

@teadrinker: Could you explain what your script does? Thanks.
@JoPo: Could you try explaining that again? Thanks.
E.g. give examples of source and destination folders.
Also, do you want two hotkeys in total, an up action and a down action?
I have a script here that may be related, it navigates up to the parent folder, or down to the child folder (if there is only one child folder). The hotkeys are Ctrl+Win+PgUp and Ctrl+Win+PgDn.

Code: Select all

;based on:
;Explorer window interaction (folder windows/Desktop, file/folder enumeration/selection/navigation/creation) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=35041

#If WinActive("ahk_class CabinetWClass")
|| WinActive("ahk_class ExploreWClass")
^#PgUp:: ;explorer - navigate to parent folder
^#PgDn:: ;explorer - navigate to child folder (if only one child)
WinGet, hWnd, ID, A
WinGetClass, vWinClass, % "ahk_id " hWnd
if !(vWinClass = "CabinetWClass") && !(vWinClass = "ExploreWClass")
	return
for oWin2 in ComObjCreate("Shell.Application").Windows
	if (oWin2.HWND = hWnd)
	{
		vDir2 := oWin2.Document.Folder.Self.Path
		vDir2 := RTrim(vDir2, "\")
		oWin := oWin2
		break
	}
oWin2 := ""
;MsgBox, % vDir2
if InStr(A_ThisHotkey, "Up")
{
	if InStr(vDir2, "\")
		SplitPath, vDir2,, vDir
	else
	{
		oWin := ""
		return
	}
}
else
{
	Loop, Files, % vDir2 "\*", D
	{
		vDir := A_LoopFileFullPath
		if (A_Index = 2)
		{
			oWin := ""
			return
		}
	}
}

if !InStr(vDir, "#") ;folders that don't contain #
	oWin.Navigate(vDir)
else ;folders that contain #
{
	DllCall("shell32\SHParseDisplayName", WStr,vDir, Ptr,0, PtrP,vPIDL, UInt,0, Ptr,0)
	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), 0)
	DllCall("shell32\ILFree", Ptr,vPIDL)
}
oWin := ""
return
#If
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
teadrinker
Posts: 4325
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to navigate to next sibling folder from Windows Explorer?

06 Mar 2018, 15:19

jeeswg wrote:@teadrinker: Could you explain what your script does?
If the current folder has no sibling folders, the script navigates to the parent folder.
JoPo
Posts: 25
Joined: 04 Feb 2018, 09:03

Re: How to navigate to next sibling folder from Windows Explorer?

06 Mar 2018, 16:07

The nice script at the beginning of this thread allows you to jump from the folder you're in windows browser to the next one just by control + page down. If the parent folder is alone = doesn't have folder before nor after, you stay in the same folder.

Whet I would like, is a script that would allow me to go int the previous / next folder, if the parent folder is alone, go in the next parent folder and repeat the same untill to find a folder amonst others and directly going into it.

Thanks, Teadrinker ! But the new script send me just in the parent folder (in the case of a lonely folder, without sibling folder) and stops. I'm totally unable to explain you why ! :mrgreen:
I gonna add some difficulty... Eh eh... If the script would allow me to do what I explained + going in the next / previous sibling folder untill it reaches the latest folder (in which there are only files = no more folder to continue to go in) , it would be like heaven !! :o
> > > > > > > > > > > > --- Musica --> here ! ---< < < < < < < < < < < <

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: haomingchen1998 and 244 guests