Sign in to follow this  
Followers 0
picea892

Outlook Inbox Archiver

1 post in this topic

Once run, this script will search your inbox for files 25 days or older and save them to a folder on your desktop.

Really a somewhat simple script, but thought others might have a use.

#Include <Date.au3>
#Include <File.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
dim $fmpreplace[30][2]
if ProcessExists("OUTLOOK.EXE") Then
    inbox()
EndIf
exit 0 

func Formatdate($start)
$h=StringMid($start,9,2)
$min=StringMid($start,11,2)
if $h<10 then $h= stringright($h,1)
If $h > 12 Then
    $h = $h - 12
    $min = $min & " PM"
ElseIf $h = 12 Then
    $min = $min & " PM"
Else
    $min = $min & " AM"
EndIf
$time=$h&":"&$min
Return $time
EndFunc


Func _NormalizeFilename($inFilename,$sender, $maxLen = 32)
    local $sExclude_List[7] = ["Re:","RE", "FW","I:","M:","R:","F:"]
    Local Const $illegalFilenameChars = " !@#$%^&*()+-={}|[]\:'<>?/.,"
    Local $c = ""
    Local $fixedFilename = ""
        $sender=StringLeft($sender,StringInStr($sender,",")-1)

    $mergedname=$sender&"_"&$inFilename
    For $i= 0 to UBound($sExclude_List)-1
        $mergedname= StringReplace($mergedname, $sExclude_List[$i], "",0,1)
    Next
    For $x = 1 To StringLen($mergedname)
        $c = StringMid($mergedname, $x, 1)
        If StringInStr($illegalFilenameChars, $c) Then
            $c = ""
        EndIf
        $fixedFilename &= $c
    Next
    If $maxLen Then
        Return StringLeft($fixedFilename, $maxLen)
    EndIf
    Return $fixedFilename
EndFunc  ;==>_NormalizeFilename

Func _NormalizeFilename2($inFilename)
    Return StringRegExpReplace($inFilename,"[?\[\]/\\=+-<>:;'"",* %]","")
EndFunc;==>_NormalizeFilename

func inbox()
    $desktop=RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders","Desktop")
    Global $objOL = ObjCreate("Outlook.Application")
    Global $session = $objOL.GetNameSpace("MAPI")
    $inbox = $session.GetDefaultFolder(6)
    $c=0
    $d=0
    if not FileExists($desktop&"\archived_email\") Then
        DirCreate($desktop&"\archived_email\")
    EndIf
    For $m In $inbox.items
        $c=$c+1
        $entryID=$m.entryid
        ;MsgBox(0,"",$entryID)
        ;$m=$inbox.GetItemFromID($m2.entryid)
        $totaltime = $m.ReceivedTime
        $year=StringMid($totaltime,1,4)
        $month=StringMid($totaltime,5,2)
        $day=StringMid($totaltime,7,2)
        if $month = @mon then

            if @MDAY-$day>25 then
                if not FileExists($desktop&"\archived_email\"&_NormalizeFilename($m.subject,$m.sendername)&"_"&$year&$month&$day&".msg") then
                    $fullname=$desktop&"\archived_email\"&_NormalizeFilename($m.subject,$m.sendername)&"_"&$year&$month&$day&".msg"
                    $m.Saveas($fullname)
                    ; $m.delete
                    $d=$d+1
                    FileSetTime($fullname, $totaltime,0)
                    ToolTip("Examining item #"&$c)
                Else
                    ToolTip("Examining item #"&$c&" already exists, file not copied")
                    sleep(200)
                EndIf
            EndIf
        ElseIf $month+1 = @mon then
            if @MDAY+(_DateDaysInMonth($year,$month)-$day)>25 Then
                if not FileExists($desktop&"\archived_email\"&_NormalizeFilename($m.subject,$m.sendername)&"_"&$year&$month&$day&".msg") then
                    $fullname=$desktop&"\archived_email\" &_NormalizeFilename($m.subject,$m.sendername)&"_"&$year&$month&$day&".msg"
                    $m.Saveas($fullname)
                    ; $m.delete
                    $d=$d+1
                    FileSetTime($fullname, $totaltime,0)
                    ToolTip("Examining item #"&$c)
                Else
                    ToolTip("Examining item #"&$c&" already exists, file not copied")
                    sleep(200)
                EndIf
            EndIf
    
        EndIf

    Next
    $session.logoff
ToolTip($d&" files were moved to "& $desktop&"\archived_email\")
sleep(3000)
EndFunc

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0