title: Bulk File Renaming - Extract parent foldername and use in file renaming
body: <p>PowerShell novice here again with my proof of concept.</p>
  
  <p>The code below successfully extracts attached files from .msg files located in folders and leaves the extracted filename without changing it. What I'm now looking for now is to extract part of the parent folder name, with standard format of...</p>
  
  <p>nnnn+string (e.g. "8322 MyStudy") i.e. 4 digits followed by a space then string.</p>
  
  <p>...to rename the extracted filename from...</p>
  
  <p>ExtractedFilename.pdf to "0nnnn - ExtractedFilename.pdf". e.g. "08322 - ExtractedFilename.pdf"</p>
  
  <p>My main problem is how to extract the numeric part of the parent folder name (from where my module will be run). I'm hoping that my poor PS formatting skills will allow me to do the rest.</p>
  
  <p>Once again, any help appreciated.</p>
  
  <pre><code>##
  ## 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
              # extract path, e.g. 'c:\path\to\'
              $msgPath = Split-Path -Path $msgFn
  
  
              # 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)"
                  $attFn = Join-Path -Path $msgPath -ChildPath ($_.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
              }
          }
      }
  
      # This function to rename expanded attachment file to study renaming standards
      Function RenameExpandedAttachments {
      }
  
      End
      {
          Write-Verbose "Done."
      }
  }
  </code></pre>