[英]Unable to pass Powershell argument to exclude parameter from Get-ChildItem
[英]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.