Forum Discussion

Jurgen Wiersema's avatar
Jurgen Wiersema
Copper Contributor
Nov 30, 2016
Solved

OfficeDevPNP Add-PnPFile error folder already exists while looping through directories

I'm getting the following error while looping through a local directory structure and trying to upload all files to a SPO library. The first file in each directory succeeds but the following files in...
  • NarasimaPerumal Chandramohan's avatar
    NarasimaPerumal Chandramohan
    Dec 02, 2016

    Made few corrections in your script to work. Below is the updated script.

     

    $sourcePath = $PSScriptRoot; # 1.Don't forget to add  "\" at end of $PSScriptRoot String value 
    #$topSPOFolder = "SiteConfigurations/_PSI_Gestapeld";
    $topSPOFolder = "Bieb1/_PSI_Gestapeld";
    # install pnp powershell..?
    #Install-Module SharePointPnPPowerShellOnline;
    # connect to spo online; TODO: use appid and appsecret
    Connect-PnPOnline -Url $makeUrl -UseWebLogin;
    # foreach (updated or new?) file in git project
    $fileNames = Get-ChildItem -Path $sourcePath -Recurse -Include *.json,*.xml;
    $fileNames = Get-ChildItem -Path $sourcePath -Recurse ;
    foreach($aFileName in $fileNames)
    {
    if($aFileName.GetType().Name -ne "DirectoryInfo") 
       {
         $filepath= [System.IO.Path]::GetDirectoryName($aFileName.FullName)
         $Urlpath= ($filepath.Replace($sourcePath, ''));
         $foldername=$Urlpath.Replace("/","\");  # 2."\","/" was changed
         $fn=$topSPOFolder+"\"+$foldername; # 3. "\" Added 
         Add-PnPFile -Path $aFileName.FullName -Folder $fn;
         $fn=$null
       }
    
    } 
    
    

Resources