Get files within size range and assign ID

I have an advanced function that accepts an array of ordered objects containing the FullName and Length of files. This is the easy part.

Each object passed in foreach() will be added to a new object containing FullName, Length, and ID properties.

Here’s where I need insight:

The ID (starts at 0) should be the same for each object until the combined lengths of the objects are in the range of 1.5 GB and 2 GB, then ID is incremented by 1.

Any subsequent objects are processed with their ID as 1. This repeats until all objects have been processed.

Example:

FullName                     ID   Length
----                         --   ------
\\server\share\file001.ext   0    0.5
\\server\share\file002.ext   0    0.2
\\server\share\file003.ext   0    0.7
\\server\share\file004.ext   0    0.1
\\server\share\file005.ext   0    0.1
\\server\share\file006.ext   0    0.2 # Total for ID 0: 1.8 GB
\\server\share\file007.ext   1    0.8 # File too large for ID 0 set, specify ID as 1.
\\server\share\file008.ext   1    0.6 # Total for ID 1: 1.4 GB
\\server\share\file009.ext   2    1.6
\\server\share\file010.ext   2    0.1 # Total for ID 2: 1.7 GB

Feedback on how to accomplish this would be greatly appreciated.

You could do something like this:

$Source = @’
FullName;Length
\server\share\file001.ext;0.5
\server\share\file002.ext;0.2
\server\share\file003.ext;0.7
\server\share\file004.ext;0.1
\server\share\file005.ext;0.1
\server\share\file006.ext;0.2
\server\share\file007.ext;0.8
\server\share\file008.ext;0.6
\server\share\file009.ext;1.6
\server\share\file010.ext;0.1
'@

$Data = $Source | ConvertFrom-Csv -Delimiter ‘;’

foreach($line in $Data){
$IDSize = [MATH]::Round($IDSize + [single]$line.length,2)
If($IDSize -gt 2){
$IDSize = [single]$line.length
$ID ++
}
[PSCustomObject]@{
Path = $line.FullName
Length = $line.Length
ID = [INT]$ID
IDSize = $IDSize
}
}

… and of course you dont have to output the “IDSize”