Import CSV file into Arrary and add new value if the data doesnt exist in csv

Hi,

I Have csv file with 3 columns and 1000’s of rows so What I like to do is check the csv file and OrgName doesnt exist then add the row.

csv file data

OrgName | MovieName | mpaa
Name 1 | M name 1 | R
Name 2 | M name 2 | 12
Name 45 | M name 45 | U

I want to match the mp4 name with orgname if existed in csv file then dont add or make a change the data.

If it doesnt existed then add the data into csv file.

I try following code but it didnt work

$movies_a=@( Import-Csv “C:\Test_Movies.csv” )
Get-ChildItem -LiteralPath “C:\Test” -Filter *.nfo -Force |
ForEach-Object {

if( $movies_a -notcontains $.BaseName)
{
$file_path_info=$
.FullName
$m.OrgName

$file = New-Object System.Xml.XmlDocument
$file.Load($file_path_info)
$file_data=$file.DocumentElement

$movie_a = New-Object PSObject
$movie_a | Add-Member -Type NoteProperty -Name OrgName -Value $_.BaseName
$movie_a | Add-Member -Type NoteProperty -Name MovieName -Value $file_data.title
$movie_a | Add-Member -Type NoteProperty -Name mpaa -Value $file_data.mpaa
$movie_a +=$movie_a
}else{
Write-Host “Already in csv”
}
}

$movie_a | Export-Csv C:\Test_Movies.csv -NoTypeInformation -Force

$CSVFilePath = '.\_Movies.csv'
"OrgName","MovieName","mpaa" | Out-File $CSVFilePath
"Name 1","M name 1","R"      | Out-File $CSVFilePath -Append
"Name 2","M name 2","12"     | Out-File $CSVFilePath -Append
"Name 45","M name 45","U"    | Out-File $CSVFilePath -Append

$movies_a = Import-Csv $CSVFilePath
foreach ($FileInfo in (Get-ChildItem -LiteralPath "$(Split-path $CSVFilePath)\" -Filter *.nfo -Force)) {
    if( $FileInfo.BaseName -notin $movies_a.OrgName ) {

        $file = New-Object System.Xml.XmlDocument
        $file.Load($FileInfo.FullName)
        $file_data = $file.DocumentElement

        $movie_a += New-Object PSObject -Property ([Ordered]@{
            OrgName   = $FileInfo.BaseName
            MovieName = $file_data.title
            mpaa      = $file_data.mpaa
        })
    } else {
        Write-Output 'Already in csv'
    }
}

$movie_a | Export-Csv ($CSVFilePath -replace '.csv','-Updated.csv') -NoTypeInformation -Force

I use following code to fix the issue.

$movies = @()

$csvFile = “C:\Test_Movies.csv”
if(Test-Path $csvFile )
{
$movies_a = @(Import-Csv $csvFile )
}

Get-ChildItem -LiteralPath “C:\Test” -Filter *.nfo -Force |
ForEach-Object {

$BaseName = $_.BaseName

if($movies_a -is [system.array]) {$Found = $movies_a | Where-Object {$_.OrgName -eq $BaseName}}

if($Found)
{

}else
{
$file_path_info=$_.FullName

$file = New-Object System.Xml.XmlDocument
$file.Load($file_path_info)
$file_data=$file.DocumentElement

$movie = New-Object PSObject
$movie | Add-Member -Type NoteProperty -Name OrgName -Value $.BaseName
$movie | Add-Member -Type NoteProperty -Name MovieName -Value $file_data.title
$movie | Add-Member -Type NoteProperty -Name mpaa -Value $file_data.mpaa
#$movie | Add-Member -Type NoteProperty -Name Extension -Value $
.Extension
$movies += $movie
$movie=$null

Write-Host “Movie not found, Lets addit”
}
}

$movie = $movies_a + $movies
$movie | Export-Csv C:\Test_Movies.csv -NoTypeInformation -Force