![](/img/trans.png)
[英]Using Get-ChildItem -Exclude or -Include returns nothing
[英]Prioritize -Exclude over -Include in Get-ChildItem
我正在尝试使用Powershell将源代码清理到另一个文件夹中:
dir $sourceDir\* -Recurse -Exclude */bin/*,*/obj/* -Include *.sln, *.myapp, *.vb, *.resx, *.settings, *.vbproj, *.ico, *.xml
看起来一切正常,但是, -Include
指令将-Exclude
之前的文件列入白名单,因此,例如,包括了/bin/
下的.XML
文件。 我希望-Exclude
优先于-Include
,因此在上述脚本中始终排除/bin/
和/obj/
文件夹。
在Powershell中有可能无需编写太多代码?
您可以切换到后期过滤以排除不需要的目录:
dir $sourceDir\* -Recurse -Include *.sln, *.myapp, *.vb, *.resx, *.settings, *.vbproj, *.ico, *.xml |
where {$_.fullname -notmatch '\\bin\\|\\obj\\'}
使用-like而不是-match:
dir $sourceDir\* -Recurse -Include *.sln, *.myapp, *.vb, *.resx, *.settings, *.vbproj, *.ico, *.xml |
where { ($_.fullname -notlike '*\bin\*') -and ($_.fullname -notlike '*\obj\*') }
这是我的看法:
param(
$sourceDir="x:\Source",
$targetDir="x:\Target"
)
function like($str,$patterns){
foreach($pattern in $patterns) { if($str -like $pattern) { return $true; } }
return $false;
}
$exclude = @(
"*\bin\*",
"*\obj\*"
);
$include = @(
"*.sln",
"*.myapp",
"*.vb",
"*.resx",
"*.settings",
"*.vbproj",
"*.ico",
"*.xml"
);
dir $sourceDir\* -Recurse -Include $include | where {
!(like $_.fullname $exclude)
}
可能不是很像Powershell,但它可以工作。 我从这里开始喜欢函数 。
欢迎任何简短的答案-请继续并提出替代方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.