从 Powershell 中下划线和空格划定的文件名中提取单词
Extract words from filename delineated by underscores and spaces in Powershell
我正在尝试从文件名中提取两个词。名称的格式为:
__XXXXXXXX_XXX_XXXXXXX_XXXX_XXXXX_XXXX XXX_Aircraft 017_XXXXXXXX-XXXXXXX_XXXXXXX-XXXXXXX-XXXXXX-01Apr2021-XXXXX
随着 X 被替换为不同的词。我需要提取飞机编号和日期,以便我可以仅使用该信息重命名文件。使用此站点的帮助,我尝试了以下方法来隔离飞机编号:
$names = gci -Path "H:\Path\to\Logs" *.log -Recurse | select @{n="Name"; e={if ($_.Name -match "Aircraft (\w+)") {
$matches[1] }}}
然而,它似乎没有给我我需要的比赛。但是,我在编程方面非常不熟练,可能会走错路。我希望用于隔离飞机编号的相同逻辑也适用于日期。
# Create a sample file.
$file = New-Item '__XXXXXXXX_XXX_XXXXXXX_XXXX_XXXXX_XXXX XXX_Aircraft 017_XXXXXXXX-XXXXXXX_XXXXXXX-XXXXXXX-XXXXXX-01Apr2021-XXXXX'
# Substitute your `Get-ChildItem` command for $file
$file |
Rename-Item -WhatIf -NewName {
if ($_.Name -match '_(Aircraft \w+?)_.+(\d{2}[a-z]{3}\d{4})-') {
# Synthesize the new file name from the extracted substrings.
'{0} - {1}' -f $Matches[1], $Matches[2]
} else {
# Input file name didn't match, (effectively) do nothing.
$_.Name
}
}
注意:上面命令中的-WhatIf
common parameter预览操作。一旦您确定该操作将执行您想要的操作,请删除 -WhatIf
。
regex used with the -match
operator above, see this regex101.com page.[1]
的解释
上面使用两个捕获组((...)
)捕获感兴趣的子串,可以通过索引1
和[=17访问=] automatic $Matches
variable.
-f
,然后使用 format operator 从捕获的子字符串构建输出文件名。根据需要调整 LHS 格式字符串。
感谢 -WhatIf
,您将看到如下输出,这是删除 -WhatIf
时会发生什么的预览 -注意 Destination:
路径中的新文件名:
What if: Performing the operation "Rename File" on target
"Item: /tmp/__XXXXXXXX_XXX_XXXXXXX_XXXX_XXXXX_XXXX XXX_Aircraft 017_XXXXXXXX-XXXXXXX_XXXXXXX-XXXXXXX-XXXXXX-01Apr2021-XXXXX
Destination: /tmp/Aircraft 017 - 01Apr2021".
请注意 脚本块 ({ ... }
) 如何作为参数传递给 Rename-Item
's -NewName
parameter, which then acts on each input file via the automatic automatic $_
variable and outputs the argument value to use for the input object at hand. Such script blocks are called 。
[1] 请注意,尽管 regex101.com 这个用于可视化、解释和试验正则表达式的站点不支持 .NET PowerShell 使用的正则表达式引擎,选择类似的引擎,例如 Java's,通常表现出相同的行为,至少从根本上是这样。
我正在尝试从文件名中提取两个词。名称的格式为:
__XXXXXXXX_XXX_XXXXXXX_XXXX_XXXXX_XXXX XXX_Aircraft 017_XXXXXXXX-XXXXXXX_XXXXXXX-XXXXXXX-XXXXXX-01Apr2021-XXXXX
随着 X 被替换为不同的词。我需要提取飞机编号和日期,以便我可以仅使用该信息重命名文件。使用此站点的帮助,我尝试了以下方法来隔离飞机编号:
$names = gci -Path "H:\Path\to\Logs" *.log -Recurse | select @{n="Name"; e={if ($_.Name -match "Aircraft (\w+)") {
$matches[1] }}}
然而,它似乎没有给我我需要的比赛。但是,我在编程方面非常不熟练,可能会走错路。我希望用于隔离飞机编号的相同逻辑也适用于日期。
# Create a sample file.
$file = New-Item '__XXXXXXXX_XXX_XXXXXXX_XXXX_XXXXX_XXXX XXX_Aircraft 017_XXXXXXXX-XXXXXXX_XXXXXXX-XXXXXXX-XXXXXX-01Apr2021-XXXXX'
# Substitute your `Get-ChildItem` command for $file
$file |
Rename-Item -WhatIf -NewName {
if ($_.Name -match '_(Aircraft \w+?)_.+(\d{2}[a-z]{3}\d{4})-') {
# Synthesize the new file name from the extracted substrings.
'{0} - {1}' -f $Matches[1], $Matches[2]
} else {
# Input file name didn't match, (effectively) do nothing.
$_.Name
}
}
注意:上面命令中的-WhatIf
common parameter预览操作。一旦您确定该操作将执行您想要的操作,请删除 -WhatIf
。
regex used with the -match
operator above, see this regex101.com page.[1]
上面使用两个捕获组((...)
)捕获感兴趣的子串,可以通过索引1
和[=17访问=] automatic $Matches
variable.
-f
,然后使用 format operator 从捕获的子字符串构建输出文件名。根据需要调整 LHS 格式字符串。
感谢 -WhatIf
,您将看到如下输出,这是删除 -WhatIf
时会发生什么的预览 -注意 Destination:
路径中的新文件名:
What if: Performing the operation "Rename File" on target
"Item: /tmp/__XXXXXXXX_XXX_XXXXXXX_XXXX_XXXXX_XXXX XXX_Aircraft 017_XXXXXXXX-XXXXXXX_XXXXXXX-XXXXXXX-XXXXXX-01Apr2021-XXXXX
Destination: /tmp/Aircraft 017 - 01Apr2021".
请注意 脚本块 ({ ... }
) 如何作为参数传递给 Rename-Item
's -NewName
parameter, which then acts on each input file via the automatic automatic $_
variable and outputs the argument value to use for the input object at hand. Such script blocks are called
[1] 请注意,尽管 regex101.com 这个用于可视化、解释和试验正则表达式的站点不支持 .NET PowerShell 使用的正则表达式引擎,选择类似的引擎,例如 Java's,通常表现出相同的行为,至少从根本上是这样。