Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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时排除多个子文件夹';s方法获取子项 我所拥有的_Powershell_Foreach - Fatal编程技术网

使用Powershell时排除多个子文件夹';s方法获取子项 我所拥有的

使用Powershell时排除多个子文件夹';s方法获取子项 我所拥有的,powershell,foreach,Powershell,Foreach,以下工作代码列出了放置脚本的所有子文件夹中的所有文件。 它只包括某些文件类型,并排除某个文件夹中的所有文件。到目前为止还不错 $fileInclude = @("*.txt", "*.hgr", "*.dat") $folderExclude = "C:\folder2" $scriptPath = split-path -parent $MyInvocation.MyCommand.Definition gci $scriptPath -recurse -inc

以下工作代码列出了放置脚本的所有子文件夹中的所有文件。
它只包括某些文件类型,并排除某个文件夹中的所有文件。到目前为止还不错

$fileInclude = @("*.txt", "*.hgr", "*.dat")           
$folderExclude = "C:\folder2"    
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition

gci $scriptPath -recurse -include $fileFilter | Where {$_.FullName -notlike "$folderExclude*"} 
我的测试的文件结构如图所示

C:.
│   Myscript.ps1
│
├───folder1
│   │   file1.dat
│   │   file1.hgr
│   │   file1.txt
│   │
│   └───folder3
│           file3.dat
│           file3.hgr
│           file3.txt
│
└───folder2
        file3.dat
        file3.hgr
        file3.txt
我想要什么 现在我想替换第二行

$folderExclude = "C:\folder2"
使用数组。后来有几十条路在里面

$folderExclude = @(
        "C:\folder2", 
        "C:\folder1\folder3"    
        )
显然,这不起作用,因为
-notlike
需要一个字符串而不是字符串数组


我不知道该如何实现这一点。我想我需要一个for each循环?

一种方法是从$folderExclude构建一个与任何排除字符串匹配的正则表达式(使用交替),然后使用-notmatch而不是-notlike

$folderExclude = @(
    "C:\folder2", 
    "C:\folder1\folder3"    
    )

[regex]$Exclude_regex = ‘(?i)^(‘ + (($folderExclude |foreach {[regex]::escape($_)}) –join “|”) + ‘)’

$fileInclude = @("*.txt", "*.hgr", "*.dat")           
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition

gci $scriptPath -recurse -include $fileFilter | Where {$_.FullName -notmatch $Exclude_regex}

编辑:这可能不像嵌套的where对象那样“时髦”,但它会更快。

您也可以这样做:

$exclude = @('c:\folder2*', 'c:\folder1\folder3'} 
gci | Where {$fn = $_.Fullname; ($exclude | Where {$fn -like $_}).count -eq 0}
如果您使用的是V4,则可以使用方便的新公共参数-PipelineVariable:

gci -pv fse | Where {($exclude | Where {$fse.FullName -like $_}).count -eq 0}
注意,这种方法有一个缺陷,如果folder1包含一个名为folder3foobar的文件,该文件将被排除。要解决此问题,您需要将排除术语修改为c:\folder1\folder3*,然后在与fse.FullName进行比较时,如果fse是目录而不是文件,则需要附加反斜杠

下面是另一种基于
-match
的方法,允许在左侧使用数组:

 gci | Where {!($exclude -match [regex]::escape($_.Fullname))}

在这种情况下,如果其中一个目录与$exclude术语数组匹配,则将有输出,该输出被强制为true,但随后被
反转

我不明白您的第一个示例到底是如何工作的。你能给我解释一下吗?Where(或Where Object)命令只需要返回一个可以解释为true或false的值。在该谓词脚本块内(在{}内),只要最终结果正确返回true或false(或可以强制为true/false的内容),就可以执行所需的任何代码。因此,在Where脚本块中,我保存了目录或文件的全名。然后我创建了一个新的管道,在那里我将$exclude数组的每个元素发送到该管道。它被包装在()中以收集输出。然后我们查询,看看是否有任何与count prop相关的输出,您的第一个示例对我有效。但是,我无法使用您或mjolinor的正则表达式解决方案。查看有关尝试正则表达式时发生的情况的代码?我测试了一下,它对我很有效。