Powershell - 通过相似的名称获取文件大小
Powershell - Get file size by similar name
当我 运行 "Get-ChildItem
" PowerShell returns 下面
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2021-05-02 9:45 PM 47726 abc.123.Copy.2.pdf
-a---- 2021-05-02 9:45 PM 47726 abc.123.Copy.3.pdf
-a---- 2021-05-02 9:45 PM 47726 abc.123.Copy.4.pdf
-a---- 2021-05-02 9:45 PM 47726 abc.123.Copy.6.pdf
-a---- 2021-05-02 9:45 PM 47726 zxc.098.Copy.2.pdf
-a---- 2021-05-02 9:45 PM 47726 zxc.098.Copy.3.pdf
-a---- 2021-05-02 9:45 PM 47726 zxc.098.Copy.4.pdf
-a---- 2021-05-02 9:45 PM 47726 zxc.098.Copy.5.pdf
-a---- 2021-05-02 9:45 PM 47726 zxc.098.Copy.6.pdf
-a---- 2021-05-02 9:45 PM 47756 poi.787.Copy.1.pdf
我如何找出如何根据类似的起始名称获取文件的数量及其总大小以输出如下所示的内容?
Total size of similar files:
abc = 190,904
zxc = 238,630
poi = 47756
Total files:
abc = 4
zxc = 5
poi = 1
假设你想按拆分字符串的第一个标记对文件进行分组,并且它们都遵循此命名约定,你可以使用 Group-Object
to group them and then loop over them to get the desired output. I would advise you to output an object instead of a string since an object can be exported to CSV 并轻松排序/过滤。
运行 的代码类似于:
Get-ChildItem /path/to/files -File | Group-Object { $_.Name.Split('.')[0] } | ForEach-Object {
[pscustomobject]@{
Count = $_.Count
StartingName = $_.Name
TotalLength = [System.Linq.Enumerable]::Sum([int64[]]$_.Group.Length)
}
}
至于根据您的示例数据测试输出的外观:
Count StartingName TotalLength
----- ------------ -----------
4 abc 190904
5 zxc 238630
1 poi 47756
当我 运行 "Get-ChildItem
" PowerShell returns 下面
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2021-05-02 9:45 PM 47726 abc.123.Copy.2.pdf
-a---- 2021-05-02 9:45 PM 47726 abc.123.Copy.3.pdf
-a---- 2021-05-02 9:45 PM 47726 abc.123.Copy.4.pdf
-a---- 2021-05-02 9:45 PM 47726 abc.123.Copy.6.pdf
-a---- 2021-05-02 9:45 PM 47726 zxc.098.Copy.2.pdf
-a---- 2021-05-02 9:45 PM 47726 zxc.098.Copy.3.pdf
-a---- 2021-05-02 9:45 PM 47726 zxc.098.Copy.4.pdf
-a---- 2021-05-02 9:45 PM 47726 zxc.098.Copy.5.pdf
-a---- 2021-05-02 9:45 PM 47726 zxc.098.Copy.6.pdf
-a---- 2021-05-02 9:45 PM 47756 poi.787.Copy.1.pdf
我如何找出如何根据类似的起始名称获取文件的数量及其总大小以输出如下所示的内容?
Total size of similar files:
abc = 190,904
zxc = 238,630
poi = 47756
Total files:
abc = 4
zxc = 5
poi = 1
假设你想按拆分字符串的第一个标记对文件进行分组,并且它们都遵循此命名约定,你可以使用 Group-Object
to group them and then loop over them to get the desired output. I would advise you to output an object instead of a string since an object can be exported to CSV 并轻松排序/过滤。
运行 的代码类似于:
Get-ChildItem /path/to/files -File | Group-Object { $_.Name.Split('.')[0] } | ForEach-Object {
[pscustomobject]@{
Count = $_.Count
StartingName = $_.Name
TotalLength = [System.Linq.Enumerable]::Sum([int64[]]$_.Group.Length)
}
}
至于根据您的示例数据测试输出的外观:
Count StartingName TotalLength
----- ------------ -----------
4 abc 190904
5 zxc 238630
1 poi 47756