Welcome › Forums › General PowerShell Q&A › Import CSV file into Arrary and add new value if the data doesnt exist in csv
- This topic has 2 replies, 2 voices, and was last updated 1 month, 2 weeks ago by
Participant.
-
AuthorPosts
-
-
December 6, 2020 at 9:14 am #276906
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 | UI 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
-
December 6, 2020 at 2:07 pm #276942PowerShell12345678910111213141516171819202122232425$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 $CSVFilePathforeach ($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.BaseNameMovieName = $file_data.titlempaa = $file_data.mpaa})} else {Write-Output 'Already in csv'}}$movie_a | Export-Csv ($CSVFilePath -replace '.csv','-Updated.csv') -NoTypeInformation -Force
-
December 6, 2020 at 7:35 pm #276990
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=$nullWrite-Host "Movie not found, Lets addit"
}
}$movie = $movies_a + $movies
$movie | Export-Csv C:\Test\_Movies.csv -NoTypeInformation -Force
-
-
AuthorPosts
- You must be logged in to reply to this topic.