Home > database >  Upload Files and Folder To Sharepoint - MISSING 1x Syntex
Upload Files and Folder To Sharepoint - MISSING 1x Syntex

Time:01-02

OBJECTIVE: Upload Local On Prem Files and Folder to SharePoint Online

PROBLEM: I have pulled from other resouces to compose the below Power Shell to upload Files and Folders from my on prem server to Host Share Point. The code works great but the PROBLEM is .... It Over Writes exsisting files and I can not figure out how to add a syntex to skip exsisting files any help would be appraciated


#Variables
$SiteURL = "https://xxxxxxx.sharepoint.com/sites/BTTeamTest1"
$FolderLocalPath = "C:\_BT-Test\"
$TargetFolder = "Shared Documents/general" #Site Relative URL
 
#Connect with SharePoint Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Function to upload all files from a local folder to SharePoint Online Folder
Function Upload-PnPFolder($LocalFolderPath, $TargetFolderURL)
{
    Write-host "Processing Folder:"$LocalFolderPath -f Yellow
    #Get All files and SubFolders from the local disk
    $Files = Get-ChildItem -Path $LocalFolderPath -File
 
    #Ensure the target folder
    Resolve-PnPFolder -SiteRelativePath $TargetFolderURL | Out-Null
 
    #Upload All files from the local folder to SharePoint Online Folder
    ForEach ($File in $Files)
    {
        Add-PnPFile -Path "$($File.Directory)\$($File.Name)" -Folder $TargetFolderURL -Values @{"Title" = $($File.Name)} | Out-Null
        Write-host "`tUploaded File:"$File.FullName -f Green
    }
}
#Call the function to upload the Root Folder
Upload-PnPFolder -LocalFolderPath $LocalFolderPath -TargetFolderURL $TargetFolderURL
 
#Get all Folders from given source path
Get-ChildItem -Path $LocalFolderPath -Recurse -Directory | ForEach-Object {
    $FolderToUpload = ($TargetFolderURL $_.FullName.Replace($LocalFolderPath,[string]::Empty)).Replace("\","/")
    Upload-PnPFolder -LocalFolderPath $_.FullName -TargetFolderURL $FolderToUpload
}



I THINK this is the line that needs to be modded but not positive

    #Upload All files from the local folder to SharePoint Online Folder
    ForEach ($File in $Files)
    {
        Add-PnPFile -Path "$($File.Directory)\$($File.Name)" -Folder $TargetFolderURL -Values @{"Title" = $($File.Name)} | Out-Null
        Write-host "`tUploaded File:"$File.FullName -f Green
    }

CodePudding user response:

Are there many files to upload? Why not use drag & drop or sync via OneDrive? You can try the follow code:

#Declare Variables
$Site = "https://mytenant.sharepoint.com"
$FilesLocal = "C:\SharePoint"
$Library = "/Shared Documents/"
 
#Connect with SharePoint Online
Connect-PnPOnline -Url $Site -UseWebLogin

$Files = Get-ChildItem -Path $FilesLocal -Force -Recurse
ForEach ($File in $Files)
{Add-PnPFile -Path "$($File.Directory)\$($File.Name)" -Folder $Library  -Values @{"Title" = $($File.Name)}}

CodePudding user response:

You will have to use the Find-PnPFile cmdlet to check if the file exists. Disclaimer: I have inserted the Find-PnPFile check into your ForEach loop. However, I have not tested it. $matching_files is a collection. Please check the count of that too (If its greater than 0)

#Upload All files from the local folder to SharePoint Online Folder
ForEach ($File in $Files)
{
    $matching_files = Find-PnPFile -Folder $TargetFolderURL -Match $($File.Name)
    If($matching_files -neq $null)
    {
       continue
    }

    Add-PnPFile -Path "$($File.Directory)\$($File.Name)" -Folder $TargetFolderURL -Values @{"Title" = $($File.Name)} | Out-Null
    Write-host "`tUploaded File:"$File.FullName -f Green
}
  • Related