批量文件重命名格式更改

时间:2018-08-01 08:22:02

标签: powershell

我正在尝试为我的部门进行概念验证,该部门试图从位于一组文件夹中的.msg文件提取附件。我仍在努力与PowerShell保持同步,尤其是在使用模块和重命名功能时。

我在网上找到了一个模块,该模块可以很好地完成我需要的一切,除了在新附件文件名中需要一个略有不同的变体。即不确定如何使用下面的代码修改行...

$attFn = $msgFn -replace '\.msg$', " - Attachment - $($_.FileName)"

下面的代码提取附件,并按照...的名称重命名它们。

带有附件MessageFilename.msg的电子邮件文件AttachmentFilename.pdf将附件文件名提取到Messagefilename - Attachement - AttachmentFilename.pdf

我真的需要将附件文件名仅提取为AttachmentFilename.pdf格式。我一直遇到的问题是,我一直丢失.msg文件名的路径,因此在尝试重命名为不存在的路径时遇到错误。我已经在调试模式下尝试了一些选项,但是尝试“替换”时始终会丢失路径上下文。

任何帮助表示赞赏...

借来的代码是...

##
## Source: https://chris.dziemborowicz.com/blog/2013/05/18/how-to-batch-extract-attachments-from-msg-files-using-powershell/
##
## Usage: Expand-MsgAttachment *
##
##

function Expand-MsgAttachment
{
    [CmdletBinding()]

    Param
    (
        [Parameter(ParameterSetName="Path", Position=0, Mandatory=$True)]
        [String]$Path,

        [Parameter(ParameterSetName="LiteralPath", Mandatory=$True)]
        [String]$LiteralPath,

        [Parameter(ParameterSetName="FileInfo", Mandatory=$True, ValueFromPipeline=$True)]
        [System.IO.FileInfo]$Item
    )

    Begin
    {
        # Load application
        Write-Verbose "Loading Microsoft Outlook..."
        $outlook = New-Object -ComObject Outlook.Application
    }

    Process
    {
        switch ($PSCmdlet.ParameterSetName)
        {
            "Path"        { $files = Get-ChildItem -Path $Path }
            "LiteralPath" { $files = Get-ChildItem -LiteralPath $LiteralPath }
            "FileInfo"    { $files = $Item }
        }

        $files | % {
            # Work out file names
            $msgFn = $_.FullName
            $msgFnbase = $_.BaseName


            # Skip non-.msg files
            if ($msgFn -notlike "*.msg") {
                Write-Verbose "Skipping $_ (not an .msg file)..."
                return
            }

            # Extract message body
            Write-Verbose "Extracting attachments from $_..."
            $msg = $outlook.CreateItemFromTemplate($msgFn)
            $msg.Attachments | % {
                # Work out attachment file name
                $attFn = $msgFn -replace '\.msg$', " - Attachment -  $($_.FileName)"

                # Do not try to overwrite existing files
                if (Test-Path -literalPath $attFn) {
                    Write-Verbose "Skipping $($_.FileName) (file already exists)..."
                    return
                }

                # Save attachment
                Write-Verbose "Saving $($_.FileName)..."
                $_.SaveAsFile($attFn)

                # Output to pipeline
                Get-ChildItem -LiteralPath $attFn
            }
        }
    }

    End
    {
        Write-Verbose "Done."
    }
}

1 个答案:

答案 0 :(得分:1)

$msgFn = $_.FullName

表示它将是c:\path\to\file.msg形式的完整路径。

因此您可以使用:

# extract path, e.g. 'c:\path\to\'
$msgPath = Split-Path -Path $msgFn


# make new path, e.g. 'c:\path\to\attachment.pdf'
$newFn = Join-Path -Path $msgPath -ChildPath ($_.FileName)