Compare & Add Content

Welcome Forums General PowerShell Q&A Compare & Add Content

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 7 years ago.

  • Author
  • #5365

    Points: 0
    Rank: Member

    by ChrisL at 2012-12-18 11:58:19

    Ok, so I have a script in which we are adding the contents of one file to another (g-c | a-c). I'm trying to modify it to compare the files line by line and only add what's in the second file that isn't contained in the first. My firs stab is the following, which filters out the differences between the files and then adds the result to a temp file. Repeat to pipe the results of the first Compare Object into a second one where I compare against the local file again.

    Compare-Object $(Get-Content "$sLocalStore\$sEnvConfig\file1.ini") $(Get-Content "$sProd\file2.ini") | Select-Object -ExpandProperty InputObject | Add-Content "$sProd\tmp.ini" -Force
    Compare-Object $(Get-Content "$sProd\tmp.ini") $(Get-Content "$sProd\File2.ini") | Select-Object -ExpandProperty InputObject | Add-Content "$sProd\File2.ini" -Force
    del "$sProd\tmp.ini" -Force

    The issue is there's still an entry that's being duplicated, even though it's even in the same position in each file. I'm also wondering is is there a way to pull this off without the temp file; I'm sure there is, I'm just not quite there yet. Any ideas would be appreciated, thanks 🙂

    by nohandle at 2012-12-18 14:59:48

    this should probably work:
    $copy = 'c:\temp\compare\copy.txt'
    $orig = 'c:\temp\compare\orig.txt'
    Compare-Object -ReferenceObject (get-Content $orig) -DifferenceObject (get-Content $copy) |
    where {$_.sideIndicator -eq "< ="} |
    select -ExpandProperty InputObject |
    Add-Content $copy

    by ChrisL at 2012-12-19 13:11:13

    Perfect! Thank you so much!

The topic ‘Compare & Add Content’ is closed to new replies.