Page 1 of 1
How to swap the filenames of selected files in a window
Posted: 09 Dec 2022, 09:42
by alsheron
To swap filenames of two selected files I previously used SwapEm but it's buggy and sometimes fails and/or corrupts files.
I can see AutoHotkey would be a great way of achieving this by writing the script and compiling to an exe I can then add to the right click menu but I'm a bit stuck in figuring out how to do something that's probably fairly simple. I've searched the forums and Googled to try to figure it out but don't know where to start.
How do I go about getting the currently selected filenames (there should only be 2 selected) and then swapping the filenames?
Are there any scripts that already do this?
Many thanks!
Re: How to swap the filenames of selected files in a window
Posted: 09 Dec 2022, 10:01
by ahk7
Re: How to swap the filenames of selected files in a window
Posted: 09 Dec 2022, 12:19
by Smile_
Press
Ctrl +
Alt +
S after two files selection.
Code: Select all
^!s::
Selection := StrSplit(Explorer_GetSelection(), "`n")
If (Selection.Length() = 2) {
FileMove, % Selection[1], % Selection[1] (I := 1)
While (ErrorLevel) {
FileMove, % Selection[1], % Selection[1] (I := A_Index + 1)
}
FileMove, % Selection[2], % Selection[1]
FileMove, % Selection[1] I, % Selection[2]
Msgbox, OK
}
Return
^!x::ExitApp
Explorer_GetSelection() {
WinGetClass, winClass, % "ahk_id" . hWnd := WinExist("A")
if !(winClass ~="Progman|WorkerW|(Cabinet|Explore)WClass")
Return
shellWindows := ComObjCreate("Shell.Application").Windows
if (winClass ~= "Progman|WorkerW")
shellFolderView := shellWindows.FindWindowSW(0, 0, SWC_DESKTOP := 8, 0, SWFO_NEEDDISPATCH := 1).Document
else {
for window in shellWindows
if (hWnd = window.HWND) && (shellFolderView := window.Document)
break
}
for item in shellFolderView.SelectedItems
result .= (result = "" ? "" : "`n") . item.Path
if !result
result := shellFolderView.Folder.Self.Path
Return result
}
Re: How to swap the filenames of selected files in a window
Posted: 09 Dec 2022, 18:12
by wetware05
hi.
A very fine job, Smile_
For what it's worth, from here they refer to two alternative mini-programs a Swap'em
https://www.portablefreeware.com/index.php?id=2052
Re: How to swap the filenames of selected files in a window
Posted: 09 Dec 2022, 18:18
by Smile_
Re: How to swap the filenames of selected files in a window
Posted: 13 Dec 2022, 13:27
by alsheron
Smile_ wrote: ↑09 Dec 2022, 12:19
Press
Ctrl +
Alt +
S after two files selection.
Code: Select all
^!s::
Selection := StrSplit(Explorer_GetSelection(), "`n")
If (Selection.Length() = 2) {
FileMove, % Selection[1], % Selection[1] (I := 1)
While (ErrorLevel) {
FileMove, % Selection[1], % Selection[1] (I := A_Index + 1)
}
FileMove, % Selection[2], % Selection[1]
FileMove, % Selection[1] I, % Selection[2]
Msgbox, OK
}
Return
^!x::ExitApp
Explorer_GetSelection() {
WinGetClass, winClass, % "ahk_id" . hWnd := WinExist("A")
if !(winClass ~="Progman|WorkerW|(Cabinet|Explore)WClass")
Return
shellWindows := ComObjCreate("Shell.Application").Windows
if (winClass ~= "Progman|WorkerW")
shellFolderView := shellWindows.FindWindowSW(0, 0, SWC_DESKTOP := 8, 0, SWFO_NEEDDISPATCH := 1).Document
else {
for window in shellWindows
if (hWnd = window.HWND) && (shellFolderView := window.Document)
break
}
for item in shellFolderView.SelectedItems
result .= (result = "" ? "" : "`n") . item.Path
if !result
result := shellFolderView.Folder.Self.Path
Return result
}
Thanks! Really appreciate you doing this. I've pasted this into a new AHK script file and run it but when I use the Ctrl S keys after selecting two files in Explorer it doesn't appear to do anything I can see. I'm clearly missing something obvious, sorry. Do I keep the default lines (#NoEnv... etc) in the new AHK script file or is there something else I might have missed?
Re: How to swap the filenames of selected files in a window
Posted: 13 Dec 2022, 14:26
by Smile_
Ctrl + Alt + S not Ctrl + S.
Re: How to swap the filenames of selected files in a window
Posted: 14 Dec 2022, 04:48
by alsheron
Smile_ wrote: ↑13 Dec 2022, 14:26
Ctrl +
Alt +
S not
Ctrl +
S.
Ah! I knew I'd missed something obvious. Works a charm! Thanks again.