批处理之家's Archiver

victorwoo 发表于 2014-8-25 09:53

PowerShell 技能连载 - 用正则表达式搜索文件

原始链接:[url=http://blog.vichamp.com/powershell/tip/2014/08/18/searching-files-with-regular-expressions/]PowerShell 技能连载 - 用正则表达式搜索文件[/url]
发表日期:2014-08-18
[hr]
[i]适用于 PowerShell 所有版本[/i]

`Get-ChildItem` 不支持高级的文件过滤。当您使用简单的通配符时,无法利用上正则表达式。

要用上正则表达式,需要增加一个过滤用的 cmdlet 和 `-match` 操作符。

这个例子将在 Windows 目录中查找所有文件名包含至少 2 位数字,且文件名不超过 8 个字符的文件:[code]
Get-ChildItem -Path $env:windir -Recurse -ErrorAction SilentlyContinue |
  Where-Object { $[i].BaseName -match '\d{2}' -and $[/i].Name.Length -le 8 }
[/code]本文国际来源:[url=http://powershell.com/cs/blogs/tips/archive/2014/08/18/searching-files-with-regular-expressions.aspx]Searching Files with Regular Expressions[/url]

sxw 发表于 2014-9-4 18:39

楼主写错了 变量  Get-ChildItem -Path $env:windir -Recurse -ErrorAction SilentlyContinue |  Where-Object { $_.BaseName -match '\d{2}' -and $_.Name.Length -le 8 }

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.