2017-03-23 65 views
1

我想要获取同一根文件夹下的子文件夹中的所有文件,它们都包含子文件夹名称中相同的字符串(“foo”)。下面给我没有错误,没有输出。我不知道我错过了什么。获取包含其名称中的字符串的子文件夹的内容

Get-ChildItem $rootfolder | where {$_.Attributes -eq 'Directory' -and $_.BaseName -contains 'foo'}) | echo $file 

最后,我想不只是附和他们的名字,但每个文件移动到目标文件夹。

谢谢。

回答

1

这里是包括每个文件夹的子文件移动到一个新的目标文件夹的解决方案:

$RootFolder = '.' 
$TargetFolder = '.\Test' 

Get-ChildItem $RootFolder | Where-Object {$_.PSIsContainer -and $_.BaseName -match 'foo'} | 
    ForEach-Object { Get-ChildItem $_.FullName | 
    ForEach-Object { Move-Item $_.FullName $TargetFolder -WhatIf } } 

删除-WhatIf当你快乐时它做它应该是什么。

如果您(例如)想要排除文件夹的子目录,或者要将子项包括在这些路径的所有子文件夹中,但不包括文件夹本身,则可能需要修改Get-ChildItem $_.FullName部分。

+0

..我尝试了一切,但那。谢谢。 – physlexic

0

更换

Get-ChildItem $rootfolder | where {$_.Attributes -match 'Directory' -and $_.basename -Match 'foo'}) | echo $file 

Get-ChildItem $rootfolder | where {($_.Attributes -eq 'Directory') -and ($_.basename -like '*foo*')} | Move-Item $targetPath 

您的要求:

,所有包含相同的字符串( “富”)

您必须使用-like比较运算符。对于完全匹配,我将使用-eq(区分大小写的版本是-ceq)而不是-match,因为它用于匹配子字符串和模式。

工作流程: 获取目录中的所有文件,通过管道将其发送到您的基于性能的滤波凡对象cmdlet属性和基名。过滤完成后,将其发送到cmdlet Move-Item。

+0

谢谢..但这会移动子文件夹和内容..我只是想要移动每个子文件夹的内容 – physlexic

+0

'Get-ChildItem $ rootfolder -File -Recurse |其中{$ _。basename-like'* foo *'} | Move-Item $ targetPath'试试这个。您必须首先获取所有文件(不包括文件夹),然后对其进行过滤。让我知道这是否有效,并用解释来更新答案。 “ForEach” – pandemic

0

将前两个变量适应您的环境。

$rootfolder = 'C:\Test' 
$target = 'X:\path\to\whereever' 
Get-ChildItem $rootfolder -Filter '*foo*' | 
    Where {$_.PSiscontainer} | 
    ForEach-Object { 
     "Processing folder: {0} " -f $_ 
    Move $_\* -Destination $target 
    } 
相关问题