0

I have created a directory list of files like this:

JPGS.LST:
"L:\_Testing\2016-05-20 20.22.53.jpg"
"L:\_Testing\2016-05-20 20.23.07.jpg"
"L:\_Testing\2016-05-20 20.23.12.jpg"
"L:\_Testing\2016-05-20 20.59.31.jpg"
"L:\_Testing\2016-05-20 21.19.25.jpg"
"L:\_Testing\2016-05-20 21.19.28.jpg"
"L:\_Testing\2016-05-20 21.49.09.jpg"
"L:\_Testing\2016-05-20 21.49.12.jpg"

From this list I want to get a total of all the file sizes - from the list.

I'm using this list to reduce these images. I want to run the script again to see total size of the same files after the reduction, so I have a before and after comparison. Other examples I see use a powershell directory listing...I need to be able to use the input file to determine total size of files in the directory listing.

I've tried:

$Files=Get-Content $Listfile
$totalSize = ($Files | Measure-Object -Property Length -Sum Length).Sum 

but that's not producing expected results. I'd like it to resolve to something like

$TotalSize=12.5 MB

Obi Wan....you're my only hope!

  • `$Files` returns strings, you are measuring string length. You'll need a ForEach to iterate and Get-Item to obtain the Length of each file, like my answer does. –  May 23 '18 at 06:05

3 Answers3

2
$files = Get-ChildItem $path -Recurse -File
$sum=($files | Measure-Object -Property length -Sum).sum

If you want a desired output with sum (MB, GB ..etc), check PowerShell display files size as KB, MB, or GB discussion with cool techniques.

Ketanbhut
  • 476
  • 2
  • 11
  • This doesn't seem correct. isn't "Get-Childitem $path -Recurse" doing it's own directory listing? I need it to iterate a supplied file listing. Tried this: `$ListFile="JPGSDIR.lst" $sum=($ListFiles | Measure-Object -Property length -Sum).sum write-Host $Sum ` I clearly don't understand the syntax...that's why I'm here! – Bryan Keadle May 23 '18 at 06:17
  • Right, you needed to import a file (which has list of file paths)-- I missed that. What I have provided is for listing files from a particular path. LotPings' solution works for you. – Ketanbhut May 23 '18 at 07:04
1

Perhaps this is what you want:

ls -recurse | % { [long]($_.Length) } | Measure-Object -sum | % { $_.Sum }
f6a4
  • 1,684
  • 1
  • 10
  • 13
  • OK...I kinda get what you're doing, but trying to interpret. Though I'm getting a number, it's not the correct number. `$totalsize = $Files | % { [int]($_.Length) } | Measure-Object -sum | % { $_.Sum } write-Host $TotalSize1` – Bryan Keadle May 23 '18 at 05:48
  • I have subdirectories added to the script (recurse). – f6a4 May 23 '18 at 05:56
1

You will have to iterate the files contained in JPGS.LST:

$TotalSize = 0
ForEach ($File in Get-Content '.\JPGS.LST'){
    If (Test-Path $File.Trim('"')){$TotalSize+=(GI $File.Trim('"')).Length}else{"$File not found"}
}
"`$TotalSize={0:N2} MB" -f ($TotalSize/1MB)
  • This is close, but it's saying all the files "not found", even though they exist. – Bryan Keadle May 23 '18 at 06:09
  • The double quotes in your file have to be removed. Changed above - try again –  May 23 '18 at 06:22
  • I had just determined that the " was the problem, and came here and see you identified that and provided the solution. How do you do that so easily?!?! Brilliant! Thank you Obi Wan! :-) – Bryan Keadle May 23 '18 at 06:33
  • I sure wish these comment were as easy to edit as the post. Can't seem to format the working code block readably: `$ListFile=$args[0] $TotalSize = 0 ForEach ($File in Get-Content $ListFile){ If (Test-Path $File.Trim('"')){$TotalSize+=(GI $File.Trim('"')).Length}else{"$File not found"} } "`$TotalSize={0:N2} MB" -f ($TotalSize/1MB)` – Bryan Keadle May 23 '18 at 06:34