批量文件重命名-提取父文件夹名称并在文件重命名中使用

时间:2018-08-01 10:19:35

标签: powershell

PowerShell新手再次用我的概念证明。

下面的代码成功地从文件夹中的.msg文件提取附件,并保留提取的文件名而不进行更改。我现在正在寻找的是提取父文件夹名称的一部分,其标准格式为...

nnnn +字符串(例如“ 8322 MyStudy”),即4位数字,后跟一个空格,然后是字符串。

...重命名从中提取的文件名...

ExtractedFilename.pdf转换为“ 0nnnn-ExtractedFilename.pdf”。例如“ 08322-ExtractedFilename.pdf”

我的主要问题是如何提取父文件夹名称的数字部分(将从中运行我的模块)。我希望我差的PS格式化技能可以让我做剩下的事情。

再次感谢您的帮助。

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

1 个答案:

答案 0 :(得分:0)

当前正在运行的脚本是:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

<body>
  <div ng-app="myApp" ng-controller="namesCtrl">

    <span>Filter your search : 
      <input type="text" ng-model="searchFilter">
      {{searchFilter}}
    </span>
    
    <hr>

    <div ng-repeat="item in arrayOfItems | myFilter: searchFilter">
      {{item.model}}{{item.id}}
    </div>
  </div>
</body>
</html>

使用$script:MyInvocation.MyCommand.Path 仅获取路径,

Split-Path

仅获取最后一个元素,请再次使用带有-Leaf参数的Split-Path

Split-Path $script:MyInvocation.MyCommand.Path

要提取前导数字,请使用带有(捕获组)的正则表达式。

Split-Path -Leaf (Split-Path $script:MyInvocation.MyCommand.Path)

将所有内容包装在if中:

'^(\d+) (.*)$'