How to swap the filenames of selected files in a window
How to swap the filenames of selected files in a window
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!
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
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
}
Last edited by Smile_ on 10 Dec 2022, 14:34, edited 1 time in total.
Re: How to swap the filenames of selected files in a window
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
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
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?Smile_ wrote: ↑09 Dec 2022, 12:19Press 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
Ctrl + Alt + S not Ctrl + S.