0

In SP2010, I'm trying to move all the files from one library to another using Powershell.

I'm using the below script to first copy the files from source to destination and then delete the source file.

But the script displays error after the first file is moved to destination and then deleted from source.I understand the script is not able to get back to the foreach loop after processing the first file , i tried different ways but nothing worked.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Custom Function to Copy Files from Source Folder to Target
Function Copy-Files($SourceFolder, $TargetFolder)
 {
write-host "Copying Files from:$($SourceFolder.URL) to $($TargetFolder.URL)"
#Get Each File from the Source
$SourceFilesColl = $SourceFolder.Files

#Iterate through each item from the source
Foreach($SourceFile in $SourceFilesColl)
{
    #Copy File from the Source
    $NewFile = $TargetFolder.Files.Add($SourceFile.Name, $SourceFile.OpenBinary(),$True)

    #Copy Meta-Data from Source
    Foreach($Field in $SourceFile.Item.Fields)
    {
        If(!$Field.ReadOnlyField)
        {
            if($NewFile.Item.Fields.ContainsField($Field.InternalName))
            {
                $NewFile.Item[$Field.InternalName] = $SourceFile.Item[$Field.InternalName]
            }
        }
    }
    #Update
    $NewFile.Item.UpdateOverwriteVersion()
    Write-host "Copied File:"$SourceFile.Name

   $SourceFile.Delete()
   Write-host "Deleted File from source:"$SourceFile.Name 
   }
   }
   #Variables for Processing
   $WebURL="http://SiteURL"
   $SourceLibrary ="Team Docs"  
   $TargetLibrary = "Sales Documents"

   #Get Objects
   $Web = Get-SPWeb $WebURL
   $SourceFolder = $Web.GetFolder($SourceLibrary)
   $TargetFolder = $Web.GetFolder($TargetLibrary)

   #Call the Function to Copy All Files
   Copy-Files $SourceFolder $TargetFolder

SP2010 Powershell Error:

enter image description here

1

Change the step Iterate through each item from the source to this:

#Iterate through each item from the source
for($i = 0; $i -lt $SourceFilesColl.Count){
$SourceFile=$SourceFilesColl[$i]
    #Copy File from the Source
    $NewFile = $TargetFolder.Files.Add($SourceFile.Name, $SourceFile.OpenBinary(),$True)
      Foreach($Field in $SourceFile.Item.Fields)
    {
        If(!$Field.ReadOnlyField)
        {
            if($NewFile.Item.Fields.ContainsField($Field.InternalName))
            {
                $NewFile.Item[$Field.InternalName] = $SourceFile.Item[$Field.InternalName]
            }
        }
    }

     #Update
    $NewFile.Item.UpdateOverwriteVersion()
    Write-host "Copied File:"$SourceFile.Name

   $SourceFile.Delete()
   Write-host "Deleted File from source:"$SourceFile.Name 
   }
| improve this answer | |
  • many thanks for the updated code. It worked well. I am looking to move and delete the files which are greater than 3 years from the date of creation. I tried that but facing some looping issue as the loop is running infinitely. Can you kindly modify your script accordingly? – George Apr 17 at 9:10
0

You can use the below code:

$web = "http://yoursiteurl"

$orgLibrary = (Get-SPWeb $web).Folders["Approval History - Centre EV"]
$destLibrary = (Get-SPWeb $web).Folders["Approval History - Centre EV1"]

$destFiles = $destLibrary.Files
foreach ($file in $orgLibrary.Files)
{
    $curFile = $file.OpenBinary()
    $destURL = $destFiles.Folder.Url + "/" + $file.Name
    $destFiles.Add($destURL, $curFile, $true)

#delete the source file 
$file.Delete() 

} 

Source:

Move files between document libraries in the same Site Collection (#2)

| improve this answer | |
  • This script doesn't delete the source documents. – George Apr 9 at 10:12
  • Add this at end of the foreach loop $file. Delete () updated the code – Habibur Rahaman Apr 9 at 10:22
  • I already done that but i still get the same error as in my question – George Apr 9 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.