批处理之家's Archiver

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

PowerShell 技能连载 - 获取指定扩展名的文件

原始链接:[url=http://blog.vichamp.com/powershell/tip/2014/08/15/getting-files-with-specific-extensions-only/]PowerShell 技能连载 - 获取指定扩展名的文件[/url]
发表日期:2014-08-15
[hr]
[i]适用于 PowerShell 所有版本[/i]

当您使用 `Get-ChildItem` 来获取一个文件列表时,您可能会注意到 `-Filter` 参数有时候会导致返回比你预期的更多的文件。

以下是一个例子。这段代码并不只是返回“.ps1”扩展名的文件,而也会返回“.ps1xml”扩展名的文件:[code]
Get-ChildItem -Path C:\windows -Recurse -ErrorAction SilentlyContinue -Filter *.ps1
[/code]要限制只返回您需要的扩展名的文件,请用一个 cmdlet 来过滤结果:[code]
Get-ChildItem -Path C:\windows -Recurse -ErrorAction SilentlyContinue -Filter [i].ps1 [/i]
  Where-Object { $_.Extension -eq '.ps1' }
[/code]这将只返回您指定的扩展名的文件。


本文国际来源:[url=http://powershell.com/cs/blogs/tips/archive/2014/08/15/getting-files-with-specific-extensions-only.aspx]Getting Files with Specific Extensions Only[/url]

页: [1]

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