4

UPDATE 1:

I have the following script, but it's not working correctly:

Add-PSSnapin Microsoft.SharePoint.PowerShell

#input = https://sharepoint.oshirowanen.com/sites/oshdev
$websiteurl = read-host "Enter site URL"
$web = Get-SPWeb $websiteurl

#input = library one
$LibraryName = read-host "Enter Library Name"
$docLibrary = $web.Lists[$LibraryName]

#input = https://sharepoint.oshirowanen.com/sites/oshdev/library one/user one
$SourceFolderURL = read-host "Enter the source folder URL"

#input = https://sharepoint.oshirowanen.com/sites/oshdev/library two/user one
$TargetFolderURL = read-host "Enter the target folder URL"

[string]$CurrDir=$args[0]
$logfile = $CurrDir + "\log.log"
$errorActionPreference = 'SilentlyContinue'

Function ProcessFolder([string]$sourcefolder,[string]$targetfolder)
{
    write-host "--------------------------------------------------"
    write-host "START OF FUNCTION:"
    write-host "SOURCE:" $sourcefolder
    write-host "DESTINATION:" $targetfolder

    $SFolder = $web.GetFolder($sourcefolder)
    $TFolder = $web.GetFolder($targetfolder)

    $Sfoldername =  $SFolder.name
    $tfoldername =  $TFolder.name

    foreach ($file in $SFolder.Files) 
    {
        try
        {
            write-host "--------------------------------------------------"
            write-host "LOOP 1:"
            write-host "DESTINATION: $targetfolder/$file.Name"
            write-host "CopyTo: $targetfolder/$file.Name"

            $file.CopyTo("$targetfolder/"+$file.Name,$true)
            [Microsoft.SharePoint.SPFile]$spFile = $web.GetFile("$targetfolder" + "/" + $file.Name) 
        }
        # Display and log the filename which is failed to copy to destination folder
        catch
        {
            $fname = $file.Name
            Write-Error 'An error occured while copying the file '$fname 'from Folder' $sourcefolder 
        }

        $sourceitem = $file.item
        $targetitem = $spFile.item          

        if($spFile.Exists -eq $true)            
        {            
            try
            {         
                $targetitem["Author"] = $sourceitem["Author"]
                $targetitem["Editor"] = $sourceitem["Editor"]
                $targetitem["Created"] = $sourceitem["Created"]
                $targetitem["Modified"] =$sourceitem["Modified"]
                $targetitem.Update()  
            }
            # Display and log the filename which is failed to update metadata destination folder
            catch
            {
                $fname = $file.Name
                Write-Error 'An error occured while updating the metada fields for the file' $fname 'from Folder' $sourcefolder 'as the user does not exists '
            }       
        }

    }
    foreach ($subfolder in $SFolder.SubFolders)
    {
        $subfoldername = $subfolder.name

        write-host "--------------------------------------------------"
        write-host "LOOP 2:"
        write-host "TFolder:" $TFolder.URL
        write-host "subfoldername:" $subfoldername

        $folderobj = $docLibrary.AddItem($TFolder.URL, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "$subfoldername")
        $folderobj.Update();
        $targetfolderurl = $folderobj.URL
        #write-host $folderobj.URL

        $strTempDestination = $websiteurl + "/" + $subfolder
        $strNewDestination = $strTempDestination.replace("/library one/","/library two/")

        write-host "SOURCE: $websiteurl/$subfolder"
        write-host "DESTINATION: $strNewDestination"

        ProcessFolder "$websiteurl/$subfolder"  "$strNewDestination"

    }

}

write-host "--------------------------------------------------"
write-host "START:"
write-host "SOURCE:" $SourceFolderURL
write-host "DESTINATION:" $TargetFolderURL
write-host "--------------------------------------------------"

ProcessFolder "$SourceFolderURL" "$TargetFolderURL" 
Remove-PsSnapin Microsoft.SharePoint.PowerShell

It does not give any errors, but it does not create the user one folder in library two and it only copies the files in the root of library one\user one to library two, and it doesn't copy the folders, only the files in the root.

I've tried finding the error by using write-host in the hope that i might see what goes wrong, but the source and destination paths look fine to me.

Any ideas?


ORIGINAL QUESTION:

I know how to move an entire document library to a different document library using powershell export-spweb and import-spweb. But how do I move a specific folder within a document library to a specific folder within a different document library?

At the same time retaining the version history and meta data of any files moved.

Using SharePoint 2010.

4
  • Do you need a PowerShell solution or is a solution to move the directory "by hand" fitting your needs as well?
    – PhilFancy
    Commented Jul 16, 2018 at 7:18
  • @PhilFancy only a PowerShell solution is needed. Commented Jul 16, 2018 at 8:19
  • Is this a one time job in your DEV environment or do you need to execute this in your live environment? Also, do you have a developer tenant in office 365?
    – Deepu Nair
    Commented Jul 18, 2018 at 8:46
  • @DeepuNair I'm using Office 2013, not Office 365. Commented Aug 17, 2018 at 8:49

3 Answers 3

1

You could try with the MoveTo operation in PowerShell (very basic, not optimized, only to get an idea ;-) ).

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$url = "http://myspurl"
$web = Get-SPWeb $url
$list = $web.Lists["myList"]

foreach($folder in $list.Folders)
{
  if($folder.Name -eq "MYFOLDER")
  {
    $query = New-Object -Type 'Microsoft.SharePoint.SPQuery'
    $query.Folder = $folder.Folder
    $folderItems = $list.GetItems($query)

    foreach($item in $folderItems)
    {
      $file = $web.GetFile($item.Url)
      $targetPath = "LISTS/TARGETPATH/" + $file.Name
      $file.MoveTo($targetPath)
    }
  }
}
0

I don't think you have a lot of options. Maybe you could use the SharePoint content and structure, but afaik that doesn't support folders, just documents. And you need to have the publishing feature enabled. It should support metadata.

Migration tooling is very good at this, but comes with a serious price tag.

0

To Move folders from a document Library to another and keep the version history:

  • both libraries should have versioning enabled.
  • open BOTH libraries in Explorer view (Library tab -> Open with Explorer).
  • drag the folder from the library and drop it in the other library explorer window using right click MOVE option to retain the version history, if you use Copy you'll lose the history.

Hope this answering your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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