我希望在PowerShell中排除所有目录的搜索。无论是 FileInfo
还是 DirectoryInfo
都包含了 Attributes
属性,这似乎正是我想要的,但我找不到如何基于它进行过滤的方法。
ls | ? { $_.Attributes -ne 'Direcory' }
ls | ? { $_.Attributes -notcontains 'Direcory' }
没用。我该怎么做?
我希望在PowerShell中排除所有目录的搜索。无论是 FileInfo
还是 DirectoryInfo
都包含了 Attributes
属性,这似乎正是我想要的,但我找不到如何基于它进行过滤的方法。
ls | ? { $_.Attributes -ne 'Direcory' }
ls | ? { $_.Attributes -notcontains 'Direcory' }
没用。我该怎么做?
PSIsContainer
属性:gci | ? { !$_.PSIsContainer }
你的方法也可以运行,但是需要像这样:
gci | ? { !($_.Attributes -band [IO.FileAttributes]::Directory) }
由于属性是枚举和位掩码,因此可以这样做。
或者,对于您的其他方法:
gci | ? { "$($_.Attributes)" -notmatch "Directory" }
这将导致属性被转换为一个字符串(可能看起来像“Directory, ReparsePoint”),而在一个字符串上你可以使用-notmatch
运算符。
PowerShell v3终于在Get-ChildItem
上拥有了-Directory
参数:
Get-ChildItem -Directory
gci -ad
Get-ChildItem | Where-Object {$_ -isnot [IO.DirectoryInfo]}
或者更简洁,但难以阅读的版本:
gci | ? {$_ -isnot [io.directoryinfo]}
Credit goes to @Joey for his insightful comment using the -is operator :)Get-ChildItem | Where-Object {$_ -is [IO.FileInfo]}
或者:
gci | ? {$_ -is [io.fileinfo]}
Get-ChildItem | Where-Object {$_ -is [IO.DirectoryInfo]}
或者:
gci | ? {$_ -is [io.directoryinfo]}
ls | ?{$_.GetType() -ne [System.IO.DirectoryInfo]}
使用get-childitem(或ls或dir)返回类型为System.IO.DirectoryInfo的目录,并且文件的类型为System.IO.FileInfo。在PowerShell中将这些类型作为文字字面值使用时,需要将它们放在括号中。
-is
运算符来实现这一点。因此,它可以简写为:gci | ? {$_ -is [IO.FileInfo]}
。 - Joey不同方法的时间,短目录下5000次迭代和System32目录下25次迭代的时间。测量使用以下工具:
(measure-command { for ($i=0;$i -lt 5e3;$i++) {
$files= gci | ? { !$_.PSIsContainer } # this is target to measure
} } ).totalmilliseconds
从最慢到最快的结果(所有行的最终结果相同):
$files= gi *.* | ? { !$_.PSIsContainer } #5000 iterations / 25long = 15.2sec / 22s
$files= foreach ($file in gi *.*) { if ($file.mode -notmatch 'd') { $file } } # 11.8s / 20s
$files= gci | ? { !($_.Attributes -band [IO.FileAttributes]::Directory) } # 8.9s / 10.7s
$files= Get-ChildItem | Where-Object {$_.mode -notmatch 'd'} # 8.8s / 10.6s
$files= gci | ? { !$_.PSIsContainer } # 7.8s / 9.8s
$files= Get-ChildItem | ? {$_ -isnot [IO.DirectoryInfo]} # 7.6s / 9.6s
$files= gci | Where-Object {$_ -is [IO.FileInfo]} # 7.6s / 9.6s
$files= foreach ($file in gci *.*) { if ($file.mode -notmatch 'd') { $file } } #7.3s / 12.4s
$files= @( foreach ($file in gci) { if ($file.mode -notmatch 'd') { $file } } ) #3.7s / 6.4s
$files= foreach ($file in gci) { if ($file.mode -notmatch 'd') { $file } } # 3.7s / 6.4s