Search code examples
filepowershellinputdirectoryfilesize

powershell filesize total from a directory listing


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!


Solution

  • 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)