Rename Outlook 2016 msg files with date and subject

Hi Guys,

I'm certain I've read about this issue on here before but I can't find anything.

A client has asked a colleague to copy a load of emails from Outlook in .msg format to a shared network drive, which is simple enough to do. When an email with a similar name is copied, one of the "rename (all)" options can be used, which works fine, albeit rather useless information when searching for a particular subject.

What I would like is to rename the files with the date and time received or sent, and the subject (i.e. 20191002 - 1438 Opus Registration Details). Is this at all possible?

At the moment, this is a one-off task so I don't want to pursue it too much if it can't be done easily. Ideally we need to educate the client in the difficult task of using .pst files instead :laughing:

But, if it can be done, it may encourage more colleagues to purchase Directory Opus :wink:

Regards,

Blueroly

Outlook's .msg format doesn't look easy to parse via a script, so the easiest way to get the receipt date and subject line out of them is probably via shell columns.

i.e. If you open a File Explorer window, can you find columns which you can add to that which display the data you want for your .msg files?

If you can find columns for the data then it's fairly easy to use those in Opus scripts to get the same data.

If there aren't columns for the data then another possibility is using Outlook's scripting automation API, but I'm not very familiar with that. (In fact, I've never used its scripting API, but I'm assuming it has one as most of the other Office apps do.)

You could use PowerShell to do this (esp if its a one off). The Outlook API is a bit hit a miss, and best to close outlook first as having it open can cause errors.

Get-ChildItem "D:\emails" -Filter *.msg |`
ForEach-Object{
    $currentFile = $_
    $outlook = New-Object -comobject outlook.application
    $msg = $outlook.CreateItemFromTemplate($currentFile.FullName)
    #$msg = $outlook.Session.OpenSharedItem($currentFile.FullName) //Dont use OpenSharedItem as it locks the file
    Rename-Item -LiteralPath $currentFile.FullName -NewName "$($_.BaseName)-$($msg.Senton.ToString('yyyyMMddHHmmss'))-$($msg.subject)$($_.Extension)"
    }

more details
http://jon.glass/blog/reads-e-mail-with-powershell/

The outlook.application object should work in VBScript and JScript as well, so you could use it from an Opus rename script.

1 Like