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

Welcome Forums General PowerShell Q&A Import CSV file into Arrary and add new value if the data doesnt exist in csv

Viewing 2 reply threads
  • Author
    Posts
    • #276906
      Participant
      Topics: 15
      Replies: 20
      Points: 158
      Rank: Participant

      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

    • #276942
      Participant
      Topics: 12
      Replies: 547
      Points: 1,345
      Helping Hand
      Rank: Community Hero
    • #276990
      Participant
      Topics: 15
      Replies: 20
      Points: 158
      Rank: Participant

      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

Viewing 2 reply threads
  • You must be logged in to reply to this topic.