question

Sureshdba-4403 avatar image
0 Votes"
Sureshdba-4403 asked Sureshdba-4403 answered

exclude copying existing files from sharepoint list to document library

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

  2. $WebURL = "http://intranet/sites/test/"

  3. $SourceListName = "agreement_list"

  4. $TargetLibraryName = "agreement"

  5. Get the Web List and Library objects

  6. $web = Get-SPWeb $WebURL

  7. $SourceList = $web.Lists[$SourceListName]

  8. $TargetLibrary = $web.Lists[$TargetLibraryName]

  9. Loop through each list item

  10. foreach ($ListItem in $SourceList.Items)

  11. {

  12. if($ListItem.Attachments.Count -gt 0)

  13. {

  14. Loop through each attachment in the list item

  15. foreach ($Attachment in $ListItem.Attachments)

  16. {

  17. Get the attachment

  18. $file = $web.GetFile($ListItem.Attachments.UrlPrefix+$Attachment)

  19. $bytes = $file.OpenBinary()

  20. $TargetFileName = $TargetLibrary.RootFolder.Url+"/"+$Attachment

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

  22. Write-Host "Copied to: $($TargetFilename)"

  23. }

  24. }

  25. }

sharepoint-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JoyZ avatar image
0 Votes"
JoyZ answered

@Sureshdba-4403,

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.




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Sureshdba-4403 avatar image
0 Votes"
Sureshdba-4403 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.