如何将多个子目录中的源文件与列表匹配,然后使用 powershell 复制到目标 [英] How to match source files in multiple subdirectories with a list and then copy to destination using powershell

查看:63
本文介绍了如何将多个子目录中的源文件与列表匹配,然后使用 powershell 复制到目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎一直在忙这个,而且我没有很多时间.我想将文件从多个子目录复制到一个目的地.现在的要求是这些文件必须与我在列表中的内容相匹配.这些文件具有不同的扩展名.看看我在下面尝试了什么:

I seem to be going round and round with this one and I don't have a lot of time. I want to copy files from multiple subdirectories to a destination. Now the requirement is that these files must match what I have on a list. These files have different extensions. See what I tried below:

$source = "C:\pike"
$destination = "c:\pike2"
$filter = get-content 'C:\same.txt'
Get-ChildItem $source -recurse | Where-Object { $_.Name -match $filter } |   Copy-Item -Destination $destination

推荐答案

代替 -match,使用 -contains 运算符来测试名称是否在 <代码>$filter 数组:

Instead of -match, use the -contains operator to test whether the name is in the $filter array:

Where-Object { $filter -contains $_.Name }

<小时>

在 PowerShell 版本 >= 3.0 中,您还可以使用 -in 运算符:

Where-Object { $_.Name -in $filter }

这篇关于如何将多个子目录中的源文件与列表匹配,然后使用 powershell 复制到目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆