I am currently in the process of setting up a file upload for a work project. The uploaded file should contain all items with the necessary data and all variations (eg. size or length). The problem is, I can't export the variation data in the same file as the general item data so I figured since I already handle the processing and upload with a powershell script, I could merge those two files before uploading the result with powershell as well.
Here are the headers for both CSV-Files:
feed_file
Artikelnummer,EAN,Hersteller,Produktname,EKPreis,UVP,Produktbeschreibung,ProduktURL,BildURL,Bestand
variant_file
"VaterArtikelnummer";"Artikelnummer";"UVP";"Bestand";"Variationsname1";"Variationsname2";"Variationsname3";"Variationswertname1";"Variationswertname2";"Variationswertname3";"EKPreis"
"Artikelnummer" is the common key for both files as it contains unique numbers that only appear once per file and most (but not all) entries in the variant file are also present in the basic feed file.
Here is the code for the merging process (as it is currently not working, it is still contained in its own file so there is no other code that could be interfering with it):
# Set the base path for the script and all relevant files
$path = "C:/path/to/files/and/script"
# Get the most recent item and variation csv files and import them
$feed_file = Import-Csv (get-childitem -path "$path/files/article/*" -Include *.csv | Sort-Object CreationTime -Descending | Select-Object -first 1)
$variant_file = Import-Csv (get-childitem -path "$path/jtlExport/*" -Include Export_*.csv | Sort-Object CreationTime -Descending | Select-Object -first 1) -Delimiter ";"
<# Generate an output where the following is true:
- Any item that is not a variant (not in $variant_file) gets assigned the already present data from $feed_file and appropriate empty columns at the end
- Any item that is a variant gets assigned the full data set, including any information about the variant
#>
$output = Foreach($item in $feed_file){
$variant_file | Where-Object Artikelnummer -eq $item.Artikelnummer -ov result
If(-not $result){
[PSCustomObject]@{
Artikelnummer = $item.Artikelnummer
EAN = $item.EAN
Hersteller = $item.Hersteller
Produktname = $item.Produktname
EKPreis = $item.EKPreis
UVP = $item.UVP
Produktbeschreibung = $item.Produktbeschreibung
ProduktURL = $item.ProduktURL
BildURL = $item.BildURL
Bestand = $item.Bestand
VaterArtikelnummer = ""
Variationsname1 = ""
Variationsname2 = ""
Variationsname3 = ""
Variationswertname1 = ""
Variationswertname2 = ""
Variationswertname3 = ""
}
}
Else{
Foreach($variant in $variant_file){
If($item.Artikelnummer -eq $variant.Artikelnummer){
[PSCustomObject]@{
Artikelnummer = $item.Artikelnummer
EAN = $item.EAN
Hersteller = $item.Hersteller
Produktname = $item.Produktname
EKPreis = $item.EKPreis
UVP = $item.UVP
Produktbeschreibung = $item.Produktbeschreibung
ProduktURL = $item.ProduktURL
BildURL = $item.BildURL
Bestand = $item.Bestand
VaterArtikelnummer = $variant.VaterArtikelnummer
Variationsname1 = $variant.Variationsname1
Variationsname2 = $variant.Variationsname2
Variationsname3 = $variant.Variationsname3
Variationswertname1 = $variant.Variationswertname1
Variationswertname2 = $variant.Variationswertname2
Variationswertname3 = $variant.Variationswertname3
}
}
}
}
}
# Export the output as a csv file ready to upload
$output | Export-Csv -Path "$path/sample.csv" -Encoding UTF8 -NoTypeInformation
While the code in general can most likely be improved massively, I am just happy that it is doing what I want it to do after spending quite a while scouring the internet to find a way to merge the two files that worked in my case (though I'd still appreciate any tips and suggestions).
Though there is one issue which I simply can't seem to figure out, although the output is handled the way I want it, there is an issue that every item that is a variant, ends up twice in the final output, but one of the entries is missing most of the data. Here is an example:
"12345678_9",,,,"388,7","894,00",,,,"1","12345678","Größe","","","58","",""
"12345678_9","3210987654321","Hersteller","Produktname","388.70","894","Beschreibung","ProduktURL","BildURL","0","12345678","Größe","","","58","",""
As you can see, the first entry in the output file (for every single item which has variant data) is a stripped down version, missing most of the original data while still keeping some. My first guess was that the item somehow is listed twice but if I check the original two input files that were used, I can only find one entry in either file for any of the items, no doubles anywhere.
I also tried changing the first part of my code (for any items without variant data) to output a fixed string in certain columns and while this had an effect on any item that isn't a variant, the double still had empty columns in that place so I would assume it must have to do with the latter part handling the items that do have matching variant data but I just can't get behind what exactly is causing this issue.
I am using Powershell 5.1 on a Windows Server 2016, I hope someone can show me the probably obvious answer to this issue. Thank you all in advance for your help!
Best regards
-Kevin
Edit: The solution provided by Hansson0728 worked perfectly, with the minor adjustment of the type "AllInLeft" but it also seems to be a lot faster than my bulky manual method.