exclude copying existing files from sharepoint list to document library

Suresh 41 Reputation points
2021-11-03T20:48:27.44+00:00

I'm using this script to copy files from SharePoint list to document library, I'm planning to schedule this everyday but the problem is its copying existing files and created duplicates. Is there a way to exclude copying existing files in document library?

  1. Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
    1. $WebURL = "http://intranet/sites/test/"
    1. $SourceListName = "agreement_list"
  2. $TargetLibraryName = "agreement"
    1. Get the Web List and Library objects

  3. $web = Get-SPWeb $WebURL
  4. $SourceList = $web.Lists[$SourceListName]
  5. $TargetLibrary = $web.Lists[$TargetLibraryName]
    1. Loop through each list item

  6. foreach ($ListItem in $SourceList.Items)
  7. {
  8. if($ListItem.Attachments.Count -gt 0)
  9. {
  10. Loop through each attachment in the list item

  11. foreach ($Attachment in $ListItem.Attachments)
  12. {
  13. Get the attachment

  14. $file = $web.GetFile($ListItem.Attachments.UrlPrefix+$Attachment)
  15. $bytes = $file.OpenBinary()
    1. $TargetFileName = $TargetLibrary.RootFolder.Url+"/"+$Attachment
  16. $TargetFile = $TargetLibrary.RootFolder.Files.Add($TargetFileName, $bytes, $true)
  17. Write-Host "Copied to: $($TargetFilename)"
  18. }
  19. }
  20. }
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,657 questions
0 comments No comments
{count} votes

Accepted answer
  1. JoyZ 18,041 Reputation points
    2021-11-04T03:12:53.02+00:00

    @Suresh ,

    Per my test, if we specify the overwrite parameter to $true, the Add method will not create duplicate files in the target library:

    public Microsoft.SharePoint.SPFile Add (string urlOfFile, byte[] file, bool overwrite);  
    

    In your code, the value is set to true, so it will overwrite the existing file instead of creating duplicates:

    $TargetFile = $TargetLibrary.RootFolder.Files.Add($TargetFileName, $bytes, $true)  
    

    My simple test:

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue  
       
    $WebURL = "http://sp13:36043"  
       
    $SourceListName = "List82"    
    $TargetLibraryName = "Lib7/5"  
       
    #Get the Web List and Library objects  
    $web = Get-SPWeb $WebURL  
    $SourceList = $web.Lists[$SourceListName]   
    $TargetLibrary = $web.Lists[$TargetLibraryName]      
       
    #Loop through each list item  
    foreach ($ListItem in $SourceList.Items)  
    {  
       if($ListItem.Attachments.Count -gt 0)  
       {  
            #Loop through each attachment in the list item  
            foreach ($Attachment in $ListItem.Attachments)      
            {     
                  #Get the attachment  
                  $file = $web.GetFile($ListItem.Attachments.UrlPrefix+$Attachment)          
                  $bytes = $file.OpenBinary()                 
           
                  $TargetFileName = $TargetLibrary.RootFolder.Url+"/"+$Attachment  
                  $TargetFile = $TargetLibrary.RootFolder.Files.Add($TargetFileName, $bytes, $false)  
                  Write-Host "Copied to: $($TargetFilename)"  
            }  
       }  
    }  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Suresh 41 Reputation points
    2021-11-04T15:34:50.097+00:00

    changing the value did the trick, thank you @JulieWang-MSFT

    0 comments No comments