首页 > 解决方案 > 将字符串与数组中的特定数字匹配

问题描述

我想创建一个脚本,使用Get-ChildItemcmdlet 在目录中搜索特定的“.txt”文件,然后将“.txt”复制到我想要的位置。对我来说困难的部分是从数组中提取特定的 .txt 文件字符串。所以基本上我需要帮助匹配数组中的特定文件名。这是我使用以下 cmdlet 返回的数组示例:

$arrayObject = (Get-ChildItem -recurse | Where-Object { $_.Name -eq "*.txt"}).Name

arrayobject 变量是这样的:

$arrayobject = "test.2.5.0.txt", "test.1.0.0.txt", "test.1.0.1.txt",
               "test.0.1.0.txt", "test.0.1.1.txt", "test.txt"

我想匹配我的数组,所以它返回以下内容:

test.2.5.0.txt,test.1.0.0.txt,test.1.0.1.txt

有人可以帮助我使用正则表达式来匹配上述文件名$arrayObject吗?

标签: powershell

解决方案


由于您已经将-Recurse参数添加到Get-ChildItem,您还可以-Include像这样使用参数:

$findThese = "test.2.5.0.txt", "test.1.0.0.txt", "test.1.0.1.txt"
$filesFound = (Get-ChildItem -Path 'YOUR ROOTPATH HERE' -Recurse -File -Include $findThese).Name

PS没有-Recurse参数你需要添加到根文件夹路径的\*末尾才能使用-Include


推荐阅读