Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Powershell: There is no file with URL error during file upload.

$
0
0

I am trying to put together a PowerShell script that I could use to upload files from a local directory, into a Document Library in a Record Center Site. Here is what my script looks like...

#Open web and library
$web = Get-SPWeb $webUrl
$docLibrary = $web.Lists[$docLibraryName]
$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles()
Write-Host "Local Directory:" $localFolderPath

ForEach($file in $files)
{
	Write-Host "Looping through files"
	Write-Host "Current file:" $file

	ElseIf ($contentType = "MyContentType")
	{
		#Open file
		$fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

		# Gather the file name
		$FileName = $File.Name

		#remove file extension
		$NewName = [IO.Path]::GetFileNameWithoutExtension($FileName)

		#split the file name by the "-" character
		$FileNameArray = $NewName.split("_")
		$check = $FileNameArray.Length

		#Add file
		$folder =  $web.getfolder($docLibraryUrlName)
		write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..."
		$spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)
		$spItem = $spFile.Item

		write-host "Success"

		#populate columns
		$spItem["Application Number"] = $FileNameArray[0].ToString()
		$spItem["Site Number"] = $FileNameArray[1].ToString()
		$spItem.Update()

		$fileStream.Close();
	}
}

Each time I run my script, I get this error message 

Exception calling "Add" with "3" argument(s): "<nativehr>0x80070003</nativehr><nativestack></nativestack>There is no file with URL 'http://myRecordLibrarySite/myRecord Library/12587_B2317.PDF' in this Web."
At C:\powershellscripts\Upload-FilesIntoSharePoint.ps1:72 char:6
+                     $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stre ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DirectoryNotFoundException

I have not been able to nail down why the script is failing. Any ideas why? Thanks for the help.


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>