简体   繁体   English

使用Powershell重命名.msg文件

[英]Renaming .msg files using Powershell

We tend to drag and drop messages from outlook into Windows Explorer so I need to rename the default message filename so that the files are searchable/readable from Explorer. 我们倾向于将邮件从Outlook拖放到Windows资源管理器中,因此我需要重命名默认的邮件文件名,以便可以从资源管理器中搜索/读取文件。

I have managed to put together the following code that almost renames an outlook file on a network folder from the default "Subject.msg" to "To - Subject - ReceivedDate - hhmmss.msg". 我设法整理了以下代码,几乎将网络文件夹上的Outlook文件从默认的“ Subject.msg”重命名为“收件人-主题-ReceivedDate-hhmmss.msg”。 The only problem is that the rename step does not work as I believe the Outlook process is locking the file. 唯一的问题是重命名步骤不起作用,因为我认为Outlook进程正在锁定文件。 I would appreciate help to avoid the locking and rename the files? 我希望能帮助您避免锁定并重命名文件? Also, I am not sure what happens if there are multiple people in the To list, I would be happy to take the first name in the To list? 另外,我不确定如果“收件人”列表中有多个人会发生什么,我是否愿意在“收件人”列表中取名字? Here is my effort: 这是我的努力:

$olMailItemPath = "W:\Marketing\Prospects\Emails\*"
Write-Host $olMailItemPath

$SourceFiles = Get-Item -path $olMailItemPath -include *.msg
$outlook = New-Object -comobject outlook.application
$namespace = $outlook.GetNamespace("MAPI")

function cleanName($aname)
{
    $aname = $aname -replace "'"
    $aname = $aname -replace ":"
    $aname = $aname -replace "@"
    $aname = $aname -replace "-"
    return ($aname.trim())
}

function cleanSubject($subject)
{
    $subject = $subject -replace 'Re:'
    $subject = $subject
    return (' - ' + $subject.trim() + ' - ')
}


foreach ($msg in $SourceFiles){
    $olMailItem = $NameSpace.OpenSharedItem($msg)
    $EmailTo = $olMailItem.To
    $EmailSubject = $olMailItem.Subject
    $DateRecieved = $olMailItem.ReceivedTime
    $newfilename = (cleanName($EmailTo)) + (cleanSubject($EmailSubject)) + $DateRecieved.ToString("yyyyMMdd - hhmmss") + ".msg"

    # Write-Host "Email Sent To: $EmailTo "
    # Write-Host "Subject: $EmailSubject "   
    # Write-Host "Date Recieved: $DateRecieved"    

    Write-Host $msg    
    Write-Host $newfilename
    Rename-Item $msg $newfilename
    }

ps [Inserted @ 18 Jun 2013] In answer to Athom, I know Outlook is locking the file as I get the following error: ps [插入时间:2013年6月18日]为了回答Athom,我知道Outlook在锁定文件时出现以下错误:

Rename-Item : The process cannot access the file because it is being used by another process.
At C:\Users\Bertie\Dropbox\Programming\Powershell\Rename Outlook Messages.ps1:41 char:16
+     Rename-Item <<<<  -path $msg -newname $newFileName
    + CategoryInfo          : WriteError: W:\Marketing\Prospects\Emails\new.msg:String) [Rename-Item], IOException
    + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand

However, when I close outlook (which is initiated by the powershell script), I can then run the Rename-Item command and it run's successfully. 但是,当我关闭Outlook(由powershell脚本启动)时,我可以运行Rename-Item命令,它可以成功运行。

How's this? 这个怎么样?

Essentially the changes I have mades are: 本质上,我所做的更改是:

  • Your renaming loop now throws its output to a hashtable. 现在,您的重命名循环会将其输出扔到哈希表中。
  • Stop-Process kills Outlook. 停止进程杀死Outlook。
  • Another loop then does the renaming. 然后,另一个循环进行重命名。

      # Declare the hastable to store the names, then generate the names. $nameHash = @{}; Foreach ($msg in $SourceFiles){ # Do the Outlook thing $olMailItem = $NameSpace.OpenSharedItem($msg) $EmailTo = $olMailItem.To $EmailSubject = $olMailItem.Subject $DateRecieved = $olMailItem.ReceivedTime $newfilename = (cleanName($EmailTo)) + (cleanSubject($EmailSubject)) + $DateRecieved.ToString("yyyyMMdd - hhmmss") + ".msg" # Write-Host "Email Sent To: $EmailTo " # Write-Host "Subject: $EmailSubject " # Write-Host "Date Recieved: $DateRecieved" # Store the names $nameHash.Add("$msg","$newfilename") } # Kill Outlook.. Then wait.... Stop-Process -Name Outlook -Force Start-Sleep -m 500 # You might be able to remove this - depends how beefy your CPU is. # Rename ForEach ($item in $nameHash.GetEnumerator()) { # Testing >>--> echo $item.Name echo $item.Value # <--<< Testing Rename-Item $item.Name $item.Value } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM