-1

I have been trying to code the powershell script to compare 2 csv files for some time. Found a scratch of the script in this forum, but it does not work the way I want. We are exporting our SKU's every week and I need to compare if there are any new SKUS in the new file.

I tried to run the script and it works, but only for a small range of rows. When I tried to compare the real file with 5k rows, it unfortunately failed. The output had almost 3k rows which is nonsense because we didnt add that many rows in one week.

Clear-Host

$csv1 = Import-Csv "SKU_export_2019-04-01.csv" # NEW FILE
$csv2 = Import-Csv "SKU_export_2019-03-25.csv" # OLD FILE

$end     = $csv1.Count
$count   = 0
$diffobj = @()

do{
    if($csv1.SKU[$count] -ne $csv2.SKU[$count]){
       $diffobj += $csv1[$count]
    }

    $count++
}until($count -eq $end)

$diffobj | export-csv C:\xampp\htdocs\diff\difference.csv -NoTypeInformation

The expected result should be an output with all the differences in the new file.

Sigitas
  • 21
  • 6
  • 1
    Possible duplicate of [Compare 2 CSV files and write all differences](https://stackoverflow.com/questions/36220782/compare-2-csv-files-and-write-all-differences) – Theo Apr 04 '19 at 12:26
  • Possible duplicate of [Powershell File Compare](https://stackoverflow.com/questions/3490810/powershell-file-compare) – Fourat Apr 04 '19 at 12:28

2 Answers2

0

Why not using Compare-Object?

$diff = Compare-Object -ReferenceObject $csv1 -DifferenceObject $csv2

Roberto
  • 567
  • 1
  • 3
  • 10
  • Just tried to execute this. This time I got only 60 rows with indicator <= But after rechecking both files, I saw that both of them have all the SKU's. – Sigitas Apr 04 '19 at 13:10
0

You script compares the csv files per order index, if the order changed you'll have a missmatch.

As suggested by others, better use Compare-Object and explicitly use the SKU field.

To get only new SKUs select by SideIndicator <= and to include any other fields/properties present in the csv also use the -PassThru parameter.

To remove the inevitably added SideIndicator use a Select-Object with -ExcludeProperty

Added calculation of this weeks Monday:

## Q:\Test\2019\04\04\SO_55515891.ps1
$MondayThisWeek = (Get-Date).Date.AddDays(-([int](Get-date).DayOfWeek)+1)
$MondayPrevWeek = $MondayThisWeek.AddDays(-7)

Clear-Host

$SKUthis = Import-Csv ("SKU_export_{0:yyyy\-MM\-dd}.csv" -f $MondayThisWeek) # NEW FILE
$SKUprev = Import-Csv ("SKU_export_{0:yyyy\-MM\-dd}.csv" -f $MondayPrevWeek) # OLD FILE

$SKUNew = Compare-Object -Ref $SKUthis -Diff $SKUprev -Property SKU -PassThru |
    Where-Object SideIndicator -eq '<=' | 
       Select-Object -Property * -ExcludeProperty SideIndicator

$SKUNew | Out-Gridview
$SKUNew | Export-Csv 'C:\xampp\htdocs\diff\difference.csv' -NoTypeInformation
  • I appreciate your help. Thank you very much. – Sigitas Apr 05 '19 at 08:37
  • By the way, is it possible to add a log file to see if the execution was completed with the date? I'm planning to add this script to Auto task scheduler – Sigitas Apr 05 '19 at 08:41
  • You could add a calculated property with the date to rhe select `@{n='NewIn';e={$MondayThisWeek}}` –  Apr 05 '19 at 08:57