如何比早于x天的Powershell中的A到B进行比较和移动?

时间:2015-05-15 08:59:50

标签: powershell copy compare move

我的Powershell脚本有问题。 ( 我想用Powershell 删除/移动/比较

这是我的想法:

如果我启动脚本,则从A中删除所有空文件夹。之后,我希望将所有数据从A移动到B,即x天更旧。 (这是我的第一个问题)

如果我制作副本就行了。

另一个想法是比较A和B,只比较差异来复制或移动。 (但我不知道如何做到这一点)

这是我的代码:

function removeFiles($source,$destination){
    $elements = Get-ChildItem $source -Recurse
    foreach($element in $elements){
        if(($element.PSIsContainer) -and (!(Get-ChildItem -Recurse -Path $element.FullName))){
            Write-Host "DELETE: " $element.FullName
            Remove-Item $element.FullName -Confirm:$false
        }
    }
}

function copyFiles($source,$destination,$days){
    $elements = Get-ChildItem $source -Recurse
    $lastwrite = (Get-Date).AddDays(-$days)
    foreach($element in $elements){
        if($element.creationTime -le $lastwrite){
            $targetFile = $destination + $element.FullName.SubString($source.Length)
            if($element.PSIsContainer){
                Write-Host "COPY FOLDER : " $element.FullName
                Copy-Item $element.FullName -Destination $targetFile
            }else{
                Write-Host "COPY FILE : " $element.FullName
                Copy-Item $element.FullName -Destination $targetFile
            }
        }
    }
}

function moveFiles($source,$destination,$days){
   Get-ChildItem -Path $source -Recurse | Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-2)} | Move-Item -Destination $destination
}

CLS

$Source = "C:\Users\tarasov_w\Downloads"
$Destination = "C:\Users\tarasov_w\Desktop\ps_test"
$LogFile = "C:\Users\tarasov_w\Desktop\$(get-date -format 'MMddyyyy').txt" 
$Days = 1

Start-Transcript $LogFile -noclobber

Write-Host "Start..."

removeFiles -source $Source -destination $Destination

#copyFiles -source $Source -destination $Destination -days $Days

moveFiles -source $Source -destination $Destination -days $Days

Write-Host "Fertig!"

更新:

function compareFiles($source,$destination){
    $folderA = Get-ChildItem $source -Recurse
    $folderB = Get-ChildItem $destination -Recurse

    $diff = Compare-Object -ReferenceObject $folderA -DifferenceObject $folderB -PassThru

    foreach($element in $diff){
        $element_fullname = $element.FullName 

        if($element.PSIsContainer){
           # ????
        }

    }
}

0 个答案:

没有答案