How to swap the filenames of selected files in a window

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
alsheron
Posts: 3
Joined: 09 Dec 2022, 09:29

How to swap the filenames of selected files in a window

Post by alsheron » 09 Dec 2022, 09:42

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!


User avatar
Smile_
Posts: 858
Joined: 03 May 2020, 00:51

Re: How to swap the filenames of selected files in a window

Post by Smile_ » 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
}
Last edited by Smile_ on 10 Dec 2022, 14:34, edited 1 time in total.

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: How to swap the filenames of selected files in a window

Post by wetware05 » 09 Dec 2022, 18:12

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


alsheron
Posts: 3
Joined: 09 Dec 2022, 09:29

Re: How to swap the filenames of selected files in a window

Post by alsheron » 13 Dec 2022, 13:27

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?

User avatar
Smile_
Posts: 858
Joined: 03 May 2020, 00:51

Re: How to swap the filenames of selected files in a window

Post by Smile_ » 13 Dec 2022, 14:26

Ctrl + Alt + S not Ctrl + S.

alsheron
Posts: 3
Joined: 09 Dec 2022, 09:29

Re: How to swap the filenames of selected files in a window

Post by alsheron » 14 Dec 2022, 04:48

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.

Post Reply

Return to “Ask for Help (v1)”