[英]Powershell GCI include with arraylist
$pdfs = @(gci $pioneerDest -Include $arrPDFName.Item(0), $arrPDFName.Item(0) | foreach {write-output $_.name})
So I want to grab pdfs from a directory. 所以我想从目录中获取pdf。 However I only want the pdfs that will be the arraylist $arrPDFName
. 但是我只想要将是arraylist $arrPDFName
的pdf。 I cannot find a way to do this. 我找不到解决办法。 Does anyone have any idea of using the powershell's collections with Get-ChildItem
? 有没有人知道将Powershell的集合与Get-ChildItem
?
Assuming something simple for your list like a list of file names you were on the right track. 假设您的列表很简单,例如文件名列表,那么您就走对了。
-Include
accepts arrays so there is not need to break it out. -Include
接受数组,因此不需要进行拆分。 Just use it explicitly. 只需明确使用它即可。 In practice -include
and -exclude
work only when -Recurse
is used. 实际上,仅当使用-Recurse
时, -include
和-exclude
起作用。 You can find a little more information on that from Get-ChildItem -Filter Array . 你可以找到从上一点点更多的信息获取,ChildItem -Filter阵列 。 Technet is also a good resource for this: Get-ChildItem
Technet也是一个很好的资源: Get-ChildItem
$arrPDFName = "text.txt","test.csv"
$pioneerDest = "c:\temp"
$pdfs = Get-ChildItem $pioneerDest -include $arrPDFName -Recurse | Select-Object -Expand Name
I removed the foreach-object
loop since you were just using that to return properties. 我删除了foreach-object
循环,因为您只是使用它来返回属性。 That is what Select-Object
is for. 那就是Select-Object
目的。 You might also consider swapping out Name
for FullName
to get the full path. 您也可以考虑将Name
换成FullName
以获取完整路径。
Feed the list of names into gci through the pipeline, it will handle it. 通过管道将名称列表输入gci,它将进行处理。
$arrPDFName | gci
If they are actually full filenames in the current directory , and you know they all exist, that should be enough. 如果它们实际上是当前目录中的完整文件名,并且您知道它们都存在,那么就足够了。
If they're not in the current directory, I'd be quite tempted to 如果它们不在当前目录中,我很想
$arrPDFName |% { "$pioneerDest\$_" } | gci
To loop over them and make them all full path names on the way into gci
. 遍历它们,并使它们在进入gci
的途中全部具有完整路径名。 Or cd $pioneerDest
first. 或cd $pioneerDest
首先。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.