powershell - 将字符串与数组中的特定数字匹配
问题描述
我想创建一个脚本,使用Get-ChildItem
cmdlet 在目录中搜索特定的“.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
吗?
解决方案
由于您已经将-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
推荐阅读
- c++ - 使用 Qt 绘制像素完美的圆
- css - 如何控制文本框显示
- python-3.x - 如何在 virtualenwrapper 中安装特定版本的 python?
- anylogic - 如何在 Ubuntu 操作系统中使用 USB 加密狗激活 AnyLogic?
- c# - 如何对打印到物理打印机的功能进行单元测试?
- r - 通过给定分布更改分类变量中字段的分布
- arrays - vertx 决定 jsonobject vs jsonarray
- python-3.x - 使用 matplotlib(或 seaborn)绘制 d 轨道图
- git - 如何创建新分支并将来自另一个分支的差异合并到其中,但单个目录除外
- machine-learning - coremltools管道输入节点未连接到模型的其余部分