hwo to move nested folder as it is to sharepoint document lib

Answered hwo to move nested folder as it is to sharepoint document lib

  • Thursday, December 06, 2012 7:03 AM
     
      Has Code

    Hi All,
         I putting up a script together to move nested folders with files in it to a doc lib n sharepoint as it is
    I'm sure i should be able to use powershell for this and i need an assistance ... almost there.

    This snipped does work but i get error:

    Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'OpenRead'.

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
    
    #[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null
    
    [System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 
    [System.Reflection.Assembly]::LoadWithPartialName("System.web")
    
    function UploadAllFiles($WebURL, $DocLibName, $FolderPath)
    {
    
     #Get the Web & Lists to upload the file
     $site = New-Object Microsoft.SharePoint.SPSite($WebURL)
     $web= $site.OpenWeb()
    
    
    #Get the Target Document Library to upload
    $List = $Web.GetFolder($DocLibName)
    
    #Get the Files from Local Folder
    $Files = Get-ChildItem $FolderPath 
    #You can filter files by: -filter “*.pdf” 
    
    #upload the files
    foreach ($File in $Files)
    {
        #Get the Contents of the file to FileStream 
    
        #Error here Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'OpenRead'.
        $stream = (Get-Item $file.FullName).OpenRead() 
    	                      
        $uploaded = $List.Files.Add($File.Name,$stream)               
        
    	$stream.Dispose()
    }
    
    #Dispose the site object
    $site.Dispose()
    }
    
    
    #call the upload function
    UploadAllFiles "http://ServerName/" "TestDoc" "D:\myrootfolder"

    This works but it doesn't copy the nested folder across as it is it just copies only the files inside the root folder.

    Any help will be appreciated.

    Thanks a lot

           

All Replies