Select Folder with exact name Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
arczi_87
Posts: 8
Joined: 25 Sep 2022, 02:03

Select Folder with exact name

Post by arczi_87 » 25 Sep 2022, 02:14

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

User avatar
mikeyww
Posts: 26849
Joined: 09 Sep 2014, 18:38

Re: Select Folder with exact name

Post by mikeyww » 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
}

arczi_87
Posts: 8
Joined: 25 Sep 2022, 02:03

Re: Select Folder with exact name

Post by arczi_87 » 25 Sep 2022, 06:07

Wow, that's a super swift reply!
Thank you for such a warm welcome :)

arczi_87
Posts: 8
Joined: 25 Sep 2022, 02:03

Re: Select Folder with exact name

Post by arczi_87 » 25 Sep 2022, 08:31

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

User avatar
mikeyww
Posts: 26849
Joined: 09 Sep 2014, 18:38

Re: Select Folder with exact name

Post by mikeyww » 25 Sep 2022, 08:39

This script is triggered by pressing F3. You can try it to verify that it does what you need.
Last edited by mikeyww on 25 Sep 2022, 08:44, edited 2 times in total.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Select Folder with exact name

Post by BoBo » 25 Sep 2022, 08:41

It's working fine for me. :think:

User avatar
mikeyww
Posts: 26849
Joined: 09 Sep 2014, 18:38

Re: Select Folder with exact name

Post by mikeyww » 25 Sep 2022, 08:44

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
}

arczi_87
Posts: 8
Joined: 25 Sep 2022, 02:03

Re: Select Folder with exact name

Post by arczi_87 » 25 Sep 2022, 08:54

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
Attachments
screen.PNG
screen.PNG (39.62 KiB) Viewed 742 times

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Select Folder with exact name

Post by BoBo » 25 Sep 2022, 09:03

...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?

User avatar
mikeyww
Posts: 26849
Joined: 09 Sep 2014, 18:38

Re: Select Folder with exact name  Topic is solved

Post by mikeyww » 25 Sep 2022, 09:12

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
}
Last edited by mikeyww on 25 Sep 2022, 09:20, edited 1 time in total.

arczi_87
Posts: 8
Joined: 25 Sep 2022, 02:03

Re: Select Folder with exact name

Post by arczi_87 » 25 Sep 2022, 09:13

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)
Attachments
screen2.PNG
screen2.PNG (44.99 KiB) Viewed 716 times

arczi_87
Posts: 8
Joined: 25 Sep 2022, 02:03

Re: Select Folder with exact name

Post by arczi_87 » 25 Sep 2022, 09:15

Thank you mikeyww!!!!!!

That works like a charm!!!

User avatar
mikeyww
Posts: 26849
Joined: 09 Sep 2014, 18:38

Re: Select Folder with exact name

Post by mikeyww » 25 Sep 2022, 09:17

OK. I think that my last post (script) works with a click. I also changed the hotkey to have * and Up.

arczi_87
Posts: 8
Joined: 25 Sep 2022, 02:03

Re: Select Folder with exact name

Post by arczi_87 » 25 Sep 2022, 09:39

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

User avatar
mikeyww
Posts: 26849
Joined: 09 Sep 2014, 18:38

Re: Select Folder with exact name

Post by mikeyww » 25 Sep 2022, 09:50

That's what my last script does.

arczi_87
Posts: 8
Joined: 25 Sep 2022, 02:03

Re: Select Folder with exact name

Post by arczi_87 » 25 Sep 2022, 09:53

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!

Post Reply

Return to “Ask for Help (v1)”