2

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
phill
  • 13,434
  • 38
  • 105
  • 141

2 Answers2

2
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}}
Gordon Bell
  • 13,337
  • 3
  • 45
  • 64
0

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;
rferrisx
  • 1,598
  • 2
  • 12
  • 14