-1

I am trying to create a code on Powershell that will actually Copy files from one Location( Lets say A) to location B. Now Location B have two subfolders (lets say X and Y). I need to copy the file from A to B but before copying I need to make sure that the files which I am copying should not be there in X or Y in order to avoid file duplication. If the file exist, it should not copy that particular file.

$PathS = Get-ChildItem -Path "\\sc-y-ap-swt-1\AutoClientFiles\reception\*.txt" |
    Where-Object { $_.CreationTime -gt (Get-Date).AddDays(-1) }

$PathD = "C:\OCM\data\EverestSwift\inbound\"
$pathtest = Get-ChildItem -path "C:\OCM\data\EverestSwift\inbound\" -Recurse -File 

If((Test-Path -Path "\\sc-y-ap-swt-1\AutoClientFiles\reception\*.txt") -eq $false) {
    Exit
} Else {
    Try {
        Foreach ($File in $Pathtest){
            if ($File -eq $PathS ){
                    Write-Host "Duplicate Files"
                    exit 1
            }
            Copy-Item -Path $PathS -Destination $PathD -Force
            Exit 0
        }

    } catch [Exception]{
        Write-Host $_.Exception.Message
        Exit 1
    }
}
  • I am no expert in powershell so with my limited coding skills I tried few loops to put that together, but unfortunately it's not doing the actual job. Could you please help me get the right code. Please let me know if you have any questions. – Mayank Wadhwa Apr 22 '19 at 21:15
  • have you looked into the features `robocopy` has? – Kory Gill Apr 22 '19 at 21:19
  • Why doesn't your code reflect your text in any point? Location A,B,X,Y vs. PathS,PathD,pathtest? Please take the [tour], read [ask] and what a [mcve] is. –  Apr 22 '19 at 22:30
  • Possible duplicate of [Copy items from Source to Destination if they don't already exist](https://stackoverflow.com/questions/25916197/copy-items-from-source-to-destination-if-they-dont-already-exist) – recnac Apr 28 '19 at 01:19

1 Answers1

0

You can do this, but why. As Cory said, this is why robocopy exists.

What do you mean by same?

The filename can be the same, but the timestamps can be different, thus making it a different file, even if the name is the same. So, you should be looking at name and timestamp or file hashes.

So, see these Q&A about such a use case.

Does Robocopy SKIP copying existing files by default?

How to skip existing and/or same size files when using robocopy

RoboCopy "%%F" %destination% *.srt *.pdf *.mp4 *.jpg /COPYALL /XO /R:0

Yet, doing this with powerShell, your post could be a duplicate of this one.

Copy items from Source to Destination if they don't already exist

Examples from the above:

$Source = 'C:\SourceFolder'
$Destination = 'C:\DestinationFolder'

Get-ChildItem $Source -Recurse | ForEach {
    $ModifiedDestination = $($_.FullName).Replace("$Source","$Destination")
    If ((Test-Path $ModifiedDestination) -eq $False) {
        Copy-Item $_.FullName $ModifiedDestination
        }
    }


# Or


$Source = '<your path here>'
$Dest = '<your path here>'
$Exclude = Get-ChildItem -recurse $Dest

Get-ChildItem $Source -Recurse -Filter '*' | 
Copy-Item -Destination $Dest -Verbose -Exclude $Exclude
postanote
  • 15,138
  • 2
  • 14
  • 25