P R*_*ers 3 size powershell file
我正在参加针对初学者的 PowerShell 课程,而讲师在寻求帮助时完全没用。我想要执行的任务是:在我的计算机中搜索以 .doc、.docx、.xls 或 .xlsx 文件扩展名结尾的文件。\n将文件名和大小(按文件扩展名分组)输出到名为\xe2\x80\x9cFile_Summary.txt\xe2\x80\x9d。输出还应包含每个文件扩展名的文件总数和总文件大小。到目前为止我创建的代码是:
\n\n$path = ".\\"\n$destination = "C:\\Users\\StayPositibve\\Desktop\\pleasework1.txt"\n\n$results = Get-ChildItem .\\* -Recurse | where {$_.extension -in ".doc",".docx",".xls",".xlsx"} | Select-Object Name, Extension, @{Name="Kbytes";Expression={ "{0:N0}" -f ($_.Length / 1Kb) }}\n$results | Sort-Object -Property extension | Out-File $destination\n$countFiles = Get-ChildItem .\\* -Recurse | where {$_.extension -in ".doc",".docx",".xls",".xlsx"} \n$countFiles | Group-Object -Property extension | Out-File $destination -Append\nRun Code Online (Sandbox Code Playgroud)\n\n代码运行(尽管我确信这是糟糕的编码);但是,我不知道如何将每个扩展名的总文件大小加在一起。我有每个扩展名的文件总数,但没有总大小。感谢任何人都可以给我的帮助!
\n您想按照 TesselttingHeckler 的建议使用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
Run Code Online (Sandbox Code Playgroud)
我像您一样重复使用$results, 和 group ,但随后我使用添加一个新属性,在其中通过管道连接到Select来获取每个分组的总大小。Measure-ObjectSelect -Expand Sum
| 归档时间: |
|
| 查看次数: |
7392 次 |
| 最近记录: |