RobCodyStuff
RobCodyStuff

Reputation: 21

Bulk File Renaming Format Change

I'm attempting a proof of concept for my department which attempts to extract attached files from .msg files located in a set of folders. I'm still struggling with getting up to speed with PowerShell, especially when using modules and rename features.

I found a module on-line that pretty well does everything I need except that I need a slightly different variant in the new attachment filename. i.e. Not sure how to modify line with code below...

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

The code below extracts the attached files and renames them along the lines of...

An email file MessageFilename.msg, with an attachment AttachmentFilename.pdf extracts the attached filename to Messagefilename - Attachement - AttachmentFilename.pdf

I really need the attachment filename to be extracted into the format AttachmentFilename.pdf only. The problem I keep having is that I keep losing the path to the .msg filename so get errors when attempting the rename to a path that doesn't exist. I've tried a few options in debug mode but keep losing the path context when attempting the 'replace'.

Any help appreciated...

The borrowed code is...

##
## 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."
    }
}

Upvotes: 0

Views: 205

Answers (1)

TessellatingHeckler
TessellatingHeckler

Reputation: 29003

$msgFn = $_.FullName

says that it will be a full path in the form c:\path\to\file.msg.

So you can use:

# 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)

Upvotes: 1

Related Questions