Powershell 计数文件扩展名以及每个扩展名的总文件大小
Powershell Count File Extensions with Total File Size for Each Extension
我正在上初学者的PowerShell课程,在寻求帮助时,讲师完全没有用。我尝试执行的任务是:在我的计算机中搜索以 .doc、.docx、.xls 或 .xlsx 文件扩展名结尾的文件。
将文件名和大小(按文件扩展名分组)输出到名为“File_Summary.txt”的文本文件。输出还应以文件总数和每个文件扩展名的总文件大小作为结尾。到目前为止我创建的代码是:
$path = ".\"
$destination = "C:\Users\StayPositibve\Desktop\pleasework1.txt"
$results = Get-ChildItem .\* -Recurse | where {$_.extension -in ".doc",".docx",".xls",".xlsx"} | Select-Object Name, Extension, @{Name="Kbytes";Expression={ "{0:N0}" -f ($_.Length / 1Kb) }}
$results | Sort-Object -Property extension | Out-File $destination
$countFiles = Get-ChildItem .\* -Recurse | where {$_.extension -in ".doc",".docx",".xls",".xlsx"}
$countFiles | Group-Object -Property extension | Out-File $destination -Append
代码运行(虽然我确定它的编码很糟糕);但是,我不知道如何将每个扩展名的总文件大小加在一起。我有每个扩展名的文件总数,但没有总大小。感谢任何人能给我的帮助!
您想按照 TessellatingHeckler 的建议使用 Measure-Object
。这将完成您想要完成的工作:
$path = ".\"
$destination = "C:\Users\StayPositibve\Desktop\pleasework1.txt"
$results = Get-ChildItem .\* -Recurse | where {$_.extension -in ".doc",".docx",".xls",".xlsx"} | Select-Object Name, Extension, @{Name="Kbytes";Expression={ "{0:N0}" -f ($_.Length / 1Kb) }}
$results | Sort-Object -Property extension | Out-File $destination
$results|group extension|select Count,Name,@{l='Total KB';e={$_.Group | Measure-Object -Property kbytes -Sum|select -expand sum}}| Out-File $destination -Append
我重新使用 $results
,并像您一样进行分组,但随后我使用 Select
添加新的 属性,在其中我使用 Measure-Object
传送到 Select -Expand Sum
.
我正在上初学者的PowerShell课程,在寻求帮助时,讲师完全没有用。我尝试执行的任务是:在我的计算机中搜索以 .doc、.docx、.xls 或 .xlsx 文件扩展名结尾的文件。 将文件名和大小(按文件扩展名分组)输出到名为“File_Summary.txt”的文本文件。输出还应以文件总数和每个文件扩展名的总文件大小作为结尾。到目前为止我创建的代码是:
$path = ".\"
$destination = "C:\Users\StayPositibve\Desktop\pleasework1.txt"
$results = Get-ChildItem .\* -Recurse | where {$_.extension -in ".doc",".docx",".xls",".xlsx"} | Select-Object Name, Extension, @{Name="Kbytes";Expression={ "{0:N0}" -f ($_.Length / 1Kb) }}
$results | Sort-Object -Property extension | Out-File $destination
$countFiles = Get-ChildItem .\* -Recurse | where {$_.extension -in ".doc",".docx",".xls",".xlsx"}
$countFiles | Group-Object -Property extension | Out-File $destination -Append
代码运行(虽然我确定它的编码很糟糕);但是,我不知道如何将每个扩展名的总文件大小加在一起。我有每个扩展名的文件总数,但没有总大小。感谢任何人能给我的帮助!
您想按照 TessellatingHeckler 的建议使用 Measure-Object
。这将完成您想要完成的工作:
$path = ".\"
$destination = "C:\Users\StayPositibve\Desktop\pleasework1.txt"
$results = Get-ChildItem .\* -Recurse | where {$_.extension -in ".doc",".docx",".xls",".xlsx"} | Select-Object Name, Extension, @{Name="Kbytes";Expression={ "{0:N0}" -f ($_.Length / 1Kb) }}
$results | Sort-Object -Property extension | Out-File $destination
$results|group extension|select Count,Name,@{l='Total KB';e={$_.Group | Measure-Object -Property kbytes -Sum|select -expand sum}}| Out-File $destination -Append
我重新使用 $results
,并像您一样进行分组,但随后我使用 Select
添加新的 属性,在其中我使用 Measure-Object
传送到 Select -Expand Sum
.