Tema: Man tamsus miskas, gal pasviesit?
Autorius: Sigitas
Data: 2015-09-29 18:35:09
    # file named - Thunderbird_Filters-Sorted-By-Name.ps1

    <#
       Sita faila paleisti su "Run with PowerShell" desiniu peles klavise
    this script sorts the tbird message filter rules file by filter name.

    i recommend ...
    - copying the original file to a folder - c:\Users\Sigitas\AppData\Roaming\Thunderbird\Profiles\qn08m9m5.default\Mail\Local Folders, 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:\Users\Sigitas\AppData\Roaming\Thunderbird\Profiles\qn08m9m5.default\Mail\Local Folders"
    $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 "`\")
   

    # 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








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

    i recommend ...
    - copying the original file to a folder - c:\Users\Sigitas\AppData\Roaming\Thunderbird\Profiles\qn08m9m5.default\Mail\mail.zebra.lt, 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:\Users\Sigitas\AppData\Roaming\Thunderbird\Profiles\qn08m9m5.default\Mail\mail.zebra.lt"
    $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 "`\")
    

    # 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