繁体   English   中英

无法使用Powershell中的Get-ChildItem -Exclude参数排除目录

[英]Unable to exclude directory using Get-ChildItem -Exclude parameter in Powershell

我正在使用Powershell v 2.0。 并将文件和目录从一个位置复制到另一个位置。 我使用字符串[]来过滤掉文件类型,还需要过滤掉被复制的目录。 正在过滤掉文件,但是,我试图过滤obj的目录不断被复制。

$exclude = @('*.cs', '*.csproj', '*.pdb', 'obj')
    $items = Get-ChildItem $parentPath -Recurse -Exclude $exclude
    foreach($item in $items)
    {
        $target = Join-Path $destinationPath $item.FullName.Substring($parentPath.length)
        if( -not( $item.PSIsContainer -and (Test-Path($target))))
        {
            Copy-Item -Path $item.FullName -Destination $target
        }
    }

我已经尝试了各种方法来过滤它, \\obj*obj*\\obj\\但似乎没有任何效果。

谢谢你的帮助。

-Exclude参数非常破碎。 我建议你使用Where-Object (?{})过滤你不想要的目录。 例如:

$exclude = @('*.cs', '*.csproj', '*.pdb')
$items = Get-ChildItem $parentPath -Recurse -Exclude $exclude | ?{ $_.fullname -notmatch "\\obj\\?" }

PS:警告的话 - 甚至不要考虑在Copy-Item本身上使用-Exclude

我使用它来列出根目录下的文件,但不包括目录

$files = gci 'C:\' -Recurse  | Where-Object{!($_.PSIsContainer)}
Get-ChildItem -Path $SourcePath -File -Recurse | 
Where-Object { !($_.FullName).StartsWith($DestinationPath) } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM