powershell - 使用Powershell重命名.msg文件

标签 powershell windows-7 outlook powershell-2.0 msg

我们倾向于将消息从Outlook拖放到Windows资源管理器中,因此我需要重命名默认消息文件名,以便可以从资源管理器中搜索/读取文件。

我设法整理了以下代码,几乎将网络文件夹上的Outlook文件从默认的“Subject.msg”重命名为“收件人-主题-ReceivedDate-hhmmss.msg”。唯一的问题是重命名步骤不起作用,因为我相信Outlook进程正在锁定文件。我希望能帮助您避免锁定并重命名文件?另外,我不确定如果“收件人”列表中有多个人会发生什么,我是否愿意在“收件人”列表中取名字?这是我的努力:

$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 [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

但是,当我关闭Outlook(由powershell脚本启动)时,我可以运行Rename-Item命令,它可以成功运行。

最佳答案

这个怎么样?

本质上,我所做的更改是:

  • 现在,您的重命名循环会将其输出抛出到哈希表中。
  • Stop-Process杀死Outlook。
  • 然后另一个循环进行重命名。
        # 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
        }
    
  • 关于powershell - 使用Powershell重命名.msg文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17154825/

    相关文章:

    html - 呈现 HTML 片段而不将其保存到文件中

    python - 如何以管理员身份运行终端以使用 virtualenv

    c# - 如何从 myRibbon.cs 调用 ThisAddIn.cs 类的方法

    vba - 如何在电子邮件到达时启动 URL

    powershell - 如何调用表达式来调用带有变量的函数或脚本?

    python - 当 $env :path? 中定义了多个时,windows 如何决定使用哪个 python 可执行文件

    java - 如何保持Java 32位和64位版本相同?

    c# - 如何在windows窗体中开发类似 "outlook 2007 send/receive progress"对话框的窗体?

    powershell - 如何使用 PowerShell 将 HTTP 请求版本更改为 1.0

    node.js - 如何从错误 : listen EADDRINUSE 中释放 localhost