0

I am using below script to copy files from one library to another:

$SourceWebURL = "http://sitecollection/sites/site" 
$SourceLibraryTitle = "doclib" 
$DestinationWebURL = "http://sitecollection/sites/site" 
$DestinationLibraryTitle = "doclib" 
$ssite = Get-SPSite $SourceWebURL
$sweb = $ssite.OpenWeb("subsite")
$dsite = Get-SPSite $DestinationWebURL 
$dweb = $dsite.OpenWeb("Archivesubsite")
$sList = $sWeb.Lists | ? {$_.Title -eq $SourceLibraryTitle} 
$dList = $dWeb.Lists | ? {$_.title -like $DestinationLibraryTitle} 
$AllFolders = $sList.Folders 
$RootFolder = $sList.RootFolder 
$RootItems = $RootFolder.files 

foreach($Folder in $AllFolders)
{
echo $Folder.url
    foreach($Item in $Folder)
    {
        $sBytes = $RootItem.OpenBinary() 
            $dFile  = $dList.RootFolder.Files.Add($Item.Name, $sBytes, $true) 
        $dFile.Update() 
    }
}

I want to loop in each folder in doclib and create the files in folders in destination library. Error I am getting is : You cannot called a method on null valued expression.

UPDATED after Nadeem's comment:

$AllFolders = $sList.Folders foreach($Folder in $AllFolders) { foreach($file in $Folder.Folder.Files) { write-Host "Hello" $sBytes = $file.OpenBinary() $dFolder = $dlist.ParentWeb.GetFolder($dWeb.Url + "/" + $dList.RootFolder.Url + "/" + $Folder.Name) $dFile = $dFolder.Files.Add($dFolder.Url + "/" + $file.Name, $sBytes, $true)
} } $dList.Update()

1 Answer 1

1

There are few things to consider here. As is clear, you want to copy the documents from Source library to destination library and also preserve the folder structure. Documents can reside directly inside the source library at the root location. This folder is called Root Folder in SharePoint and it can be retrieved using $RootFolder = $sList.RootFolder. Documents can also reside inside actual folders. These folders can be retrieved using $AllFolders = $sList.Folders. Here is a way to copy the documents from all the folders except root folder of the source library to the root folder in destination library. Note that, ideally you will need to create a folder structure first before adding the file in it. However, I have already answered that question

$web = Get-SPWeb -Identity http://aissp2013/sites/Team          
$sList = $web.Lists["Doc1"]
$dList = $web.Lists["DOC2"]
$AllFolders = $sList.Folders 
foreach($Folder in $AllFolders)
{
    foreach($file in $Folder.Folder.Files)
    {
        write-Host "Hello"
        $sBytes = $file.OpenBinary() 
        $dFile  = $dList.RootFolder.Files.Add($dList.RootFolder.Url + "/" + $file.Name, $sBytes, $true)         
    }
}
$dList.Update() 
7
  • Thanks Nadeem, but I have one doubt, files are copied into root folders of destination subsite. can't we move them into actual folders on destination ? like $dFile = $dList.specificFolder.Files.Add($dList.RootFolder.Url + "/" + $file.Name, $sBytes, $true)
    – Aquarius24
    Jan 29, 2015 at 11:34
  • Of course we can copy files in specific folder. There needs to be a logic to get the folder structure info from source library and then creating that folder structure in destination and finally copying the files in respective folder like $specificFolder.Files.Add($specificFolder.Url + "/" + $file.Name, $sBytes, $true). I was just showing the correct way of adding a file to folder in response to your question. Jan 29, 2015 at 11:40
  • I have updated my answer after trying to get the specific folder in destination subsite. May be a little mistake I am committing so I am getting - You cannot call a method on null valued expression
    – Aquarius24
    Jan 29, 2015 at 11:57
  • Got the error and corrected, now it is working as expected. thanks a ton Nadeem.
    – Aquarius24
    Jan 29, 2015 at 12:06
  • Glad I could help. Jan 29, 2015 at 12:06

Your Answer

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

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