1

我正在尝试在文件夹和子文件夹中查找所有文件扩展名并生成一个列表。我找到了一个 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
}

这将在文件夹路径中为每个文件夹提供一个新的标题,我需要结果是这样的

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

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

4

2 回答 2

1

该脚本现在有效。我需要改变

foreach($folder in $folders)

foreach($folder in (Get-Content $file))
于 2013-11-14T09:41:32.350 回答
0

It is not short or sweet, but try this:

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
于 2013-11-14T02:30:22.027 回答