phill
phill

Reputation: 13844

PowerShell : how do you run a folder size summation?

My objective is to write a copy script which moves files from one directory, "d:\aa1", to "d:\aa2" up to a specified size of another directory, "d:\bbb". In other words... I'd like it to copy all files from "d:\aa1" to "d:\aa2" until the size of "d:\aa1" is the same size or less than "d:\bbb".

So far I have

$lmt = get-childitem d:\bbb  | measure-object -property length -sum 
do { get-childitem -path d:\aa1 | move-item -destination "d:\aa2" } while {measure-object {$_.sum -lt $lmt} 

But the syntax doesn't seem to be working. How can I do it?

Upvotes: 2

Views: 2552

Answers (2)

rferrisx
rferrisx

Reputation: 1728

There should be a property or method for the sum of a directory.You can add options to 'ls' as needed for the script below:

rv total;
foreach ($i in ((ls -Force) | Select Length)) {[int64]$total+=$i[0].Length};
$total /1GB;

Upvotes: 0

Gordon Bell
Gordon Bell

Reputation: 13633

Get-ChildItem -path d:\aa1 `
| % {if (((Get-ChildItem d:\aa2 `
| Measure-Object -Property Length -Sum).Sum + $_.Length) `
-lt (Get-ChildItem d:\bbb | Measure-Object -Property Length -Sum).Sum) `
{Move-Item $_.FullName d:\aa2}}

Upvotes: 2

Related Questions