简体   繁体   English

使用 PowerShell 提取文件名的一部分

[英]Extract part of file name using PowerShell

I get about 200 files everyday that need to be processed.我每天收到大约 200 个需要处理的文件。 I need to extract the part of the file name.我需要提取文件名的一部分。 For Example, I receive the files with the following naming convention:例如,我收到具有以下命名约定的文件:

X12345678 - NAME - DocumentType.pdf

I need a PowerShell script to extract the 'X12345678' part of the file name to process it further.我需要一个 PowerShell 脚本来提取文件名的'X12345678'部分以进一步处理它。

Can you please help me with this.你能帮我解决这个问题吗?

You can use a simple RegEx.您可以使用简单的 RegEx。 Given a filename as per your example, here is the general idea:根据您的示例给出文件名,这是一般想法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM