0

Almost same question as here : Power shell script to copy the files and folders with in 2 specific dates

I understand the script, which copies entire folders/files from a library to another. But my need is to copy entire folders structure/files from a library to a specific folder.

And I don't know how to modify this script to suit my need. Because the $dFile = $dList.RootFolder.Files.Add($RootItem.Name, $sBytes, $true) does not work for a folder ... So I need to modify the script in order to work with folders and not for a list.

Here is the script : (sources)

## 
#Set Static Variables 
## 

$SourceWebURL = "http://www.contoso.com" 
$SourceLibraryTitle = "Shared Documents" 
$DestinationWebURL = "http://archive.contoso.com/WWWArchive" 
$DestinationLibraryTitle = "Shared Documents" 

## 
#Begin Script 
## 

$sWeb = Get-SPWeb $SourceWebURL 
$sList = $sWeb.Lists | ? {$_.Title -eq $SourceLibraryTitle} 
$dWeb = Get-SPWeb $DestinationWebURL 
$dList = $dWeb.Lists | ? {$_.title -like $DestinationLibraryTitle} 

$AllFolders = $sList.Folders 
$RootFolder = $sList.RootFolder 
$RootItems = $RootFolder.files 

foreach($RootItem in $RootItems) 
{ 
    $sBytes = $RootItem.OpenBinary() 
    $dFile = $dList.RootFolder.Files.Add($RootItem.Name, $sBytes, $true) 

    $AllFields = $RootItem.Item.Fields | ? {!($_.sealed)} 

    foreach($Field in $AllFields) 
    { 
        if($RootItem.Properties[$Field.Title]) 
        { 
            if(!($dFile.Properties[$Field.title])) 
            { 
                $dFile.AddProperty($Field.Title, $RootItem.Properties[$Field.Title]) 
            } 
            else 
            { 
                $dFile.Properties[$Field.Title] = $RootItem.Properties[$Field.Title] 
            } 
        } 
    } 
    $dFile.Update() 
} 

foreach($Folder in $AllFolders) 
{ 
    Remove-Variable ParentFolderURL 
    $i = 0 

    $FolderURL = $Folder.url.Split("/") 

    while($i -lt ($FolderURL.count-1)) 
    { 
    $ParentFolderURL = "$ParentFolderURL/" + $FolderURL[$i] 
    $i++ 
    } 

    $CurrentFolder = $dList.Folders | ? {$_.url -eq $ParentFolderURL.substring(1)} 
    if(!($CurrentFolder.Folders | ? {$_.name -eq $Folder.Name})) 
    { 
        $NewFolder = $dlist.Folders.Add(("$DestinationWebURL" + $ParentFolderURL), [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $Folder.name) 
        $NewFolder.update() 
    } 
    else 
    { 
        $NewFolder = $dList.Folders | ? {$_.name -eq $Folder.Name} 
    } 
    $AllFiles = $sList.Items 
    $sItems = $Folder.folder.Files 

    if($Folder.Folder.Files.count -gt 0) 
    { 
        foreach($item in $sItems) 
        { 

            $Relative = ($Item.ServerRelativeUrl).substring(1) 
            $TargetItem = $AllFiles | ? {$_.URL -eq $Relative} 
            $sBytes = $TargetItem.File.OpenBinary() 
            $dFile = $Newfolder.Folder.Files.Add($TargetItem.Name, $sBytes, $true) 
            $AllFields = $TargetItem.Fields | ? {!($_.sealed)} 

            foreach($Field in $AllFields) 
            { 
                if($TargetItem.Properties[$Field.Title]) 
                { 
                    if(!($dFile.Properties[$Field.title])) 
                    { 
                        $dFile.AddProperty($Field.Title, $TargetItem.Properties[$Field.Title]) 
                    } 
                    else 
                    { 
                        $dFile.Properties[$Field.Title] = $TargetItem.Properties[$Field.Title] 
                    } 
                } 
            } 
            $dFile.Update() 
        } 
    } 
}
1
  • I guess there's something to do with $folder.Folder.Files.Add But I keep getting an error with this
    – Nico
    Commented Nov 18, 2014 at 9:35

1 Answer 1

1

Ok, I got it !

I need to do this :

$myfolder = $library.Folders | ?{$_.Name -eq "My Folder"}

$sBytes = $Item.OpenBinary()

$newfile = $myfolder.Folder.Files.Add($myfolder.Url + "/" + $Item.Name, $sBytes, $true)

And this works.

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.