Working with network folders that share the same name Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
paul1965
Posts: 25
Joined: 27 Feb 2021, 08:06

Working with network folders that share the same name

Post by paul1965 » 28 Feb 2021, 17:21

Hi. I just started using AHK yesterday to write a few scripts, and so far I'm very impressed and happy with it. One issue I've run into today is how to handle writing a script that opens at least two folders on a networked PC that have the same name as two folders already open on the host PC that's running the script (folder names are Downloads and Torrents). Obviously I'd prefer to not have to rename the folders on either PC, any suggestions on what can be done? Right now the way the script is written, it opens the host Download folder, scrolls to the bottom, then minimizes it. Once it gets to the networked Download folder, it opens it, but then activates the host PC folder instead of the networked PC folder. I've included a snippet of the code, sorry if it's not in the correct format. I did do a search, but maybe I didn't use the right keywords as I didn't find anything matching my issue.

Code: Select all

Host PC:
Run F:\Downloads ;
WinActivate , Downloads ;
WinWaitActive , Downloads ;
Sleep , 1000
WinMinimize , Downloads ;
Sleep , 2500 ;

Network PC:
Run \\Torrent\Downloads ; 
WinActivate , Downloads ;
WinWaitActive , Downloads ;
Send , {END} ;
Sleep , 1000
WinMinimize , Downloads ;
[Mod edit: [code][/code] tags added.]
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Working with network folders that share the same name

Post by BoBo » 28 Feb 2021, 17:41

a) you could be more specific :arrow: SetTitleMatchMode
b) prevent any window-based activity (no idea what you wanna do afterward) and use 'command line' related instead of 'manual' events.
User avatar
mikeyww
Posts: 26944
Joined: 09 Sep 2014, 18:38

Re: Working with network folders that share the same name  Topic is solved

Post by mikeyww » 28 Feb 2021, 18:10

Code: Select all

wTitle = ahk_class CabinetWClass ahk_exe explorer.exe
WinMinimize, %wTitle%
Run, F:\Downloads
WinWaitActive, %wTitle%,, 10
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the window.
Else WinMinimize
Run, \\Torrent\Downloads
WinWaitActive, %wTitle%,, 10
If !ErrorLevel {
 Send {End}
 WinMinimize
} Else MsgBox, 48, Error, An error occurred while waiting for the window.
If you want to send End to the first window, then you can move the Send command.
paul1965
Posts: 25
Joined: 27 Feb 2021, 08:06

Re: Working with network folders that share the same name

Post by paul1965 » 28 Feb 2021, 18:50

Thank you both for the suggestions. @mikeyww I tried your code, but ran into the same problem. AHK will open both Download folders, but now refuses to minimize either one (and displays the error message). I was going to switch to trying to activate by pid for the network Download folder, but it appears that changes each time the network folder is opened.
Last edited by paul1965 on 28 Feb 2021, 19:30, edited 1 time in total.
WatsonEnterprises
Posts: 19
Joined: 25 May 2020, 23:04

Re: Working with network folders that share the same name

Post by WatsonEnterprises » 28 Feb 2021, 18:54

You want to distinguish between two explorer windows both titled "Downloads", but they have different paths? (F:\Downloads vs \\NetworkSomething\Downloads?). It's possible to get the explorer path from their HWNDs/windowIDs, which would let you tell them apart. Then use WinActivate on "ahk_id someID" instead of "Downloads".

I would do something like this.

Code: Select all

titleToMatch := "Downloads"
winIDsForallWindowsTitledDownloads := winIDsForAllMatchingWindows(titleToMatch)

msgbox % "Found " winIDsForallWindowsTitledDownloads.Length() " matching windows"
for _, windowID in winIDsForallWindowsTitledDownloads {
	explorerPath := PathOfExplorerWindow(windowID)
	
	msgbox, , , % "I'm about to activate the window for`r`n" explorerPath, 2
	WinActivate, % "ahk_id " windowID
}
return



winIDsForAllMatchingWindows(titleToMatch){
	WinGet, IDs, List, % titleToMatch
	winIDs := Array()
	Loop, % IDs
		winIDs.push( IDs%A_Index% )
	return winIDs
}

PathOfExplorerWindow(windowID){
	path := ShellWindowFor(windowID).Document.Folder.Self.Path
	return path
}
ShellWindowFor(matchThisHWND){
	allShellWindows := ComObjCreate("Shell.Application").Windows()
	for oWin in allShellWindows
		if (oWin.HWND = matchThisHWND)
			return oWin
}


Code: Select all

F1::ActivateExplorerWindowThatMatchesPath("C:\path to my downloads")
F2::ActivateExplorerWindowThatMatchesPath("D:\some other path")

ActivateExplorerWindowThatMatchesPath(path_I_want){
	for _, windowID in winIDsForAllMatchingWindows("ahk_class CabinetWClass")	;all explorer windows
		if (PathOfExplorerWindow(windowID) = path_I_want)
			WinActivate, % "ahk_id " windowID
}
paul1965
Posts: 25
Joined: 27 Feb 2021, 08:06

Re: Working with network folders that share the same name

Post by paul1965 » 28 Feb 2021, 19:12

Actually, @mikeyww has the right code. When I ran it by itself, both Download folders were launched then minimized. I had some issues merging his code into mine, hence the initial failure. I've now corrected my errors and have run the full script again, and everything appears to look good. I've got a few other hiccups to fix with some network path names, but I think I should be in good shape now. Thanks everyone for the quick and professional replies!
User avatar
mikeyww
Posts: 26944
Joined: 09 Sep 2014, 18:38

Re: Working with network folders that share the same name

Post by mikeyww » 28 Feb 2021, 19:39

If you need the HWND, it is also straightforward:

Code: Select all

wTitle = ahk_class CabinetWClass ahk_exe explorer.exe
Run, \\Torrent\Downloads
WinWaitActive, %wTitle%,, 10
uid := WinActive("A")
MsgBox, %uid%
paul1965
Posts: 25
Joined: 27 Feb 2021, 08:06

Re: Working with network folders that share the same name

Post by paul1965 » 28 Feb 2021, 20:10

mikeyww wrote:
28 Feb 2021, 19:39
If you need the HWND, it is also straightforward:
@mikeyww I'm good with your first bit of code. The script opens the 26 folders I need it to, on the correct monitors, scrolls to the bottom of each, then minimizes the window. I'll create a smaller version to run on my Torrent PC and I'll be all set. Sure beats doing it manually like I have for years!

Thanks again to everyone who responded :+1:
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Working with network folders that share the same name

Post by BoBo » 01 Mar 2021, 06:35

Could have been even easier. Windows Explorer provides the option "show full path in title" (or similar), so that's specific enough.
User avatar
mikeyww
Posts: 26944
Joined: 09 Sep 2014, 18:38

Re: Working with network folders that share the same name

Post by mikeyww » 01 Mar 2021, 06:41

Thanks, @BoBo!
Post Reply

Return to “Ask for Help (v1)”