locked
Powershell script to copy documents not working RRS feed

  • Question

  • I am trying to copy documents from a document library to another document library in a subsite.

    I have this script

        write-host "Starting to copy" -ForegroundColor Cyan
     
        #Add the SharePoint snapin
        Add-PSSnapin Microsoft.SharePoint.Powershell -ea SilentlyContinue
                 
        write-host "Loading powershell module" -ForegroundColor Cyan    
        $urlWebSrc = "http://mysite"
        $urlWebTgt = "http://mysite/Search"
        $listNameSrc = "Documents"
        $listNameTgt = "Archieve"

        $webSrc = Get-SPWeb $urlWebSrc
        $webTgt = Get-SPWeb $urlWebTgt
        
        # Get your source and destination libraries
        $source = $webSrc.Lists[$listNameSrc]
        $destination = $webTgt.Lists[$listNameTgt]
        
        # Get the collection of items to move, use source.GetItems(SPQuery) if you want a subset
        $items = $source.Items;

        # Get the root folder of the destination we'll use this to add the files
        $folder = $webSrc.GetFolder($destination.RootFolder.Url);

        # Now to move the files and the metadata
        foreach ($item in $items)
        {   
            #Get the file associated with the item     
            $file = $item.File;
            
            $newFileDest = ("{0}/{1}/{2}" -f $urlWebTgt, $folder.Url, $file.Name);
            write-host "moving to" - $newFileDest;
            
            $item.File.MoveTo($newFileDest, $true)                           
        }


    But this is not working. I am getting errors:


    > Exception calling "MoveTo" with "2" argument(s): "Folder
    > "2006Archieve" does not exist." At C:\script\archive2.ps1:41 char:26
    > +         $item.File.MoveTo <<<< ($newFileDest, $true)
    >     + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    >     + FullyQualifiedErrorId : DotNetMethodException

     
    Wednesday, June 3, 2015 2:36 PM

Answers