Skip to primary content
Skip to secondary content

SharePointed

SharePoint and Power Platform and Stuff

SharePointed

Main menu

  • About | Contact
  • Privacy Policy

Tag Archives: copy files

Copy files to another document library maintain metadata

Posted on June 17, 2011 by Ian Hayse
13

(this applies to MOSS / WSS 2007) Scroll to the bottom for a script that will work with SharePoint 2010.

How do you move all the fires from library A to library B?
Easy answer is to open both libraries in Explorer View and copy them over.
Doing this is quick and easy, but your metadata will be lost (if you care).

In my case I had to maintain the metadata when moving a bunch of files to a library in another site.

[System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)

$siteUrl = "http://sharepoint"
$webName = ""

$spSite = new-object Microsoft.SharePoint.SPSite($siteurl)

$spWeb = $spSite.OpenWeb($webName)

$spList = $spWeb.Lists["Documents"]

$listItems = $spList.Items
$listTotal = $listItems.Count

for ($x=$listTotal-1;$x -ge 0; $x--)
{
try
{  $listItems[$x].CopyTo("http://sharepoint/Docs/Documents/"+ $listItems[$x].name)
  Write-Host("DELETED: " + $listItems[$x].name)
  $listItems[$x].Recycle()
  }
Catch
{
Write-Host $_.Exception.ToString()
}
}

$spSite.Dispose
$spWeb.Dispose

$listItems[$x].Recycle() can be replaced with $listItems[$x].Delete()
I wanted to move the items to the Recycle Bin for safe keeping.

If the file already exists in the destination library, the file will not be moved or deleted.
*if you wanted to deal with this scenario, you could simply create a new file name in the Catch event, and copy the file over.*

Foreach does not work if are wanting to loop through the library and delete items.

UPDATE

In this example, I’m using a CAML query to find all documents created before 01/01/2014. This example will also process the items in batches, cutting down on server load. When working with CAML queries, be mindful of the ‘ and ” characters.

$web = Get-SPWeb "http://sharepointed.com/"
$list = $web.Lists["Shared Documents"]

$spQuery = New-Object Microsoft.SharePoint.SPQuery
$spQuery.ViewAttributes = "Scope='Recursive'";
$spQuery.RowLimit = 2000
$caml = '<Where><Lt><FieldRef Name="Created" /><Value IncludeTimeValue="TRUE" Type="DateTime">2014-01-01T04:06:45Z</Value></Lt></Where> '
$spQuery.Query = $caml 

do
{
    $listItems = $list.GetItems($spQuery)
    $spQuery.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
	$listTotal = $listItems.Count

	for ($x=$listTotal-1;$x -ge 0; $x--)
	{
		try
			{
				$listItems[$x].CopyTo("http://sharepoint/Docs/Documents/"+ $listItems[$x].name)
				Write-Host("DELETED: " + $listItems[$x].name)
				$listItems[$x].Recycle()
			}
		catch
			{
			Write-Host $_.Exception.ToString()
			}
	}
}
while ($spQuery.ListItemCollectionPosition -ne $null)
Posted in Powershell, SharePoint | Tagged copy files, metadata, powershell, sharepoint | 13 Replies
Privacy Policy Proudly powered by WordPress