2009-02-26 17 views
0

我無法使以下PowerShell語句正常工作。目標是獲取按照最早到最年輕排列的..\archive文件夾中的文件夾列表。如果PowerShell中的Get-ChildItem語句有什麼問題?

我想從..\Archive..\movetotape文件夾中複製等於或小於$ClosedJobssize的文件夾數量。這是因爲.. \ Archive文件夾的大小決不會在硬盤上發生變化。

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\" }} 

我會做什麼錯?我沒有得到任何錯誤。它只是當我執行它時坐下並掛起。

+0

`(GET-childitem -path 「\\ srv02 \ d $ \印前\檔案」 -recurse -force |測量對象,物業長度-sum).Sum`將讓你的總規模,包括歸檔目錄中所有文件的子文件夾。我不明白你想通過添加當前管道對象的長度來比較。這也是昂貴的,你正在爲它的每一個對象... 然後,`move-item -destination「\\ srv02 \ d $ \ preress \ archive \ MoveToTape \」`沒有指定一個源,因此什麼都不做(如果腳本有這麼多,可能會提示你輸入源)。 – Jimmeh 2014-03-23 22:27:11

回答

1

試試這個。這是一個漫長的一行(刪除-whatIf執行移動):

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 
+0

單線不適合我。它只是將所有內容複製到movetotape文件夾中 – phill 2009-03-12 04:50:13

0

我不太肯定我明白了。但我認爲你想將文件夾移動到\archive\archive\movetotape填充\movetotape,直到它是$ClosedJobsSize或更小的大小。對?

有幾件事:您將\archive中的所有內容加起來,因此您的比較結果永遠不會改變。其次,其中一個檢查的文件夾是MoveToTape本身,這可能會導致您將其移入自身(這應該會導致異常)。

鑑於此,我認爲這段代碼可以工作,但我沒有測試過它。

## 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 
} 
+0

目標是將文件從一個文件夾轉移到另一個相同大小的文件夾。因此,如果1 gig的數據來自closedjobs文件夾並放入存檔中,那麼1 gb的最舊文件應該從存檔中移出到移動文件夾中。謝謝! – phill 2009-02-27 16:46:00