Somebytes SoftwareSomebytes Software Development

Sort mails and attachments in a directory structure by date and from address.


In this guide you will learn how to use EEAttachments and PowerShell to automatically store mails and their attachments in a directory structure based on the date / from address pattern. The directory structure is generated by the PowerShell script.


After you have created an order for your mails in EEAttachments, please mark the two checkboxes "Export attachments" and "Export mail".

Then create a PowerShell script with the following content, which is started for processing the attachments.

Script running for attachments (attachmentstruct.ps1):

$dt = Get-Variable -Name DateTimeReceived -ValueOnly
$file = Get-Variable -Name file -ValueOnly
$from = Get-Variable -Name From -ValueOnly
$dtstr = $dt.tostring("yyyyMMdd")
$path="C:\temp\NachDatum\"+$dtstr+"\"+$from.Address
if (!(Test-Path $path))
{
   mkdir $path
}
mv $file $path

Adjust the path and the date format according to your needs. Enter the file in the EEAttachments order under "Execute per attachment". You need %file%,%subject%,%from% and %datetimerecived% as parameters, which are transferred to the script as variables.



Script running per Mail (mailstruct.ps1):

$dt = Get-Variable -Name DateTimeReceived -ValueOnly
$eml = Get-Variable -Name emlfile -ValueOnly
$from = Get-Variable -Name From -ValueOnly
$subject = Get-Variable -Name subject -ValueOnly
$dtstr = $dt.tostring("yyyyMMdd")
$path="C:\temp\NachDatum\"+$dtstr+"\"+$from.Address
if (!(Test-Path $path))
{
mkdir $path
}
[System.IO.Path]::GetinvalidFileNameChars() | ForEach-Object {$subject = $subject.Replace($_," ")}
$newfilename = $path + '\' + $subject +'.eml'
mv $eml $newfilename

Adjust the path and the date format according to your needs and enter the script in the EEAttachments job under "Execute per mail". You need %subject%,%from% and %datetimerecived% as parameters.



The scripts were then expanded a bit so that the category and the project (job name) are now also used in the path.

The path name then looks like this: X:\Exporpath\[projektname]\[date]\[mailaddress]\[category]\[filename from subject]

Here you can download the two extended Powershellscripts and adapt them to your needs if necessary.