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 there is an action that automatically saves all the email attached files to a folder on your computer. If a file with the same name already exists in that folder, it will be replaced and this is something that many people love as they know they will have the latest file version in a known place on their computers. Think, for example, a clerk that receives weekly by email the updated list of prices in an Excel file. He/She does not have to care where to look for that file or search for the latest email, just open a folder in the computer, and the most recent list price will be there.

But for other users, this is a problem. They want to keep track the multiple file versions they receive and having that file replaced everytime an email is received is a problem. They cannot check, for example, how much the prices have changed.

This blog post shows a workaround to the Email Parser default behavior which is replacing the existing files. In this example, we will add to the attachment filename the date and time it was sent. The result looks like this:

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:

 

renaming attached files

 

It is a very standard setup in the email source and email filtering part. A Gmail email source along with an email filter is the first step. Then, we do not do any text capturing as we only care about the attachments. In the first action we save the attachments, and then we repeat the following actions for each attachment.

The key here is the action named “Create a new file name” It is a script and it may look weird to you if you do not have programming skills. It takes the date and time when the incoming email was sent and build a new file name with that.

The last step just renames the attached file like if you were typing a “rename” command in the windows command prompt. Let’s see the settings of every action:

 

saving excel attachments automatically

 

repeating actions for each attachment

 

a script to create an unique file name

 

 

 

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["Attachment"]);
    
    // 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);
    }
}

 

renaming attachments from the windows command prompt