Email Parser

Extract data from incoming emails and automate your workflow

MENUMENU

Renaming attachments

 

The settings file of this example is available here

 

In Email Parser you can save the attached files to a folder on your computer. By default, if a file already exist with the same name, it will be replaced. If this is something you do not want to happen you need to rename the attachment. This is done running the “rename” command in the action Run a command under the Windows command prompt. For example:

 

attached files saved by date

 

 

As you can see in the screenshot above, an Excel file is sent every Friday at around 7:40 pm (the last part of the file name is the time). The original attachment file name is “my attached excel.xlsx” but Email Parser has automatically renamed it adding the date and time to the file name. This prevents a new incoming Excel to replace the old one because they share the same name. Email Parser was set up as this:

 

 

 

 

 

It is a very standard setup in the email source and email filtering part. We do not do any text capturing as this time we only care about the attachments. In the first action we save the attachments, and then we repeat the following actions for each attachment. These actions are creating a new file name and running the Windows rename command:

 

 

 

 

 

 

using System;
using System.Text;
using System.IO;
using System.Net.Mail;
using System.Collections.Specialized;
using EmailParserBackend.ScriptingInterface;

public class MyAction : ScriptBasedAction
{
public override bool Run(MailMessage email, NameValueCollection fields)
    {
    // Create a string that looks like this "aaa@aaa.com - 2017-09-21 19:46:50"
    string new_filename = fields["DateTimeSent"] + " - " + Path.GetFileName(fields["SavedAttachmentPath"]);
    
    // Remove any forbidden characters from the file name. Forbidden characters are < > / \ etc. 
    new_filename = new_filename.Replace(":","").Replace("/","").Replace("\"","").Replace("<","").Replace(">","").Replace("\\","");
    
    // Finally add the new_filename to the list of available fields to the next actions    
    fields.Add("new_filename",new_filename);
    return(true);
    }
}