Sort Message Filters

Discussion of general topics about Mozilla Thunderbird
Locked
johnmsch
Posts: 164
Joined: April 20th, 2004, 10:30 am
Location: Marietta, GA

Sort Message Filters

Post by johnmsch »

For those of you that have lots and lots of message filters, here is an AutoHotkey script to sort them alphabetically. Once I got a few hundred of them, I tried manually ordering them, but it was taking too long, so I threw this script together. Hopefully someone else can use this.

Be sure to set the variable "FileLocation" to where your msgFilterRules.dat file is located, and set "BackupFileLocation" to where you want the backup to go. It will create a backup to that location, with a date/time stamp at the end of the name. Also, be sure that Thuderbird is not running when you run this script.

;
; (Win+q) - sort message filters for tbird
;
#q::

FileName := "msgFilterRules.dat"
FileLocation := "C:\mozilla.org\Users\Johnny\Thunderbird.slt\Mail\Local Folders\"
BackupFileLocation := "C:\mozilla.org\Users\Johnny\"

FileRead, Contents, %FileLocation%%FileName%

StringReplace, Contents, Contents, `r`n, % Chr(255), All
StringReplace, Contents, Contents, name=", `r`nname=", All
StringReplace, Contents, Contents, version=", aaaaaaaaaaversion="

Sort, Contents

StringReplace, Contents, Contents, aaaaaaaaaaversion=", version="
StringReplace, Contents, Contents, `r`nname=", name=", All
StringReplace, Contents, Contents, % Chr(255), `r`n, All

FormatTime, DateTimeString, , yyyyMMddHHmmss
FileMove %FileLocation%%FileName%, %BackupFileLocation%%FileName%.%DateTimeString%
FileDelete, %FileLocation%%FileName%
FileAppend, %Contents%, %FileLocation%%FileName%

; Free the memory
FileName =
FileLocation =
BackupFileLocation =
Contents =
DateTimeString =

msgbox done
User avatar
Lee_Dailey
Posts: 14194
Joined: July 27th, 2004, 4:33 pm
Location: milky way galaxy, sol system, terra, north america, usa, tx, bedford

Re: Sort Message Filters

Post by Lee_Dailey »

howdy johnmsch,

that's pretty nifty! i never would have thot to do it as a long string and then work with it. here's a similar version in windows powershell. since that's included with win7, it may be handier for some folks. i use autohotkey for hotkeys, so your script would work for me. [*grin*] i doubt that others will even know what autohotkey IS.

anyway, here's my not-a-good-programmer version of your script done in powershell.

Code: Select all

# file named - Thunderbird_Filters-Sorted-By-Name.ps1

<#
this script sorts the tbird message filter rules file by filter name.

i recommend ...
- copying the original file to a folder - c:\temp, for example
- run the script
- view the new file in a text editor like notepad to see if it really worked [*grin*]
- copy the new file to the original location
- rename the oiginal file - ex. = msgFilterRules.dat.bak
- rename the new, sorted file to msgFilterRules.dat

you can do it all in one swell foop if you desire by ...
- setting $Directory to the tbird mail account folder where the msgFilterRules.dat file is located
- un-commenting the first $OutFile line and commenting out the second $OutFile line
#>

$Directory = "c:\temp"
$InFile = "msgFilterRules.dat"
# live dangerously! overwrite your input file with your output file!
# $OutFile = $InFile
$OutFile = -join ($InFile, ".", (get-date).ToString("yyyy-MM-dd"))
$LineBreak = "linebreak"
$FilterStart = "filterstart"
$SortFirst = "a_"
$NewFilterList = ""

# read the file - it comes in as an array
$OldFilterList = get-content -path ($Directory, $InFile -join "`\")
# save a backup just in case
set-content -path ($Directory, ($InFile, ".bak" -join "") -join "`\") -value $OldFilterList

# convert the array into a l-o-n-g string with appropriate re-splitter markers
foreach($line in $OldFilterList)
    {
    # mark the version line so it always sorts first
    if($line -match "version=")
        {
        $line = $SortFirst, $line -join ""
        }
    # mark the start of a filter   
    if($line -match "name=")
        {
        $line = $FilterStart, $line -join ""
        }
    # mark the end of a line   
    $line = $line, $LineBreak -join " "
    # build the l-o-n-g string
    $NewFilterList = $NewFilterList, $line -join ""
    }
# split it at the filter marker, sort it, join it back into a string
# - note that the filter marker is gone now
$NewFilterList = (($NewFilterList -split $FilterStart) | sort-object) -join ""
# split at the linebreak marker
# - note that the linebreak marker is gone now
$NewFilterList = $NewFilterList -split $LineBreak
# get rid of the no longer needed "a_" on the "version=" line
# - it otta ALWAYS be item zero
$NewFilterList[0] = $NewFilterList[0] -replace $SortFirst, ""

# write the new file
set-content -path ($Directory, $OutFile -join "`\") -value $NewFilterList

# end


take care,
lee
johnmsch
Posts: 164
Joined: April 20th, 2004, 10:30 am
Location: Marietta, GA

Re: Sort Message Filters

Post by johnmsch »

Nice one Lee. I had thought about using PS or even VB Script, but just started playing with AHK first. Still can't believe how simple it was!
User avatar
Lee_Dailey
Posts: 14194
Joined: July 27th, 2004, 4:33 pm
Location: milky way galaxy, sol system, terra, north america, usa, tx, bedford

Re: Sort Message Filters

Post by Lee_Dailey »

howdy johnmsch,

yep, ahk IS easy. i love it for what it was first designed for - hotkeys. i've a dozen or so that i use every danged day. powershell ... is odd. there are command line structures that it uses that REALLY annoy me [the FOR structure, for instance] and seem to exist for no reason. then you realize it's what ms is using for most of their server interfaces - both command line and gui - and it makes a tad more sense. the easy interface to COM objects lets me control some of my apps from scripts in handy ways. like starting up itunes & play the last-used playlist.

the various powershell sites are pretty fun to read if you like that kinda thing. [*grin*]

thanks for the idea! i never would have gone at it that way and your way is MUCH easier than the kludge i was trying.

take care,
lee
DFaivre
Posts: 1
Joined: September 8th, 2012, 9:47 am

Re: Sort Message Filters

Post by DFaivre »

Hello johnmsch ,

I realize that your post was quite some time ago however I just happened across your post and would like to utilize your script. I have quite a few message filters and new ones are added almost daily. Being able to sort them is something I have wanted to be able to do and like you found it to be to time consuming.

When searching for my msgFilterRules.dat file I found that I actually have several of these files. I am asumming that this is because I have several email accounts that Thunderbird is setup with. That being the case which on do I use as the path for the script?

Thanks again for your script and any help you are able to provide.
User avatar
DanRaisch
Moderator
Posts: 127246
Joined: September 23rd, 2004, 8:57 pm
Location: Somewhere on the right coast

Re: Sort Message Filters

Post by DanRaisch »

DFaivre, johnmsch has not posted on the forums since April of this year. If they see this question the can respond directly to you by Private Message.

Locking this due to the age of the original posts (2010).
Locked