Powershell 批量文件重命名格式更改

Powershell 批量文件重命名格式更改,powershell,Powershell,我正在尝试为我的部门提供概念证明,该部门试图从位于一组文件夹中的.msg文件中提取附加文件。我仍然在努力跟上PowerShell的速度,尤其是在使用模块和重命名功能时 我在网上找到了一个模块,它可以很好地完成我所需要的一切,只是我需要在新的附件文件名中添加一个稍微不同的变量。i、 e.不确定如何使用下面的代码修改行 $attFn = $msgFn -replace '\.msg$', " - Attachment - $($_.FileName)" 下面的代码提取附加文件,并沿以下行重命名它们

我正在尝试为我的部门提供概念证明,该部门试图从位于一组文件夹中的.msg文件中提取附加文件。我仍然在努力跟上PowerShell的速度,尤其是在使用模块和重命名功能时

我在网上找到了一个模块,它可以很好地完成我所需要的一切,只是我需要在新的附件文件名中添加一个稍微不同的变量。i、 e.不确定如何使用下面的代码修改行

$attFn = $msgFn -replace '\.msg$', " - Attachment - $($_.FileName)"
下面的代码提取附加文件,并沿以下行重命名它们

带有附件的电子邮件文件
MessageFilename.msg
将附件文件名提取到
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."
    }
}
表示它将是一个完整路径,格式为
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)

这就解决了!非常感谢您抽出时间为PS新手用户服务。
# 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)