tags:

views:

749

answers:

1

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. Any ideas?

thanks in advance

+1  A: 
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
Better than mine since it checks the total before the copy.
EBGreen
I think you are missing an _ in $.length. I think it should be $_.Length.
EBGreen
It is $_.Length in Edit textbox, so I had to change to code format.
Gordon Bell
Can you split up the lines after each pipe symbol, it will make your code easier to read. Not a big deal
Peter Seale
I'm getting an error saying Measure-object : property "length" cannot be found in any objects input
phill
nevermind, i got it.. how do I have it only copy folders?
phill
Somewhere you need | Where-Object ($_.Attributes -match "Directory")
Bratch
You also need to specify -recurse with Get-ChildItem if you want the sum to include files in subdirectories, and -force if there are hidden files/folders.
Bratch