在文件夹中查找文件扩展名和总大小

时间:2013-11-13 14:53:27

标签: file powershell file-extension

我正在尝试在文件夹和子文件夹中找到所有文件扩展名并生成列表。我找到了一个oneliner,但它不会生成我想要的列表。我有多条路径所以我这样做。

$date = get-date -Format d
$File = "C:\NoBackup\FolderPaths.txt"

foreach ($Folder in (Get-Content $File)) {
Get-ChildItem $Share -Recurse -ErrorAction SilentlyContinue | Group-Object extension | Select-Object @{Name="Folder";Expression={$Folder}}, name, @{n='TotalSize';e={$_.group | ForEach-Object -Begin {$size=0} -Process {$size += ([decimal]::round($_.Length / 1MB))} -End {"$size MB"}}} | Sort-Object -Property 'TotalSize' -Descending | Format-Table -AutoSize
}

这将在folderpaths中给出一个新的头文件foreach文件夹,我需要结果像这样

           .ext1    .ext2    .ext3    .ext4
D:\Folder1   5MB     12MB     20MB      8MB
D:\Folder2  10MB     54MB     12MB      3MB
D:\Folder3   2MB     12MB     20MB    100MB

我无法找到重写代码以获得我需要的东西。希望你能帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

该脚本现在可以使用了。我需要改变

foreach($folder in $folders)

foreach($folder in (Get-Content $file))

答案 1 :(得分:0)

它不短或甜,但试试这个:

function ConvertTo-Units($decimal)
{
    $value = [decimal]::Round($decimal/1mb,2)
    $units = "MB"
    if($value -eq 0)
    {
        $value = [decimal]::Round($decimal/1kb,2)
        $units = "KB"
    }
    return "{0} {1}" -f $value,$units
}

$File = "C:\NoBackup\FolderPaths.txt"
$fileData = @{}
foreach ($folder in (Get-Content $file))
{
    $files = Get-ChildItem $folder -Recurse -ErrorAction SilentlyContinue -File
    $fileData[$folder] = $files | Select-Object extension,length | %{$h = @{}} { $h[$_.extension]  += $_.length } { $h}
}

$extensions = $fileData.Keys | % { $fileData[$_].Keys } | % tolower | Select-Object -Unique | ? { $_ }
$properties = @(
    @{Name="Folder";Expression={$_}}
)
$extensions | % {$properties += @{Name="$_";Expression=[scriptblock]::Create("ConvertTo-Units `$fileData[`$folder][""$_""]")}}
$result  = @()
foreach($folder in $folders)
{
    $result += $folder | Select-Object $properties
}
$result | ft * -AutoSize -force