Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

File and folder copy with comparison

$
0
0

Hi folks,

Let me explain the situation. FolderA and FolderB had identical data (the same files and folders/subfolders structure) in them as they were in sync before. The sync between them has been broken and there is no way of knowing what got changed in FolderA and FolderB.

Now I want to get rid of FolderA. Before that I need a script that will copy data from FolderA to FolderB.
New files and folders need to be copied with no exception. However, it should compare file name and last modified date/time for existing files. If both are equal on both sides then skip copying. If file names are the same, but different date modified then copy and rename by adding date modified to the file name. It should not overwrite existing files/folders. Thank you for your help.

 

This is my script. I'm getting errors when I run it from the server. Locally on my machine it works fine.

 

$Exclude = @("*.pst", "*.tmp*", "~*.*", "Thumbs.db" )

$src = "\\Server1\e$\FolderA\"

$trg = "\\Server2\e$\FolderB\"

 

function Copy-Files ([string]$source,[string]$destination){

    $srcc = Get-childitem $source

    $trgc = Get-childitem $destination

 

   if($srcc.count -ne 0 -and $trgc.count -ne 0 ){

     Compare-Object $srcc $trgc -Property Name, Extension, LastWriteTime,BaseName,PSIsContainer | Where-Object {$_.SideIndicator -eq "<=" } | ForEach-Object {

 

        if($_.PSIsContainer){

            if(-Not (Test-Path $($destination + $_.name))){

                Write-Host $($source + $_.name) +"  --  "+$($destination + $_.name)  + " 1"

                Copy-Item $($source + $f.name) $($destination + $_.name) -force -ea SilentlyContinue -Exclude $Exclude

            }

 

        }

        else {

        if(Test-Path $($destination + $_.name)){

            Write-Host $($source + $_.name) +"  --  "+$($destination + $_.name)  + " 2"

            Copy-Item $($source + $_.name) -Destination $($destination + $($_.BaseName + "_" + ($_ | Select-Object -ExpandProperty LastWriteTime).ToString("MM-dd-yyyy") + $_.Extension)) -force -ea SilentlyContinue -Exclude $Exclude

        }

        else {

            Write-Host $($source + $_.name) +"  --  "+$($destination + $_.name)  + " 3"

            Copy-Item $($source + $_.name) -Destination $($destination + $_.name) -force -ea SilentlyContinue -Exclude $Exclude

        }

        }

    }

    }

 

    Copy-Folders $source $destination

}

 

function Copy-Folders ([string]$source,[string]$destination){

    $srcc = Get-childitem $source

    $trgc = Get-childitem $destination

 

    if($trgc.count -ne 0 -and $srcc.count -ne 0)

    {

    Compare-Object $srcc $trgc -Property FullName,Name,PSIsContainer | Where-Object {$_.SideIndicator -eq "<=" -and $_.PSIsContainer -eq $true} |ForEach-Object {

          if(Test-Path $($destination + $_.name)){

 

             Copy-Files $($_.Fullname+"\") $($destination + $_.Name+"\")

           }

        }

    }

    else 

    {

        foreach($f in $srcc){

            if($f.PSIsContainer){

               if(-Not (Test-Path $($destination + $f.name))){

                    Write-Host $($source + $f.name) +"  --  "+$($destination)   + " 4"

                    Copy-Item $($source + $f.name) $($destination) -force -ea SilentlyContinue -Exclude $Exclude

               } 

            } 

            else {

                Write-Host $($source + $f.name) +"  --  "+$($destination + $f.name)   + " 5"

                Copy-Item $($source + $f.name) $($destination + $f.name) -recurse -force -ea SilentlyContinue -Exclude $Exclude

            }

 

        }

 

 

 

    }

}

 

Copy-Files $src $trg

 

 

Errors:

 

Compare-Object : Cannot bind argument to parameter 'ReferenceObject' because it is null.

At line:10 char:20

+      Compare-Object <<<<  $srcc $trgc -Property Name, Extension, LastWriteTime,BaseName,PSIsContainer | Where-Object {$_.SideIndicator -eq "<=" } | ForEach-Object {

    + CategoryInfo          : InvalidData: (:) [Compare-Object], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CompareObjectCommand

 

Compare-Object : Cannot bind argument to parameter 'ReferenceObject' because it is null.

At line:42 char:19

+     Compare-Object <<<<  $srcc $trgc -Property FullName,Name,PSIsContainer | Where-Object {$_.SideIndicator -eq "<=" -and $_.PSIsContainer -eq $true} |ForEach-Object {

    + CategoryInfo          : InvalidData: (:) [Compare-Object], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CompareObjectCommand

 

Compare-Object : Cannot bind argument to parameter 'DifferenceObject' because it is null.

At line:10 char:20

+      Compare-Object <<<<  $srcc $trgc -Property Name, Extension, LastWriteTime,BaseName,PSIsContainer | Where-Object {$_.SideIndicator -eq "<=" } | ForEach-Object {

    + CategoryInfo          : InvalidData: (:) [Compare-Object], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CompareObjectCommand

 

Compare-Object : Cannot bind argument to parameter 'DifferenceObject' because it is null.

At line:42 char:19

+     Compare-Object <<<<  $srcc $trgc -Property FullName,Name,PSIsContainer | Where-Object {$_.SideIndicator -eq "<=" -and $_.PSIsContainer -eq $true} |ForEach-Object {

    + CategoryInfo          : InvalidData: (:) [Compare-Object], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CompareObjectCommand


Viewing all articles
Browse latest Browse all 6937

Trending Articles