Skip to main content
added 1259 characters in body
Source Link
vinit kumar
  • 823
  • 6
  • 19

with folder

function UploadImages($weburl)
{
$docLibraryName = "TestLibrary"
$localFolderPath = "c:\Install\Docs"
$subFolderName="SubFolder" ##Pass your Folder Name Here

Add-PsSnapin Microsoft.SharePoint.PowerShell -erroraction silentlycontinue 
$web = Get-SPWeb -Identity $webUrl

$docLibrary = $web.Lists[$docLibraryName] 
#Attach to local folder and enumerate through all files
$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles() | ForEach-Object { 

    #Create file stream object from file
    $fileStream = ([System.IO.FileInfo] (Get-Item $_.FullName)).OpenRead()
    $contents = new-object byte[] $fileStream.Length
    $fileStream.Read($contents, 0, [int]$fileStream.Length);
    $fileStream.Close(); 

    write-host "Copying" $_.Name "to" $docLibrary.Title "in" $web.Title "..." 

    #Add file
    $folder = $web.getfolder($docLibrary.Title+"/"+$subFolderName) ##Changed code here to get subfolder
    $spFile = $folder.Files.Add($folder.Url + "/" + $_.Name, $contents, $true)
    $spItem = $spFile.Item 



Write-Host -f Green "Added Images to library !!!"
}
}

Example with C# click here

with folder

function UploadImages($weburl)
{
$docLibraryName = "TestLibrary"
$localFolderPath = "c:\Install\Docs"
$subFolderName="SubFolder" ##Pass your Folder Name Here

Add-PsSnapin Microsoft.SharePoint.PowerShell -erroraction silentlycontinue 
$web = Get-SPWeb -Identity $webUrl

$docLibrary = $web.Lists[$docLibraryName] 
#Attach to local folder and enumerate through all files
$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles() | ForEach-Object { 

    #Create file stream object from file
    $fileStream = ([System.IO.FileInfo] (Get-Item $_.FullName)).OpenRead()
    $contents = new-object byte[] $fileStream.Length
    $fileStream.Read($contents, 0, [int]$fileStream.Length);
    $fileStream.Close(); 

    write-host "Copying" $_.Name "to" $docLibrary.Title "in" $web.Title "..." 

    #Add file
    $folder = $web.getfolder($docLibrary.Title+"/"+$subFolderName) ##Changed code here to get subfolder
    $spFile = $folder.Files.Add($folder.Url + "/" + $_.Name, $contents, $true)
    $spItem = $spFile.Item 



Write-Host -f Green "Added Images to library !!!"
}
}

Example with C# click here

Source Link
vinit kumar
  • 823
  • 6
  • 19

I think you can create Console application with C# or use Powershell. I think the Powershell way is too easy and there are lot of articles available in web.

 #Specify tenant admin and site URL  
$User = "Your id"  
$Password = 'Your Password'  
$SiteURL = "Site URL/"  
$Folder = "C:\Folder\Folderinside"  
#Path where you want to Copy  
$DocLibName = "Documents"  
#Docs library  
# Add references to SharePoint client assemblies and authenticate to Office 365 site - required  
for CSOM  
Add - Type - Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
Add - Type - Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"#  
Bind to site collection  
$Context = New - Object Microsoft.SharePoint.Client.ClientContext($SiteURL)  
$Creds = New - Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User, (ConvertTo - SecureString $Password - AsPlainText - Force))  
$Context.Credentials = $Creds  
Retrieve list  
$List = $Context.Web.Lists.GetByTitle($DocLibName)  
$Context.Load($List)  
$Context.ExecuteQuery()  
# Upload file  
Foreach($File in (dir $Folder - File))  
{  
    $FileStream = New - Object IO.FileStream($File.FullName, [System.IO.FileMode]::Open)  
    $FileCreationInfo = New - Object Microsoft.SharePoint.Client.FileCreationInformation  
    $FileCreationInfo.Overwrite = $true  
    $FileCreationInfo.ContentStream = $FileStream  
    $FileCreationInfo.URL = $File  
    $Upload = $List.RootFolder.Files.Add($FileCreationInfo)  
    $Context.Load($Upload)  
    $Context.ExecuteQuery()  
}