PowerShell中的if Get ChildItem语句有什么问题?

PowerShell中的if Get ChildItem语句有什么问题?,powershell,Powershell,我无法使以下PowerShell语句正常工作。目标是获取。\archive文件夹中的文件夹列表,按从最早到最年轻的顺序排序 我想将数量等于或小于$closedjobsize的文件夹从。\Archive复制到。\movetotape文件夹。这样,硬盘上..\Archive文件夹的大小就不会改变 get-childitem -path "\\srv02\d$\Prepress\Archive" | sort-object -property @{Expression={$_.CreationTim

我无法使以下PowerShell语句正常工作。目标是获取
。\archive
文件夹中的文件夹列表,按从最早到最年轻的顺序排序

我想将数量等于或小于
$closedjobsize
的文件夹从
。\Archive
复制到
。\movetotape
文件夹。这样,硬盘上..\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\" }}

我可能做错了什么?我没有发现任何错误。当我执行它时,它只是静止和挂起。

试试这个。这是一个长的单行程序(删除
-whatIf
以执行移动):


我不太明白。但我认为您需要将
\archive
中的文件夹移动到
\archive\movetotape
以填充
\movetotape
,直到其大小小于等于
$closedjobsize
为止。对吧?

有两件事:您正在将
\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
}

目标是将文件从一个文件夹转移到另一个相同大小的文件夹。因此,如果1G的数据来自closedjobs文件夹并放入存档,那么1G的最旧文件应该从存档中取出,放入movetotape文件夹。谢谢那么我想我的代码可以做到这一点。它将用\archive中的文件夹填充\movetotape,直到\movetotape充满$closedjobsize数据量(或者它没有东西可移动)。给它一个测试,让我知道它是否是你需要的。我不知道它在哪里计算$closedJobsSize?是否缺少一行?您没有指定它是如何计算的,所以我假设它是静态定义的,或者您以某种方式找到了它。一行对我不起作用。它只是将所有内容复制到movetotape文件夹
(get childitem-path“\\srv02\d$\prepress\archive”-recurse-force | measure object-Property Length-Sum)。Sum
将获得总大小,包括归档目录中所有文件的子文件夹。我不明白通过将当前管道对象的长度添加到该长度中进行比较,您想要实现什么。它也很昂贵,而且你对每一个连接到它的对象都这么做。。。然后,
move item-destination“\\srv02\d$\prepress\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
}