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

How do I update columns in a library using PowerShell during a file upload?

$
0
0

I am trying to put together a script that will do a bulk upload of files along with associated metadata into a SP library. The first part of the requirement is to upload .pdf files while grabbing the metadata from the file name. Currently, my script does the uploads, but it it does not update the fields with the metadata it is getting from the file names. Here is what my script curently looks like

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}

#Script settings
$webUrl = "http://llc-hdc-spfe1d:19500/sites/SampleRecordCenter/"
$docLibraryName = "My Library"
$docLibraryUrlName = "MyLibrary"
$localFolderPath = get-childitem "C:\test" -recurse

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

ForEach($file in $files)
{
    if ($localFolderPath | where {$_.extension -eq ".pdf"})
    {
        #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
		$myArray = @()

        foreach ($MetaDataString in $FileNameArray)
		{
            #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)

		    if ($FileNameArray.Length -eq 3)
		    {
			    #populate columns
			    $spItem = $docLibrary.AddItem()
			    $spItem["FirstColumn"] = $myArray[0]
			    $spItem["SecondColumn"] = $myArray[1]
			    $spItem["ThirdColumn"] = $myArray[2]
			    $spItem.Update()
		    }
		    elseif ($myArray.Length -eq 4)
		    {
			    #populate columns
			    $spItem = $docLibrary.AddItem()
			    $spItem["FirstColumn"] = $myArray[0]
			    $spItem["SecondColumn"] = $myArray[1]
			    $spItem["ThirdColumn"] = $myArray[2]
			    $spItem["FourthColumn"] = $myArray[3]
			    $spItem.Update()
		    }
            #Close file stream
            $fileStream.Close();
        }
    }
}
#Dispose web
$web.Dispose()

The .pdf files have the same naming convention like "first_second_third.pdf" and "first_second_third_fourth.pdf"...I want to grab each part of the file name, and put that data in the associated column in the library. Right now, am getting my file name and storing that information in an array, but my code isn't updating each column as I hope it will. What am I doing wrong here?

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>