Page 1 of 1

Select Folder with exact name

Posted: 25 Sep 2022, 02:14
by arczi_87
Hello,

Sorry if this is too much but I don't know where to start.
I'm trying to write a script that selects a folder based on a selected file name.

In other words, when I choose a file 'name.jpg', the script adds a folder called "name" to the selection. Searching only in the same folder.
The reason for this is to cut and paste both of them to a different location.

Any help really appreciated.

Thank you,
Artur

Re: Select Folder with exact name

Posted: 25 Sep 2022, 05:39
by mikeyww
Welcome to this AutoHotkey forum!

Code: Select all

Run, explorer

#IfWinActive ahk_class CabinetWClass ahk_exe explorer.exe
F3::
file := getSelected().1
SplitPath, file, fn, dir,, fnBare
file := dir "\" fnBare "\" fn
MsgBox, %file%
Return
#IfWinActive

getSelected() { 
 ; Adapted: https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256 by teadrinker
 hwnd := WinExist("A"), selection := []
 WinGetClass, class
 If (class ~= "(Cabinet|Explore)WClass")
  For window in ComObjCreate("Shell.Application").Windows
   If (window.hwnd = hwnd)
    For item in window.document.SelectedItems
     selection.Push(item.Path)
 Return selection
}

Re: Select Folder with exact name

Posted: 25 Sep 2022, 06:07
by arczi_87
Wow, that's a super swift reply!
Thank you for such a warm welcome :)

Re: Select Folder with exact name

Posted: 25 Sep 2022, 08:31
by arczi_87
May I ask for some additional help?

I'm running the script and it opens windows explorer. I'm navigating to my database folder but nothing happens when I click a file (even though, there are folders with the same name).
Do I need to adjust the script, or am I failing to run it properly?
mikeyww wrote:
25 Sep 2022, 05:39
Welcome to this AutoHotkey forum!

Code: Select all

Run, explorer

#IfWinActive ahk_class CabinetWClass ahk_exe explorer.exe
F3::
file := getSelected().1
SplitPath, file, fn, dir,, fnBare
file := dir "\" fnBare "\" fn
MsgBox, %file%
Return
#IfWinActive

getSelected() { 
 ; Adapted: https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256 by teadrinker
 hwnd := WinExist("A"), selection := []
 WinGetClass, class
 If (class ~= "(Cabinet|Explore)WClass")
  For window in ComObjCreate("Shell.Application").Windows
   If (window.hwnd = hwnd)
    For item in window.document.SelectedItems
     selection.Push(item.Path)
 Return selection
}
Thank you,
Artur

Re: Select Folder with exact name

Posted: 25 Sep 2022, 08:39
by mikeyww
This script is triggered by pressing F3. You can try it to verify that it does what you need.

Re: Select Folder with exact name

Posted: 25 Sep 2022, 08:41
by BoBo
It's working fine for me. :think:

Re: Select Folder with exact name

Posted: 25 Sep 2022, 08:44
by mikeyww
Thank you, BoBo.

For a click:

Code: Select all

Run, explorer

#IfWinActive ahk_class CabinetWClass ahk_exe explorer.exe
~LButton::
MouseGetPos,,,, control
If Instr(control, "Tree") {
 ToolTip
 Return
} 
file := getSelected().1
SplitPath, file, fn, dir,, fnBare
file := dir "\" fnBare "\" fn
ToolTip, %file%
Return
#IfWinActive

getSelected() { 
 ; Adapted: https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256 by teadrinker
 hwnd := WinExist("A"), selection := []
 WinGetClass, class
 If (class ~= "(Cabinet|Explore)WClass")
  For window in ComObjCreate("Shell.Application").Windows
   If (window.hwnd = hwnd)
    For item in window.document.SelectedItems
     selection.Push(item.Path)
 Return selection
}

Re: Select Folder with exact name

Posted: 25 Sep 2022, 08:54
by arczi_87
I really appreciate your help, not sure why it's not working.

While using the initial script, I get the messagebox but it doesn't add the folder to the selection.
I've attached an example, do you have an idea what that might be?

Thank you,
Artur

Re: Select Folder with exact name

Posted: 25 Sep 2022, 09:03
by BoBo
...but it doesn't add the folder to the selection.
Do you mean that the selected file should be moved finally to the folder of the same name?

Re: Select Folder with exact name  Topic is solved

Posted: 25 Sep 2022, 09:12
by mikeyww

Code: Select all

Run, explorer

#IfWinActive ahk_class CabinetWClass ahk_exe explorer.exe
~*LButton Up::
MouseGetPos,,,, control
If Instr(control, "Tree") {
 ToolTip
 Return
} 
file := getSelected().1
SplitPath, file,, dir,, fnBare
ToolTip, % dir "\" fnBare
For window in ComObjCreate("Shell.Application").Windows
 If WinActive() = window.hwnd
  Try window.Document.SelectItem(dir "\" fnBare, True)
Return
#IfWinActive

getSelected() { 
 ; Adapted: https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256 by teadrinker
 hwnd := WinExist("A"), selection := []
 WinGetClass, class
 If (class ~= "(Cabinet|Explore)WClass")
  For window in ComObjCreate("Shell.Application").Windows {
   Try window.hwnd
   Catch
    Return
   If (window.hwnd = hwnd)
    For item in window.document.SelectedItems
     selection.Push(item.Path)
  }
 Return selection
}

Re: Select Folder with exact name

Posted: 25 Sep 2022, 09:13
by arczi_87
Maybe I did a poor job describing it, sorry. Hope the image helps

When you click a file (or press F3), you should get the folder selected (if there is one with the same name)

Re: Select Folder with exact name

Posted: 25 Sep 2022, 09:15
by arczi_87
Thank you mikeyww!!!!!!

That works like a charm!!!

Re: Select Folder with exact name

Posted: 25 Sep 2022, 09:17
by mikeyww
OK. I think that my last post (script) works with a click. I also changed the hotkey to have * and Up.

Re: Select Folder with exact name

Posted: 25 Sep 2022, 09:39
by arczi_87
That's amazing! :)

This all looks like wizardry to me and I don't know if that's asking too much, but could I ask you for one more feature?

Could it work if I select more than 1 file? In other words - it should always select a corresponding folder when you click on more and more files. So you end up with 2, 4, 6, 8 selected objects, etc.

I have no idea if that's a big effort, but either way, thank you so much for helping me clean years of negligence of my database :P

Artur

Re: Select Folder with exact name

Posted: 25 Sep 2022, 09:50
by mikeyww
That's what my last script does.

Re: Select Folder with exact name

Posted: 25 Sep 2022, 09:53
by arczi_87
It really does!!! Thank you so much!!

You got the patience of an angel, sorry for being difficult.
Have a wonderful end of the week!