powershell - 在 PowerShell Get-ChildItem -Exclude 不与 -Recurce 参数一起使用

标签 powershell

enter image description here我想递归搜索所有目录,除了以 _(下划线)开头的目录。

例如:_archive_DO_NOT_DELETE_processing

我使用了下面的代码,但它不起作用。似乎 exclude 不适用于 recurse 。有什么想法吗?

$exclude = @("_*")
$source = "C:\code\Powershell\delete empty folders\New folder\" 
$allitems = Get-ChildItem $source -Directory -Exclude $exclude -Recurse
foreach($myItem in $allitems)
{
  echo $myItem.fullname
} 

输出:

C:\code\Powershell\delete empty folders\New folder\New folder\New folder
C:\code\Powershell\delete empty folders\New folder\New folder\New folder\New folder
C:\code\Powershell\delete empty folders\New folder\New folder\New folder (2)
C:\code\Powershell\delete empty folders\New folder\New folder\New folder (3)
C:\code\Powershell\delete empty folders\New folder\New folder\New folder (3)\New folder
C:\code\Powershell\delete empty folders\New folder\_archive\New folder

So, as I told before I want to avoid the directory started with underscore, hence the last line should not come.

Even I used below code as well, still same output:

$exclude = @("_*")
$source = "C:\code\Powershell\delete empty folders\New folder\"
$allitems = Get-ChildItem $source -Recurse -Directory |
            Where {$_.FullName -notlike $exclude}
foreach($myItem in $allitems)
{
    echo $myItem.fullname
}

最佳答案

这里发生的是 Get-ChildItem 的 -exclude 参数基于项目的 name 属性工作。使用通配符时,名称必须与非通配符部分匹配。当您的目录名称以空格开头时,不会发生这种情况。我们来看一个例子:

# Create two files, one starts with a space, the other doesn't
Set-Content -Path " data1.txt" -Value $null
Set-Content -Path "data2.txt" -Value $null

gci -name "data*"
 data2.txt

gci -name " data*"
 data1.txt

结果是单场比赛,使用了强硬的通配符。原因是data1.txt的第一个字符是空格,与参数的第一个字符不匹配,即d

-exclude 以狡猾而臭名昭著。通过管道将结果传递到 where-object 通常更容易解决。像这样,

Get-ChildItem $source -Directory -recurse | ? {$_.fullname -NotMatch "\\\s*_"} | % { $_.fullname }
# "\\\s*_" is regex for \, any amount of whitespace, _ 
C:\code\Powershell\delete empty folders\New folder\New folder
C:\code\Powershell\delete empty folders\New folder\New folder\New folder
C:\code\Powershell\delete empty folders\New folder\New folder\New folder (2)
C:\code\Powershell\delete empty folders\New folder\New folder\New folder (3)
C:\code\Powershell\delete empty folders\New folder\New folder\New folder\New folder
C:\code\Powershell\delete empty folders\New folder\New folder\New folder (3)\New folder

Get-ChildItem $source -Directory -recurse | ? {$_.fullname -Match "\\\s*_"} | % { $_.fullname }
C:\code\Powershell\delete empty folders\New folder\ _archive
C:\code\Powershell\delete empty folders\New folder\ _archive\New folder

请注意,匹配需要评估 FullName 属性。仅使用 Name 只会匹配叶级别。

关于powershell - 在 PowerShell Get-ChildItem -Exclude 不与 -Recurce 参数一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51666987/

相关文章:

.net - 执行 [adsisearcher] 并将其存储在哈希表或字典集合列表中的最快方法

powershell - Azure PowerShell commandlet 和命令行工具之间有什么区别?

c# - 在 powershell 和 C# 之间共享变量

powershell - 将运行的命令输出存储在远程系统上,并以正确的格式列出,然后从Powershell中的列表中选择选项?

loops - Powershell $Host.UI.ReadKey() 菜单循环 - 按键未重置

arrays - 在 Powershell : Why does testing an empty array behave differently from an empty string? 中测试 null

c# - 子节点中的属性需要 xml 命名空间前缀

powershell - 是否可以在 PowerShell 中使用带有嵌套哈希表的 splatting?

powershell - 搜索并获取文件,但如果值为 null,则忽略代码

PowerShell 脚本和在运行时加载/调用模块