我正在尝试过滤等于 .config
的文件扩展名并且文件名中不包含 vshost,因此我运行此脚本:
$files = dir
foreach($file in $files) {
if($file.Name -notcontains 'vshost' -and $file.Extension -eq '.config') {
Write-Host $file;
}
}
但输出仍然包含名称中带有 vshost
的文件:
foo.exe.config
foo.vshost.exe.config
foo2.vshost.exe.config
预期的输出是:
foo.exe.config
我缺少什么以及如何解决这个问题?
最佳答案
-notcontains
用于查找数组中的项目,例如。 $array -notcontains $singleitem
。您需要使用 -notlike
或 -notmatch
(正则表达式模式)。例如。
if($file.Name -notlike '*vshost*' -and $file.Extension -eq '.config') {
或
if($file.Name -notmatch 'vshost' -and $file.Extension -eq '.config') {
关于.net - 使用 -notcontains 过滤文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37010783/