SOLVED

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

%3CLINGO-SUB%20id%3D%22lingo-sub-33018%22%20slang%3D%22en-US%22%3EOfficeDevPNP%20Add-PnPFile%20error%20folder%20already%20exists%20while%20looping%20through%20directories%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-33018%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20getting%20the%20following%20error%20while%20looping%20through%20a%20local%20directory%20structure%20and%20trying%20to%20upload%20all%20files%20to%20a%20SPO%20library.%20The%20first%20file%20in%20each%20directory%20succeeds%20but%20the%20following%20files%20in%20the%20same%20direcotry%20give%20an%20error.%20It%20seems%20to%20me%20the%20context%20is%20outdated%20(OfficeDevPNP%20doesn't%20seem%20to%20know%20that%20the%20folder%20was%20already%20created%20in%20the%20previous%20iteration%20of%20the%20loop.%20How%20do%20I%20fix%20this%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAdd-PnPFile%20%3A%20Er%20bestaat%20al%20een%20bestand%20of%20map%20met%20de%20naam%20%3CA%20href%3D%22https%3A%2F%2Fmytenant.sharepoint.com%2Fsites%2Fmysite%2Fmylibrary%2Fmyfolder%2Fmysubfolder%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fmytenant.sharepoint.com%2Fsites%2Fmysite%2Fmylibrary%2Fmyfolder%2Fmysubfolder%2F%3C%2FA%3E%3CBR%20%2F%3Esubsubfolder.%20(%22A%20File%20or%20Folder%20already%20exists%20with%20the%20name%22)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E(Part%20of)%20the%20code%3A%3C%2FP%3E%3CP%3E%24fileNames%20%3D%20Get-ChildItem%20-Path%20%24scriptPath%20-Recurse%20-Include%20*.json%2C*.xml%3B%3C%2FP%3E%3CP%3Eforeach(%24aFileName%20in%20%24fileNames)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%23%20create%20folder%20structure%20leading%20to%20file%20in%20SPO%3CBR%20%2F%3E%24fileLocalSourcePath%20%3D%20%24aFileName.FullName%3B%3CBR%20%2F%3E%24fileSPOTargetPath%20%3D%20%24topSPOFolder%20%2B%20(%24fileLocalSourcePath.Replace(%24sourcePath%2C%20'%20').TrimStart('%20')%20-replace%20'%5C%5C'%2C%20'%2F')%3B%3CBR%20%2F%3E%24fileSPOTargetPath%20%3D%20%24fileSPOTargetPath.Replace('%2F'%20%2B%20%24aFileName.Name%2C%20'%20')%3B%3CBR%20%2F%3E%23%20upload%20file%20to%20SPO%3CBR%20%2F%3EAdd-PnPFile%20-Path%20%24fileLocalSourcePath%20-Folder%20%24fileSPOTargetPath%20-Web%20%24aWeb%20-Publish%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-33018%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPnP%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-269225%22%20slang%3D%22en-US%22%3ERe%3A%20OfficeDevPNP%20Add-PnPFile%20error%20folder%20already%20exists%20while%20looping%20through%20directories%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-269225%22%20slang%3D%22en-US%22%3E%3CP%3EI%20had%20the%20same%20error.%3CBR%20%2F%3E%3CBR%20%2F%3EFor%20me%20it%20was%20that%20my%20library%20was%20called%3A%20%22Gedeelde%20Documenten%22%20(%3D%20dutch%20for%20%22Shared%20Documents%22).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20I%20did%3A%20%26nbsp%3B%3C%2FP%3E%3CUL%3E%3CLI%3EAdd-PnPFile%20-Path%20'.%5Ctest.docx'%20-Folder%20'%5CGedeelde%20Documenten%5C1433%5C'%26nbsp%3B%3C%2FLI%3E%3C%2FUL%3E%3CP%3EThis%20gave%20me%20the%20error%20that%20the%20folder%201433%20already%20exists.%3C%2FP%3E%3CP%3EI've%20change%20the%20syntax%20to%3A%3C%2FP%3E%3CUL%3E%3CLI%3E%3CSPAN%3EAdd-PnPFile%20-Path%20'.%5Ctest.docx'%20-Folder%20'%5CGedeelde%2520Documenten%5C1433%5C'%26nbsp%3B%3C%2FSPAN%3E%3C%2FLI%3E%3C%2FUL%3E%3CP%3E%3CSPAN%3Eand%20everything%20is%20working%20fine%20now!%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3ERegards%2C%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EReinout%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-33553%22%20slang%3D%22en-US%22%3ERe%3A%20OfficeDevPNP%20Add-PnPFile%20error%20folder%20already%20exists%20while%20looping%20through%20directories%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-33553%22%20slang%3D%22en-US%22%3E%3CP%3EMade%20few%20corrections%20in%20your%20script%20to%20work.%20Below%20is%20the%20updated%20script.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24sourcePath%20%3D%20%24PSScriptRoot%3B%20%23%201.Don't%20forget%20to%20add%20%20%22%5C%22%20at%20end%20of%20%24PSScriptRoot%20String%20value%20%0A%23%24topSPOFolder%20%3D%20%22SiteConfigurations%2F_PSI_Gestapeld%22%3B%0A%24topSPOFolder%20%3D%20%22Bieb1%2F_PSI_Gestapeld%22%3B%0A%23%20install%20pnp%20powershell..%3F%0A%23Install-Module%20SharePointPnPPowerShellOnline%3B%0A%23%20connect%20to%20spo%20online%3B%20TODO%3A%20use%20appid%20and%20appsecret%0AConnect-PnPOnline%20-Url%20%24makeUrl%20-UseWebLogin%3B%0A%23%20foreach%20(updated%20or%20new%3F)%20file%20in%20git%20project%0A%24fileNames%20%3D%20Get-ChildItem%20-Path%20%24sourcePath%20-Recurse%20-Include%20*.json%2C*.xml%3B%0A%24fileNames%20%3D%20Get-ChildItem%20-Path%20%24sourcePath%20-Recurse%20%3B%0Aforeach(%24aFileName%20in%20%24fileNames)%0A%7B%0Aif(%24aFileName.GetType().Name%20-ne%20%22DirectoryInfo%22)%20%0A%20%20%20%7B%0A%20%20%20%20%20%24filepath%3D%20%5BSystem.IO.Path%5D%3A%3AGetDirectoryName(%24aFileName.FullName)%0A%20%20%20%20%20%24Urlpath%3D%20(%24filepath.Replace(%24sourcePath%2C%20''))%3B%0A%20%20%20%20%20%24foldername%3D%24Urlpath.Replace(%22%2F%22%2C%22%5C%22)%3B%20%20%23%202.%22%5C%22%2C%22%2F%22%20was%20changed%0A%20%20%20%20%20%24fn%3D%24topSPOFolder%2B%22%5C%22%2B%24foldername%3B%20%23%203.%20%22%5C%22%20Added%20%0A%20%20%20%20%20Add-PnPFile%20-Path%20%24aFileName.FullName%20-Folder%20%24fn%3B%0A%20%20%20%20%20%24fn%3D%24null%0A%20%20%20%7D%0A%0A%7D%20%0A%0A%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-33315%22%20slang%3D%22en-US%22%3ERe%3A%20OfficeDevPNP%20Add-PnPFile%20error%20folder%20already%20exists%20while%20looping%20through%20directories%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-33315%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3BThank%20you%20NarasimaPerumal!%20You%20script%20works%20perfectly%20although%20I%20don't%20understand%20yet%20why%20mine%20doesn't.%20I%20combined%20both%20our%20scripts%20to%20make%20the%20below%20script.%20Add-PnpFile%20creates%20the%20required%20folders%20itself%20so%20it%20isnt%20necessary%20to%20use%20Add-PnPFolder%20separately.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24sourcePath%20%3D%20%24PSScriptRoot%3B%3CBR%20%2F%3E%23%24topSPOFolder%20%3D%20%22SiteConfigurations%2F_PSI_Gestapeld%22%3B%3CBR%20%2F%3E%24topSPOFolder%20%3D%20%22Bieb1%2F_PSI_Gestapeld%22%3B%3C%2FP%3E%3CP%3E%23%20install%20pnp%20powershell..%3F%3CBR%20%2F%3E%23Install-Module%20SharePointPnPPowerShellOnline%3B%3C%2FP%3E%3CP%3E%23%20connect%20to%20spo%20online%3B%20TODO%3A%20use%20appid%20and%20appsecret%3CBR%20%2F%3EConnect-PnPOnline%20-Url%20%24makeUrl%20-UseWebLogin%3B%3C%2FP%3E%3CP%3E%23%20foreach%20(updated%20or%20new%3F)%20file%20in%20git%20project%3CBR%20%2F%3E%24fileNames%20%3D%20Get-ChildItem%20-Path%20%24sourcePath%20-Recurse%20-Include%20*.json%2C*.xml%3B%3C%2FP%3E%3CP%3Eforeach(%24aFileName%20in%20%24fileNames)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24filepath%3D%20%5BSystem.IO.Path%5D%3A%3AGetDirectoryName(%24aFileName.FullName)%3CBR%20%2F%3E%24Urlpath%3D%20(%24filepath.Replace(%24sourcePath%2C%20''))%3B%3CBR%20%2F%3E%24foldername%3D%24Urlpath.Replace(%22%5C%22%2C%22%2F%22)%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%24fn%3D%24topSPOFolder%2B%24foldername%3B%3C%2FP%3E%3CP%3EAdd-PnPFile%20-Path%20%24aFileName.FullName%20-Folder%20%24fn%3B%3CBR%20%2F%3E%24fn%3D%24null%3CBR%20%2F%3E%7D%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-33301%22%20slang%3D%22en-US%22%3ERe%3A%20OfficeDevPNP%20Add-PnPFile%20error%20folder%20already%20exists%20while%20looping%20through%20directories%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-33301%22%20slang%3D%22en-US%22%3E%3CP%3ECheck%20the%20below%20script%2C%20which%20will%20loop%20through%20the%20directories%20and%20add%20the%20required%20folder%20and%20upload%20the%20files%20to%20SPO.%20While%20modifying%20%22%24folder%22%2C%20don't%20forget%20to%20have%20%22%5C%22%20at%20the%20end.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24cred%3DGet-Credential%0AConnect-PnPOnline%20-Url%20https%3A%2F%2Ftenantname.sharepoint.com%2Fsites%2Fcontosobeta%20-Credentials%20%24cred%0A%24Folder%20%3D%20%20%22C%3A%5CDesktop%5CUserfolder2%5C%22%0A%24rootfolder%3D%24folder.Replace(%22%5C%22%2C%22%5C%5C%22)%0A%24ParentFolder%20%3D%22Shared%20Documents%5Ctesting%22%20%0A%0AFunction%20UploadFiles(%24path)%0A%7B%0A%0A%20%24files%20%3D%20Get-ChildItem%20%24path%0A%20foreach%20(%24file%20in%20%24files)%20%0A%20%7B%20%20%0A%20%20if(%24file.GetType().Name%20-eq%20%22DirectoryInfo%22)%20%0A%20%20%7B%0A%20%20%20%24foldername%3D%24file.Name%0A%20%20%20%24parent%3D%24file.Parent.FullName%0A%20%20%20%24parent1%3D%24parent%2B%22%5C%22%0A%20%20%20If(%24parent1%20-eq%20%24Folder)%0A%20%20%20%7B%0A%20%20%20%20Add-PnPFolder%20-Name%20%24foldername%20-folder%20%24ParentFolder%0A%20%20%20%7D%0A%20%20%20Else%0A%20%20%20%7B%0A%20%20%20%20%24Urlpath1%3D%20(%24parent%20-split%20%22%24rootfolder%22%20)%5B1%5D%0A%20%20%20%20%24foldernames1%3D%24Urlpath1%0A%20%20%20%20if(%24foldernames1%20-like%20%22*%5C*%22)%0A%20%20%20%20%7B%0A%20%20%20%20%20%24foldername1%3D%24foldernames1.Replace(%22%5C%22%2C%22%2F%22)%0A%20%20%20%20%7D%0A%20%20%20%20Else%0A%20%20%20%20%7B%0A%20%20%20%20%20%24foldername1%3D%24foldernames1%0A%20%20%20%20%7D%0A%20%20%20%20%24fnPath%3D%24ParentFolder%2B%22%2F%22%2B%24foldername1%0A%20%20%20%20Add-PnPFolder%20-Name%20%24foldername%20-folder%20%24fnPath%0A%20%20%20%7D%0A%0A%20%20%20%24Folderpath1%3D%24file.FullName%0A%0A%20%20%20UploadFiles%20%20%24Folderpath1%0A%20%20%7D%0A%20%20Else%0A%20%20%7B%0A%20%20%20%24filepath%3D%20%5BSystem.IO.Path%5D%3A%3AGetDirectoryName(%24file.FullName)%0A%20%20%20%24Urlpath%3D%20(%24filepath%20-split%20%22%24rootfolder%22%20)%5B1%5D%0A%20%20%20%24foldernames%3D%24Urlpath%0A%20%20%20if(%24foldernames%20-like%20%22*%5C*%22)%0A%20%20%20%7B%0A%20%20%20%20%24foldername%3D%24foldernames.Replace(%22%5C%22%2C%22%2F%22)%0A%20%20%20%7D%0A%20%20%20Else%0A%20%20%20%7B%0A%20%20%20%20%24foldername%3D%24foldernames%0A%20%20%20%7D%20%20%20%0A%20%20%20%24fn%3D%24ParentFolder%2B%22%2F%22%2B%24foldername%0A%0A%20%20%20Add-PnPFile%20-Path%20%24File.FullName%20-Folder%20%24fn%20%3B%20%20%20%0A%20%20%20%24fn%3D%24null%0A%20%20%7D%0A%20%7D%0A%7D%0AUploadFiles%20-path%20%24Folder%0A%0A%0A%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-33061%22%20slang%3D%22en-US%22%3ERE%3A%20OfficeDevPNP%20Add-PnPFile%20error%20folder%20already%20exists%20while%20looping%20through%20directories%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-33061%22%20slang%3D%22en-US%22%3EMaybe%20you%20should%20groep%20the%20files%20to%20each%20folder...%20and%20for%20each%20folder%20iterate%20through%20the%20array%20instead%20of%20for%20each%20file.%20That%20makes%20it%20easier%20to%20debug%20if%20something%20is%20not%20working.%20(je%20mag%20me%20ook%20een%20mailtje%20sturen%2C%20dan%20kan%20ik%20verder%20meedenken%20%3A)%3C%2Fimg%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

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 the same direcotry give an error. It seems to me the context is outdated (OfficeDevPNP doesn't seem to know that the folder was already created in the previous iteration of the loop. How do I fix this?

 

Add-PnPFile : Er bestaat al een bestand of map met de naam https://mytenant.sharepoint.com/sites/mysite/mylibrary/myfolder/mysubfolder/
subsubfolder. ("A File or Folder already exists with the name")

 

(Part of) the code:

$fileNames = Get-ChildItem -Path $scriptPath -Recurse -Include *.json,*.xml;

foreach($aFileName in $fileNames)
{
# create folder structure leading to file in SPO
$fileLocalSourcePath = $aFileName.FullName;
$fileSPOTargetPath = $topSPOFolder + ($fileLocalSourcePath.Replace($sourcePath, ' ').TrimStart(' ') -replace '\\', '/');
$fileSPOTargetPath = $fileSPOTargetPath.Replace('/' + $aFileName.Name, ' ');
# upload file to SPO
Add-PnPFile -Path $fileLocalSourcePath -Folder $fileSPOTargetPath -Web $aWeb -Publish;
}

5 Replies
Highlighted
Maybe you should groep the files to each folder... and for each folder iterate through the array instead of for each file. That makes it easier to debug if something is not working. (je mag me ook een mailtje sturen, dan kan ik verder meedenken :)
Highlighted

Check the below script, which will loop through the directories and add the required folder and upload the files to SPO. While modifying "$folder", don't forget to have "\" at the end.

 

$cred=Get-Credential
Connect-PnPOnline -Url https://tenantname.sharepoint.com/sites/contosobeta -Credentials $cred
$Folder =  "C:\Desktop\Userfolder2\"
$rootfolder=$folder.Replace("\","\\")
$ParentFolder ="Shared Documents\testing" 

Function UploadFiles($path)
{

	$files = Get-ChildItem $path
	foreach ($file in $files) 
	{  
		if($file.GetType().Name -eq "DirectoryInfo") 
		{
			$foldername=$file.Name
			$parent=$file.Parent.FullName
			$parent1=$parent+"\"
			If($parent1 -eq $Folder)
			{
				Add-PnPFolder -Name $foldername -folder $ParentFolder
			}
			Else
			{
				$Urlpath1= ($parent -split "$rootfolder" )[1]
				$foldernames1=$Urlpath1
				if($foldernames1 -like "*\*")
				{
					$foldername1=$foldernames1.Replace("\","/")
				}
				Else
				{
					$foldername1=$foldernames1
				}
				$fnPath=$ParentFolder+"/"+$foldername1
				Add-PnPFolder -Name $foldername -folder $fnPath
			}

			$Folderpath1=$file.FullName

			UploadFiles  $Folderpath1
		}
		Else
		{
			$filepath= [System.IO.Path]::GetDirectoryName($file.FullName)
			$Urlpath= ($filepath -split "$rootfolder" )[1]
			$foldernames=$Urlpath
			if($foldernames -like "*\*")
			{
				$foldername=$foldernames.Replace("\","/")
			}
			Else
			{
				$foldername=$foldernames
			}   
			$fn=$ParentFolder+"/"+$foldername

			Add-PnPFile -Path $File.FullName -Folder $fn ;   
			$fn=$null
		}
	}
}
UploadFiles -path $Folder


Highlighted

 Thank you NarasimaPerumal! You script works perfectly although I don't understand yet why mine doesn't. I combined both our scripts to make the below script. Add-PnpFile creates the required folders itself so it isnt necessary to use Add-PnPFolder separately.

 

 

$sourcePath = $PSScriptRoot;
#$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;

foreach($aFileName in $fileNames)
{
$filepath= [System.IO.Path]::GetDirectoryName($aFileName.FullName)
$Urlpath= ($filepath.Replace($sourcePath, ''));
$foldername=$Urlpath.Replace("\","/");

$fn=$topSPOFolder+$foldername;

Add-PnPFile -Path $aFileName.FullName -Folder $fn;
$fn=$null
}

Highlighted
Solution

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
   }

} 

Highlighted

I had the same error.

For me it was that my library was called: "Gedeelde Documenten" (= dutch for "Shared Documents").

 

So I did:  

  • Add-PnPFile -Path '.\test.docx' -Folder '\Gedeelde Documenten\1433\' 

This gave me the error that the folder 1433 already exists.

I've change the syntax to:

  • Add-PnPFile -Path '.\test.docx' -Folder '\Gedeelde%20Documenten\1433\' 

and everything is working fine now!

 

Regards,

 

Reinout