Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell在根目录中查找包含excludes的文件_Powershell - Fatal编程技术网

Powershell在根目录中查找包含excludes的文件

Powershell在根目录中查找包含excludes的文件,powershell,Powershell,我有以下代码来尝试在我的项目中定位文件夹 $rootDir = Split-Path -Parent $MyInvocation.MyCommand.Path $fileName = "Scripts*" $paths = gci -path $rootDir -filter $fileName -Recurse | Select-Object -Expand FullName 我遇到的问题是,我不希望它在bin或obj文件夹中找到文件夹。有没有办法将这些人排除在搜索之外 基

我有以下代码来尝试在我的项目中定位文件夹

$rootDir = Split-Path -Parent $MyInvocation.MyCommand.Path

    $fileName = "Scripts*"
    $paths = gci -path $rootDir -filter $fileName -Recurse | Select-Object -Expand FullName
我遇到的问题是,我不希望它在bin或obj文件夹中找到文件夹。有没有办法将这些人排除在搜索之外

基本上,我需要递归地搜索解决方案文件夹中的文件夹名,不包括bin/obj位置


当我尝试使用-Exclude时,它会导致编译错误。我想一定有比这更简单的方法来实现它。

我认为问题在于-Exclude只接受字符串。您需要使用Where对象来过滤多个字符串

$paths = gci -path $rootDir -filter $fileName -Recurse | Where-Object {$_.FullName -notlike "*bin*\*" -and $_.FullName -notlike "*obj*\*"} | Select-Object -Expand FullName

问题是-Exclude被传递给提供程序,而文件系统提供程序通常不擅长过滤。我建议使用Where子句并使用一个-Match运算符(-inotmatch是不区分大小写的反向匹配,因此它排除与文本匹配的任何内容),如下所示:

$rootDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$fileName = "Scripts*"
$paths = gci -path $rootDir -filter $filename -Recurse | Select-Object -Expand FullName | ?{$_ -inotmatch "\\(bin|obj)\\"}
它查找以反斜杠()开头,后跟bin或obj,后跟另一个反斜杠的字符串