I have tried many approaches to this and everyone so far seems to be line by line based rather than data based. This is fine if there was only a new add because 1 file will have 1 extra line. If I add that new line to the middle of the file it will consider it and everything after it new.
What I am trying to do is take a csv file that is generated by a separate program and compare it to the previous days file. I am looking for any new data, modified data or deleted data only. I do have a unique column in the csv named User ID if that helps. Here are a few examples of what I am looking for:
This would generate an empty csv3
csv1:
User ID,Name,Phone
1,John,1234567890
2,Jane,0987654321
50,Mary,1111111111
55,Mark,2222222222csv2:
User ID,Name,Phone
1,John,1234567890
2,Jane,0987654321
50,Mary,1111111111
55,Mark,2222222222csv3:
User ID,Name,Phone
This would generate an add
csv1:
User ID,Name,Phone
1,John,1234567890
2,Jane,0987654321
50,Mary,1111111111
55,Mark,2222222222csv2:
User ID,Name,Phone
1,John,1234567890
2,Jane,0987654321
50,Mary,1111111111csv3:
User ID,Name,Phone,Action(new column)
55,Mark,2222222222,add
This would generate a modification
csv1:
User ID,Name,Phone
1,John,1234567890
2,Jane,0987654321
50,Mary,1111111111
55,Mark,2222222222csv2:
User ID,Name,Phone
1,John,1234567890
2,Max,0987654321
50,Mary,1111111111csv3:
User ID,Name,Phone,Action(new column)
2,Max,0987654321,old
2,Jane,0987654321,new
This would generate a delete
csv1:
User ID,Name,Phone
1,John,1234567890
2,Jane,0987654321
50,Mary,1111111111
55,Mark,2222222222csv2:
User ID,Name,Phone
1,John,1234567890
2,Max,0987654321
3,Jim,3333333333
50,Mary,1111111111csv3:
User ID,Name,Phone,Action(new column)
3,Jim,3333333333,delete
Here is the script that gets me the closest it will allow me to compare but only if the new line is at the end of csv.
$path = “c:\ *.csv”
$files = Get-ChildItem -Path $path |
Sort-Object LastWriteTime -Descending$file1a = $files | Select-Object -First 1
$file2a = $files | Select-Object -First 1 -Skip 1$file1 = Import-CSV -Path $file1a
$file2 = Import-CSV -Path $file2aCompare-Object $file2 $file1 -PassThru | Export-CSV “c:\compare.csv” -NoTypeInformation