使用PowerShell提取部分文件名

时间:2018-09-06 09:20:25

标签: powershell

我每天大约需要处理200个文件。 我需要提取文件名的一部分。 例如,我收到具有以下命名约定的文件:

X12345678 - NAME - DocumentType.pdf

我需要PowerShell脚本来提取文件名的'X12345678'部分,以便进一步处理。

能帮我吗?

1 个答案:

答案 0 :(得分:0)

您可以使用简单的RegEx。根据您的示例给出的文件名,以下是基本概念:

# Check the filename
if("X12345678 - NAME - DocumentType.pdf" -match "^(?<code>\w\d+) -.*$")
{
    # Get the code
    $fileCode = $matches.code

    # Do something with the code
    "Filename is valid: $fileCode"
}
else
{
    "Filename is not in required format"
}