tags:

views:

639

answers:

2

I'm having trouble getting this powershell statement to work. The objective is I'm trying to get a list of folders which are in ..\archive folder sorted by oldest to youngest. I would like to copy the number of folders which amount to or less than $ClosedJobssize from the ..\Archive to the ..\movetotape folder. This is so the size of the ..\Archive folder never changes on the hard drive.

get-childitem -path "\\srv02\d$\Prepress\Archive" | sort-object -property 

@{Expression={$_.CreationTime};Ascending=$false} | % { if (((get-childitem -path 

"\\srv02\d$\prepress\archive" -recurse -force | measure-object -Property Length -Sum).Sum + $_.Length) 

-lt $closedjobssize ) { move-item -destination "\\srv02\d$\prepress\archive\MoveToTape\" }}

Any ideas what I might be doing wrong? I don't get any errors, it just sits and hangs when I execute it.

thanks in advance

A: 

Try this it's a long one-liner (remove -whatIf to perform the move):

dir "\\srv02\d$\Prepress\Archive" | sort CreationTime -desc | where { $_.psiscontainer -AND (dir $_.fullname -recurse -force | measure-object -Property Length -Sum).Sum -lt $closedjobssize} | Move-Item -dest "\\srv02\d$\prepress\archive\MoveToTape\" -whatIf
Shay Levy
the one liner didn't work for me. It just copied everything into the movetotape folder
phill
A: 

I'm not quite sure I understand. But I think you want to move folders in \archive to \archive\movetotape to fill up \movetotape until it is $ClosedJobsSize or less in size. Right?

Couple things. You are adding up the size of everything in \archive, so the result of your comparison will never change. Second, one of the folders checked is MoveToTape itself, which could cause you to move it into itself (this should give an exception).

Given that, I think this code will work but I haven't tested it.

## Get all the directories in \arcive that need to be moved
$Directories = Get-ChildItem "\\srv02\d$\Prepress\Archive" | 
    Where-Object {$_.PSIsContainer -and ($_.Name -ne "MoveToTape")} | Sort-Object CreationTime -Descending
foreach ($Directory in $Directories)
{
    $SumOfMoveToTape = (Get-ChildItem "\\srv02\d$\prepress\archive\MoveToTape\" -Recurse | Measure-Object -Property Length -Sum).Sum
    $SumOfItem = (Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum
    if(($SumOfMoveToTape + $SumOfItem) -lt $ClosedJobsSize)
    {
     ## If we can fit on MoveToTape, then move this directory
     Move-Item -Destination "\\srv02\d$\prepress\archive\MoveToTape\"
    }
    ## If you want to keep folders in order (and not try to squeze whatever onto the tape
    ## then put an 'else {break}' here
}
JasonMArcher
The objective is to shift files from one folder to another of the same size. So if 1 gig of data came from the closedjobs folder and placed into the archive, then 1 gb of the oldest files should come out of the archive to go into the movetotape folder. thanks!
phill
Then I think my code will do that. It will fill up \movetotape with folders from \archive until \movetotape full with $ClosedJobsSize amount of data (or it runs out of stuff to move). Give it a test and let me know if it is what you need.
JasonMArcher
I'm not seeing where does it calculate $closedJobsSize? is there a missing line?
phill
You did not specify how it is calculated, so I assume that it is statically defined, or you figure it out somehow.
JasonMArcher