LoginSignup
5
2

More than 5 years have passed since last update.

Thunderbird の .eml ファイルや Outlook の .msg ファイルのファイル名に送信日時を付ける

Posted at

概要

指定したフォルダツリー内にある全ての .eml や .msg ファイルに対して、ファイル名の先頭部分にメッセージの送信時刻を付加する処理を一括して行なうための簡単なツールを作りました。

背景

Outlook や Thunderbird のメールメッセージを、エクスプローラーのフォルダへのドラッグ&ドロップでファイルとして保存した場合、ファイル名は (メッセージの件名)+(拡張子 .msg / .eml)となり、またファイルの更新日時はその操作を行なったタイミングになるため、保存された複数のファイルを送信時刻でソートすることができません。

使い方

下記のコードをバッチファイルとして拡張子 .bat のファイルに保存し、.eml ファイルや .msg ファイルを含む対象フォルダをその上にドラッグ&ドロップします。

コード

中身はほぼ PowerShellスクリプトですが、Windows バッチファイルとして実行できます(先頭行をコメントアウトまたは削除して拡張子を .ps1 に変更すると PowerShell スクリプトになります)。

eml ファイル用

RenameEml.bat
@Powershell -NoP -C "&([ScriptBlock]::Create((gc '%~f0'|?{$_.ReadCount -gt 1}|Out-String)))" '%1' & pause & exit/b
# by earthdiver1
param([parameter(Position=0)][String]$Dir)
if (-not $Dir) { 
    Write-Host "対象フォルダが指定されていません。処理を終了します。" -Fore Red
    exit
} 
if (-not (Test-Path -LiteralPath $Dir -PathType Container)) { 
    Write-Host "対象フォルダがありません。処理を終了します。" -Fore Red
    exit
}
$outlook = New-Object -ComObject Outlook.Application
$stream = New-Object -ComObject ADODB.Stream
$msg = New-Object -ComObject CDO.Message
Write-Host "処理を開始します。"
Get-ChildItem -LiteralPath $Dir -Filter "*.eml" -Recurse | % {
    $stream.Open()
    $stream.LoadFromFile($_.FullName)
    $msg.DataSource.OpenObject($stream,"_Stream")
    $stream.Close()
    Rename-Item -LiteralPath $_.FullName "$($msg.Senton.ToString('yyyyMMddHHmm'))_$($_.BaseName)$($_.Extension)"
}
Write-Host "処理が終了しました。"

msg ファイル用

RenameMsg.bat
@Powershell -NoP -C "&([ScriptBlock]::Create((gc '%~f0'|?{$_.ReadCount -gt 1}|Out-String)))" '%1' & pause & exit/b
# by earthdiver1
param([parameter(Position=0)][String]$Dir)
if (-not $Dir) { 
    Write-Host "対象フォルダが指定されていません。処理を終了します。" -Fore Red
    exit
} 
if (-not (Test-Path -LiteralPath $Dir -PathType Container)) { 
    Write-Host "対象フォルダがありません。処理を終了します。" -Fore Red
    exit
}
$outlook = New-Object -ComObject Outlook.Application
Write-Host "処理を開始します。"
Get-ChildItem -LiteralPath $Dir -Filter "*.msg" -Recurse | % {
    $msg = $outlook.CreateItemFromTemplate($_.FullName)
    Rename-Item -LiteralPath $_.FullName "$($msg.Senton.ToString('yyyyMMddHHmm'))_$($_.BaseName)$($_.Extension)"
}
Write-Host "処理が終了しました。"

 
クリエイティブ・コモンズ 表示 - 継承 4.0 国際

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2