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

Fle*_*lea 24 powershell powershell-2.0

我正在使用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
        }
    }
Run Code Online (Sandbox Code Playgroud)

我已经试过各种方法对其进行过滤,\obj*obj*\obj\ 但似乎没有任何工作.

谢谢你的帮助.

man*_*lds 56

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

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

PS:提醒一句-甚至不考虑使用-ExcludeCopy-Item本身.


小智 7

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

$files = gci 'C:\' -Recurse  | Where-Object{!($_.PSIsContainer)}
Run Code Online (Sandbox Code Playgroud)


Bjo*_*aen 5

Get-ChildItem -Path $SourcePath -File -Recurse | 
Where-Object { !($_.FullName).StartsWith($DestinationPath) } 
Run Code Online (Sandbox Code Playgroud)