Anyways, here it is in case someone is interested, or if the author wants to improve it
Code: Select all
@echo off
echo ****************************************************************
echo ****************** BAYESIAN FILTER UPDATE SCRIPT ******************
echo ****************************************************************
echo.
rem ****************************************************************
rem *************** CUSTOMIZE VARS FOR YOUR SYSTEM *******************
rem ****************************************************************
rem *** Set the Domains (ie, "Post-Office Names") you use as Spam and Ham Sources
rem *** These must match the spellings of the folders in the "Postoffices" folder
rem *** Use spaces to separate Domain/Postoffice Names
SET Domains=mydomain.com yourdomain.net herdomain.org hisdomain.biz
rem *** Set the 'minimum number of messages' required before updating the dictionary
rem *** Ham & Spam messages will ACCUMULATE each time - until there are enough!
SET MinMsgsBeforeProcessing=1000
rem *** NOTE: Be careful to NOT have any 'trailing spaces' when setting variables below
rem *** Set the 'PostOffice' and 'Dictionary' folders - WITHOUT a trailing "\"
SET PO_Folder=E:\Email\Postoffices
SET DIC_Folder=E:\Email\Dictionaries
rem *** Set the 'Dictionary' path exactly the same as in the 'Bayesian Filter' Properties
SET Dictionary=%DIC_Folder%\Bayesian_Dic.TAB
rem *** Set the 'Mailbox Names' you are using to collect Spam and Ham messages
rem *** It is assumed you use the 'Inbox' of these mailboxes to store these emails
rem *** If this is not so, then adjust the MOVE command(s) in the next section
SET SpamMailbox=SPAM
SET HamMailbox=HAM
rem *** Set Spam and Ham 'Dictionary' and 'Storage' folders - WITHOUT a trailing "\"
SET NewSpam=%DIC_Folder%\NewSpam
SET NewHam=%DIC_Folder%\NewHam
SET OldSpam=%DIC_Folder%\OldSpam
SET OldHam=%DIC_Folder%\OldHam
SET ExcessSpam=%DIC_Folder%\ExcessSpam
SET ExcessHam=%DIC_Folder%\ExcessHam
rem ****************************************************************
rem ************** COLLECT THE HAM AND SPAM MESSAGES ******************
rem ****************************************************************
rem *** Move email stored by your configured filters to the dictionary folders
echo.
echo MOVE messages from Spam Source folder(s) to dictionary Spam folder...
echo.
FOR %%D IN (%Domains%) DO MOVE /Y "%PO_Folder%\%%D\MAILROOT\%SpamMailbox%\Inbox\*.mai" "%NewSpam%\"
echo.
echo MOVE messages from Ham Source folder(s) to dictionary Ham folder...
echo.
FOR %%D IN (%Domains%) DO MOVE /Y "%PO_Folder%\%%D\MAILROOT\%HamMailbox%\Inbox\*.mai" "%NewHam%\"
rem *******************************************************************
rem ************** MOVE THE EXCESS HAM AND SPAM MESSAGES ******************
rem *******************************************************************
echo.
echo MOVE messages from excess Spam folder to dictionary Spam folder...
echo.
MOVE /Y "%ExcessSpam%\*.mai" "%NewSpam%\"
echo.
echo MOVE messages from excess Ham folder to dictionary Ham folder...
echo.
MOVE /Y "%ExcessHam%\*.mai" "%NewHam%\"
rem *******************************************************
rem *** IMPORTANT NOTE - DO NOT IGNORE OR THIS WON'T WORK *****
rem *******************************************************
rem
rem *** You MUST add this registry entry to enable 'Delayed Expansion' ***
rem
rem [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor]
rem "DelayedExpansion"=dword:00000001
rem
rem *******************************************************
rem *** This section will count and move EXCESS Ham OR Spam to the storages folders
rem *** The target is 1-to-1 -- equal quantities of Ham and Spam messages
rem *** Initialize counter variables
SET NewSpam_Count=0
SET NewHam_Count=0
SET ExcessSpam_Count=0
SET ExcessHam_Count=0
rem *** NOTE: "SET /A" lets us do math! Use SET /? to see all available options
rem *** COUNT the messages in the Dictionary folders
FOR %%M IN ("%NewHam%\*.mai") DO SET /A NewHam_Count += 1
FOR %%M IN ("%NewSpam%\*.mai") DO SET /A NewSpam_Count += 1
echo.
echo There are %NewHam_Count% Ham messages and %NewSpam_Count% Spam messages
echo.
rem *** If either message count is below the minimum required, then just exit
IF %NewHam_Count% LSS %MinMsgsBeforeProcessing% (
ECHO There are only %NewHam_Count% Ham Messages - EXITING...
GOTO DONE
)
IF %NewSpam_Count% LSS %MinMsgsBeforeProcessing% (
ECHO There are only %NewSpam_Count% Spam Messages - EXITING...
GOTO DONE
)
rem *** Calculate number of 'Excess Messages' - only 1 can be 'positive'
SET /A ExcessHam_Count=NewHam_Count - NewSpam_Count
SET /A ExcessSpam_Count=NewSpam_Count - NewHam_Count
rem *** MOVE any Excess Messages to Storage - of whichever type is 'excess'
IF %ExcessHam_Count% GTR 0 (
echo There are %ExcessHam_Count% TOO MANY HAM Messages
FOR %%M IN ("%NewHam%\*.mai") DO (
rem *** MUST delimit with "!" here (delayed expansion) to update in a loop
IF !ExcessHam_Count! GTR 0 MOVE /Y "%%M" "%ExcessHam%\"
SET /A ExcessHam_Count = !ExcessHam_Count! - 1
)
)
IF %ExcessSpam_Count% GTR 0 (
echo There are %ExcessSpam_Count% TOO MANY SPAM Messages
FOR %%M IN ("%NewSpam%\*.mai") DO (
rem *** MUST delimit with "!" here (delayed expansion) to update in a loop
IF !ExcessSpam_Count! GTR 0 MOVE /Y "%%M" "%ExcessSpam%\"
SET /A ExcessSpam_Count = !ExcessSpam_Count! - 1
)
)
rem ****************************************************************
rem *************** UPDATE & RELOAD THE DICTIONARY *******************
rem ****************************************************************
echo.
echo.
echo *** Tell MTA filter to write current dictionary from memory to file.
echo.
MESPAMCMD -w
echo.
echo *** Process the new Spam and Ham messages to update the dictionary.
echo.
MESPAMCMD -m "%Dictionary%" "%NewSpam%" "%NewHam%"
echo.
echo *** Tell MTA filter to reload the dictionary from the newly updated file
echo.
MESPAMCMD -r
echo.
rem ****************************************************************
rem **************** EMPTY THE DICTIONARY FOLDERS ********************
rem ****************************************************************
echo.
echo *** Copy all the mail from the dictionary folders to the storage folders
echo *** These will come in handy if you ever need to create a new dictionary
echo.
echo MOVE NewHam to OldHam ...
echo.
MOVE /Y "%NewHam%\*.*" "%OldHam%\"
echo.
echo MOVE NewSpam to OldSpam ...
echo.
MOVE /Y "%NewSpam%\*.*" "%OldSpam%\"
rem *************** FINISHED -- PAUSE IF TESTING THE SCRIPT *******************
:DONE
rem *** Enable PAUSE while testing the batch file so you can see the output
rem *** Otherwise the DOS window immediately closes upon completion
echo.
PAUSE
EXIT