Powershell中不同的文件类型列表

时间:2012-12-17 20:19:05

标签: powershell

我正在尝试在Powershell中编写一个单行程序,列出文件夹(及其子文件夹)中的所有文件类型。

到目前为止我已经得到了这个:

Get-ChildItem D:\Audio -Recursive | Select-Object PSParentPath, Extension | Export-Csv D:\Test.csv

但是,我想做得更好,并为每个文件夹显示每个找到的扩展名一次。

例如,假设我在D:\ Audio \ foo中有10个mp3文件和1个jpg文件,在D:\ Audio \ bar中有5个flac文件和1个txt文件。我希望输出为:

  • foo .mp3
  • foo .jpeg
  • bar .flac
  • bar .txt

我想我应该使用Get-Unique但是如何在Extension属性上指定它而不是在Path属性上?

3 个答案:

答案 0 :(得分:34)

只需将-Unique添加到Select-Object:

Get-Childitem -Recurse | Select-Object PSParentPath,Extension -Unique

(另外,DirectoryName可能比PSParentPath更好)

答案 1 :(得分:2)

我完成了Jimmeh的解决方案,使用-file只接受文件

Get-ChildItem "c:\temp" -Recurse -file | select directoryname, Extension -Unique

答案 2 :(得分:0)

试试这个:

Get-ChildItem D:\Audio -Recurse | Group-Object psparentpath, extension | ? {(($_.Name -split ", .")[1]) -ne $Null } | ft -AutoSize @{Expression={(($_.Name.Split("\"))[$_.Name.Split("\").Length -1] -split ",")[0]};Label="Folder"}, @{Expression={($_.Name -split ", .")[1]};Label="Extension"}

要显示文件夹+扩展程序的完整路径,请尝试以下操作。

Get-ChildItem D:\Audio -Recurse | Group-Object psparentpath, extension | ? {(($_.Name -split ", .")[1]) -ne $Null } | ft -AutoSize @{Expression={($_.Name.Replace("Microsoft.PowerShell.Core\FileSystem::","") -split ", .")[0]};Label="Folder"}, @{Expression={($_.Name -split ", .")[1]};Label="Extension"}