Auto-Select Files (matching NumericalSequence) [AHK]

Discuss and announce Total Commander plugins, addons and other useful tools here, both their usage and their development.

Moderators: white, Hacker, petermad, Stefan2

Post Reply
User avatar
Balderstrom
Power Member
Power Member
Posts: 2148
Joined: 2005-10-11, 10:10 UTC

Auto-Select Files (matching NumericalSequence) [AHK]

Post by *Balderstrom »

Auto-Select Files (matching NumericalSequence) [AHK]
-- Inspired from this thread.

TC_SelectSequence.ahk (version 2.50)

Download:
[Download Directory]
Compiled TC_SelectSequence.exe (with source files) [RAR]


Usage (primary):
Move cursor to a file in a TC panel (Click, Arrows, etc).
Press, Hotkey-combination, or Button.

Files matching the 'numerical sequence' of file will be selected.

TC_SelectSequence automates the tedious part of creating a regex for a group of similiar files.
When you have the files you want selected, you could:
  1. Invert Selection
  2. Save Selection (to file) (so you can resume the selection later)
  3. Show only Selected files.
OR:
  1. Show only Selected files.
The first 3 steps, could be done with TCMC.exe or a very small AHK script, and assigned to an EM_ command or button -- possible as an expansion of this script with a few more line-commands, besides the sole one of /edit, currently.
Command-Line Parameters:
/edit - Functionality is identical to normal Auto-Select, except the script will open up the "Define selection" dialog so you can make further/additional changes to the 'regex' string.

/anynum :: Allows the sequence match to be \d+ instead of \d{#}.
Normally, if you Auto-Select on "File1.txt", only File0.txt thru to File9.txt would get selected. With the /anynum parameter, File1.txt, File33.txt, File999.txt, etc would be matched and selected.

/braces :: Will document this, when I post v2.55 (later today) - as I believe the functionality will change (for better defaults). Or possibly split it into 2 parameters -- but I think more logical defaults will allow it to remain at one optional parameter only.

It primarily deals with what should happen when a number is at the end of a fileName in braces or brackets, e.g. File10(1).txt, File10 (1).txt, File1 (100).txt

Usage examples:
All of the following files would get selected, if the cursor is just on one of the files (and press button or hotkey):
A Set 4 of Files Cateogry 7 001.txt
A Set 4 of Files Cateogry 7 001.txt
A Set 4 of Files Cateogry 7 004.txt
A Set 4 of Files Cateogry 7 006.txt
Or files that are more in line with Digital Cameras:
GEDCX8001.jpg
GEDCX8002.jpg
GEDCX8003.jpg
GEDCX8004.jpg
GEDCX8005.jpg
Explanation:
The code creates a Filter called, TC_SelectSequence
And a regex to match the files, in the cases above:
  • Regex: "^A Set 4 of Files Category 7 \d{3}\.txt$"
  • Regex: "^GEDCX\d{4}\.jpg$"
It also will match if there are non-number characters following the last number sequence: Eg: GEDCX8004__.jpg

Setup:
Create a User-Defined command(s):
em_TCSelectSequence
Command: "C:\Location\of\TC_SelectSequence.exe"
em_TCSelectSequenceEdit
Command: "C:\Location\of\TC_SelectSequence.exe" /edit
Create a TC Hotkey (e.g): Shift+Win+s
  • Assign to em_TCSelectSequence
Create a TC Hotkey (e.g): Ctrl+Shift+Win+s
  • Assign to em_TCSelectSequenceEdit
Additionally, one could add either the FullCommand or the User-Command to a Button, or StartMenu, etc.

UnMerged Script (required #includes are in the Download)

Code: Select all

;;
;;	TC_SelectSequence.ahk
;;	v2.50
;;	Balderstrom, Nov.2010
;;
	#SingleInstance, Force
	#NoEnv
SetBatchLInes, -1
SendMode Input

nArgs=%0%
ReadArgs(nArgs)
return
ReadArgs(nArgs=0)
{
	Loop, % nArgs
	{
		editFindStr:=(editFindStr || %A_Index%=="/edit")
		matchAnyNum:=(matchAnyNum || %A_Index%=="/anynum")
		locateBrace:=(locateBrace || %A_Index%=="/braces")
	}
	IniRead, iniSearch, %A_ScriptDir%\TC_SelectSequence.ini, Config, searches, %A_Space%

	TC_SelectSequence(iniSearch, editFindStr, matchAnyNum, locateBrace)
return
}

ExitNow(key="")
{
	Send, % key ? key : ""
ExitApp
}


TC_SelectSequenceAutoSave()
{
	WinWaitActive, ahk_class TSTDTREEDLG
	ControlSetText, TAltEdit1, TC_SelectSequence, A
	Send, {Enter}
	WinWaitActive, ahk_class #32770,,1
	if( !ErrorLevel )
		Send, {Enter}
	WinWaitActive, ahk_class TCOMBOINPUT
ExitNow("{Enter}")
}


TC_SelectSequence(iniSearch="searches", editFindStr="", matchAnyNum="", locateBrace="")
{
	aPanel:=QueryActivePanelID( cID1, cID2)
	aText:=LB_QueryText(aPanel, cPos:=LB_FindCursorPos( cID1 ))
	ControlGetText, aPath, TMyPanel3, A
	RegExMatch(aPath aText, "(.*)\>(.*\\)?([^\\]+)(\.[a-zA-Z]+) \>?", aFile)
	fileName:=aFile3
	fileExt :=aFile4
	;partPath:=aFile1 "\" aFile2
	;fullPath:=aFile1 "\" aFile2 aFile3 aFile4

	RegExMatch(fileName, "(.*[^ ])[ ]?(([\(\{\[])(\d+)([\)\}\]]))$", sTmp)
	;MsgBox, sTmp1: %sTmp1%_`nsTmp3: %sTmp3%`nsTmp4: %sTmp4%`nsTmp5: %sTmp5%
	if( sTmp2 )
	{
		RegExMatch(sTmp1, "(.*\D)?(\d+)(\D+)?$", split)
		;MsgBox, split1: %split1%`nsplit2: %split2%`nsplit3: %split3%

		if( (s4:=strlen(sTmp4)) > (s2:=strlen(split2)) )
		{
			;; Number in brackets is longer than the prev#
			;; We will use the (001) as the sequenceNumber.
			filter:=RegExReplace(sTmp1, "([\^\$\.\+\{\(\[\]\)\}])", "\$1")
			filter.="[ ]?\" sTmp3 (matchAnyNum ? "\d+\" : "\d{" s4 "}\") sTmp5 "\" fileExt
			filter:="^" filter "$"
		}
		else
		{
			;; Number in brackets, at the end of filename is shorter than the prev#
			;; So if we have enabled /braces, we are only going to match files w/ that
			;; else we will basically ignore the (1) --> ([ ]\(1\))? "optional".
			split1:=RegExReplace(split1, "([\^\$\.\+\{\(\[\]\)\}])", "\$1")
			split2:=(matchAnyNum ? "\d+" : "\d{" s2 "}")
			split3:=RegExReplace(split3, "([\^\$\.\+\{\(\[\]\)\}])", "\$1")
			filter:="^" split1 split2 split3 "([ ]?\" sTmp3 "\d+\" sTmp5 ")"
			filter.=(locateBrace ? "\" : "?\") fileExt "$"
		}
	}
	else
	{
		RegExMatch(fileName, "(.*\D)?(\d+)(\D+)?$", split)
		split1:=RegExReplace(split1, "([\^\$\.\+\{\(\[\]\)\}])", "\$1")
		split2:=(matchAnyNum ? "\d+" : "\d{" strlen(split2) "}")
		split3:=RegExReplace(split3, "([\^\$\.\+\{\(\[\]\)\}])", "\$1")
		filter:="^" split1 split2 split3 
		filter.=(locateBrace ? "([ ]?[\{\[\(]\d+[\)\]\}])?" : "" ) "\" fileExt "$"
	}
	;MsgBox, Final`nfilter: %filter%	
	TC_iniWrite( filter,iniSearch, "TC_SelectSequence_SearchFor"	)
	TC_iniWrite( "",	iniSearch, "TC_SelectSequence_SearchIn"	)
	TC_iniWrite( "",	iniSearch, "TC_SelectSequence_SearchText"	)
	TC_iniWrite( "0|000002001020|||||||||0000| ", iniSearch, "TC_SelectSequence_SearchFlags" )
	PostMessage, 0x433, 521,,, A	; cm_SpreadSelection | Select group
	WinWaitActive, ahk_class TCOMBOINPUT
	ControlSetText, Edit1, >TC_SelectSequence, A
	if( !editFindStr )
		ExitNow("{Enter}")
		
	Send, {Tab}{Tab}{Enter}	;Send, !{d}
	WinWaitActive, ahk_class TFindFile
	aWinDefine:=WinExist("A")
	while( (aWin:=WinActive("A")) == aWinDefine )
	{
		Sleep, 250
	}
	WinGetClass, aClass, ahk_id %aWin%
	if( aClass=="TCOMBOINPUT" )
		ExitNow("{Esc}")
	TC_SelectSequenceAutoSave()
return
}

#include %A_ScriptDir%\Lib\TC.ahk
#include %A_ScriptDir%\Lib\LB.ahk
Future Development Notes & Quirks:
/braces, works in 2.50
Future ideas, a few more command-line parameters:
/invert (selection)
/showselected (only)
/unselect
and
/hide - which would do /invert, /showselect, /unselect

Version History:
2.50
▪ Improved: /braces command-line parameter.
That /braces parameter is (beta), it's defaults will likely change.


2.45
▪ Added: TC_SelectSequence.ini :: if you run a localized (non-english) version of TC.

† Not sure if this is neccessary.

2.40
▪ Removed hotkeys.
▪ Removed English language specifics: should work with any language localization of Total commander.
▪ Provided compiled download, .exe in a .rar


2.35
▪ Added, /braces and /anynum args (line-command parameters)
▪ Rewrote regex handler, should properly find files that have ^$(){}[] in their name (now).


2.21
▪ Added, Enter/NumpadEnter triggers to "Define selection" dialog (to notify script it should Save & Exit.)


2.2
▪ Implemented "/edit" line-command.
▪ Removed commented out/unused Hotkey section.


2.0
▪ Writes to wincmd.ini directly.
▪ Minor regex fix for files that are all numbers. ie 0001.jpg
Last edited by Balderstrom on 2010-11-12, 21:05 UTC, edited 9 times in total.
Rogurt
Junior Member
Junior Member
Posts: 23
Joined: 2009-08-26, 09:26 UTC

Post by *Rogurt »

Hi Balderstrom

I did as advised. Since the german version didn´t show a "user command" in Menue or Help I did it with an entry in the starter menue (which I believe is the same thing as the english "user command").

It works in terms of giving no error and selecting files. But it shows a pop up saying expand selection (german "Auswahl erweitern") which gives two options *.* and TC_SelectSequence. The first choice results in every file selected (not only related file of sequence). The second gives an error that no results were found ("keine passenden Einträge gefunden")

I dont get what I am doing wrong. Does the autohotkey program have to be started by hand to run in background?
User avatar
Balderstrom
Power Member
Power Member
Posts: 2148
Joined: 2005-10-11, 10:10 UTC

Post by *Balderstrom »

I imagine you have unicode characters in your file names. I hadn't considered that...

The fix is easy though, use AutoHotkey_L instead of AutoHotkey. (download is on the main page there)

As well your wincmd.ini OR whatever file you have [searches] redirected to needs to be saved as Unicode --- I imagine in your case, your TC .ini files are already unicode format.

For myself, it was a little bit of trouble-shooting, and attempting UTF8, Unicode, and then AHK_L. I have both installed, I just keep AHK_L under %ProgramFiles%\AutoHotkey_L\AutoHotkey_L.exe

As well, version 2.35 - a couple more /flags line-command arguments:
/edit (added in 2.20)
/anynum (2.35)
/braces (2.35)


Ah, also the code has english titles in it, which if you are running a localized language version of TC, it will hiccup.

Version: 2.36
* Removed all the english titles on the lines that had ahk_class
* changed: Send, !{y} to: Send, {Enter}
* changed: Send, !{d} to: Send, {Tab}{Tab}{Enter}

But, there are 3 buttons in the "Define selection" dialog.
(The same dialog as when you search for files, except it has a different title).
[Save]
[Cancel]
[Help]
I'd need the German for Save and Cancel, and which letter is underlined for Save.

I can't use the Button# for those, as if you select any of the TopTabs, then the Button#'s change. Thus cannot consistently get the correct information without the text.

Also one of the hotkeys (Alt+s), is based on that [Save]
---> !s:: TC_SelectSequenceAutoSave("!{s}")

One question though, is the german wincmd.ini file in english for the [sections] or in german?
as I do a write to the ini file into the "[searches]" section
Rogurt
Junior Member
Junior Member
Posts: 23
Joined: 2009-08-26, 09:26 UTC

Post by *Rogurt »

Hello Balderstrom

thanks for your effort. But right now I just have to get my focus back on the job that made me select image sequences in the first place. Spent too much time now on how to get selection and display faster. Rather done everything by hand I assume...

But I will get back to the topic as soon as I have more time since this sure is a common task.


Thanks again.

Rogurt
User avatar
Balderstrom
Power Member
Power Member
Posts: 2148
Joined: 2005-10-11, 10:10 UTC

Post by *Balderstrom »

v2.40,

* English Language elements removed from the script.
* Should work with any localized version of Total Commander.
* Downloads of compiled script. (packed RAR or EXE)
* Unicode compatible ( compiled with AHK_L )
* Setup instructions in first post.
:: Same as the Script instructions, except one doesn't need to install/use AutoHotkey (pre-setup), and only need to provide the location of the binary file for the user-command/button.

Unfortunately, since it's compiled: it's ~280kb.
Rogurt
Junior Member
Junior Member
Posts: 23
Joined: 2009-08-26, 09:26 UTC

Post by *Rogurt »

Hello Balderstrom

the link to the .exe and .rar seems to be broken (Opera 10.6 and Firefox 3.6. testet)
User avatar
Balderstrom
Power Member
Power Member
Posts: 2148
Joined: 2005-10-11, 10:10 UTC

Post by *Balderstrom »

Updated links, provided direct link to the folder, and the rar. Tested when I wasn't signed into Windows Live as well. Both seem to be working now.

Version 2.45,
Added TC_SelectSequence.ini, (for localized, non-english versions of TC) if it's necessary - I don't know if it is or not.

If your wincmd.ini contains non-english [section] names, then (according to google translate) searches in german is suchen.
if your wincmd.ini doesn't have a [searches] section, then it should have a [suchen] section or similiar.
If such is the case, then in the provided TC_SelectSequence.ini change
"searches=searches" to "searches=suchen"

Otherwise at the moment the provided .ini is not required.
If used, the .ini should be in the same location as TC_SelectSequence.exe

Could probably change that to a line-command/parameter, but I think it is cleaner as an .ini file.
Rogurt
Junior Member
Junior Member
Posts: 23
Joined: 2009-08-26, 09:26 UTC

Re: Auto-Select Files (matching NumericalSequence) [AHK]

Post by *Rogurt »

the download links mentioned in the first post seem not to work anymore. could you plz provide new links?

Cheers
Rogurt
Post Reply