Search: select name w/ biggest number from a group of files

English support forum

Moderators: Hacker, petermad, Stefan2, white

Post Reply
qwbrt
New Member
New Member
Posts: 1
Joined: 2015-05-07, 14:03 UTC

Search: select name w/ biggest number from a group of files

Post by *qwbrt »

I have this list of files (extensions not included):
a1
a12
b4
b145
c8
c11
d0

What I need is for TC to search their filenames, and only select the filename with the biggest number in each of the groups (the a's group being a1, a12, the d's group being d0 etc).
So it should look like:
a12
b145
c11
d0

How do I do this?
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

AutoHotk select name w/ biggest number from a group of files

Post by *Stefan2 »

Hi and welcome qwbrt.

qwbrt wrote:I have this list of files (extensions not included):
a1
a12
b4
b145
c8
c11
d0

What I need is for TC to search their filenames,
and only select the filename with the biggest number in each of the groups
(the a's group being a1, a12, the d's group being d0 etc).

So it should look like:
a12
b145
c11
d0

How do I do this?


Not with build-in functions, but you could use a external script
to do the calculation and then send TC a command to select from a list.

Here is a example with AutoHotkey, hope this work as expected:

How-to:
1.) download and unpack AutoHotkey
2.) set up an button or usercmd.ini entry as noted on top of the script
3.) Fill the "vListOfFileToSelect = " ... beneath of ... ";// U S E R S E T T I N G S:" ... with the list of file names to become selected.
4.) have the folder with your files open in TC
5.) execute this script



Find_andSelect_BiggestNum.ahk

Code: Select all

;//   D E S C R I P T I O N:
;// AHK-script for Total Commander to find and select biggest number from each filename.
;// Stefan, v00.1, 2015-05-08
;// http://ghisler.ch/board/viewtopic.php?p=295812#295812
;=====================================================================================
;// AutoHotkey (AHK) is a free, open-source macro-creation and automation software. (http://ahkscript.org/)
;// Download the executable: http://ahkscript.org/download/  (portable zip, Unicode 32-bit, is just fine)
;// Unpack the archive, e.g. to  "TC-Folder\Tools\AHK" or take care on the path yourself.
;// More Resources / Help / AutoHotkey Community: http://ahkscript.org/#Resources_at_bottom
;// The AutoHotkey Foundation, Our History: http://ahkscript.org/foundation/history.html
;// Note: I still use the old v1.0 version (AutoHotkey1.0.48.03 - May 3, 2009) because it's much smaller.
;//       So it could be that something doesn't work for you, if you use other version then I had used.
;//  ------------------------------------------------------------------------------------
;// Integration into Total Commander:
;//     Save this script with a new name, like DoXYZ.ahk ('.ahk' is mandatory)
;//     Create a new usercmd.ini entry, like ('em_' is mandatory)
;//         [em_RunAHK_DoXYZ]
;//         Cmd=%Commander_Path%\Tools\AHK\AutoHotkey.exe
;//         Para=%Commander_Path%\Tools\AHK\DoXYZ.ahk <perhaps an TC parameter here>
;//            (see Help > Operation > User interface > Button bar > click on "Dialog box to change")
;//     Use this new entry as button command "em_RunAHK_DoX", 
;//     or assign a keyboard shortcut to it (Configuration > Misc.)
;//     Also, you can use this 'cmd' and 'para' commands on an button directly. (right click the button bar)
;=====================================================================================
;//  S C R I P T    S E T T I N G S:
#SingleInstance force
If not WinActive("ahk_class TTOTAL_CMD") {
    MsgBox Please execute from within TC.
    ExitApp
}  
WinGet, vTCHandleID, ID, A
ControlGetFocus, vCurrFocus, ahk_id %vTCHandleID%
;=====================================================================================
;//  U S E R   S E T T I N G S:
;===== get the list:
;// test (for best result use a sorted list!!!):
vListOfFileToSelect =
(
a1
a12
b4
b145
c8
c11
d0 
)
;want to find:
;a12
;b145
;c11
;d0 

;//TODO: read that list from file...
;If (%0% > 0) ;    vListOfFileToSelect=%1%
;
;// TC-command: COPY SELECTED FILENAMES TO CLIPBOARD:
;cm_CopyNamesToClip=2017;Copy filenames to clipboard
;SendMessage, 0x0433, 2017, , , ahk_id%vTCHandleID%
;// GET CLIPBOARD CONTENT:
;vListOfFileToSelect := clipboard
;
;=====================================================================================
;//   T H E    S C R I P T:
;===== Process the list
Loop,parse, vListOfFileToSelect, `n, `r
{
    vRevList := A_LoopField .  "`r`n" . vRevList 
}
Loop,parse, vRevList, `n, `r
{
    vCurrLine := A_LoopField
    if(vCurrLine = "")
        break

    ;split name into first part and trailing number: (.+?)(\d+)
    vCurrLet := RegExReplace(vCurrLine, "(.+?)(\d+)", "$1")
    vCurrNum := RegExReplace(vCurrLine, "(.+?)(\d+)", "$2")

    if(vCurrLet <> vLastLet)
       vNewSelection := vCurrLet . vCurrNum . ".*`r`n"  . vNewSelection
        
    vLastLet=%vCurrLet%
}
;=====================================================================================
;===== Work with our fabricated result
;// PASTE RESULT TO CLIPBOARD:
Clipboard = %vNewSelection%
;// TC-command: LOAD SELECTION FROM CLIPBOARD:
; MsgBox We try to select:`n%vNewSelection%
;cm_LoadSelectionFromClip=2033;Read file selection from clipboard
SendMessage, 0x0433, 2033, , , ahk_id%vTCHandleID%
;=====================================================================================
ExitApp

My button code (select, copy and paste to TCs' button bar)

Code: Select all

TOTALCMD#BAR#DATA
%Commander_Path%\TOOLs\AHK\AutoHotkey.exe %Commander_Path%\TOOLs\AHK\Find_andSelect_BiggestNum.ahk

%Commander_Path%\TOOLs\AHK\AutoHotkey.exe
FindBiggestNum.ahk


-1
Post Reply