I have a code that converts an xml into a csv, however some xml are empty, my desired output is then to only have the header in those files. to do that, I want to add to the first line my header.
due to the format of the xml I have to replace the header of every file
the bit doing it is here:
$header = Get-Content -Path $OutPath'\'$content'.csv'
Try {
$header[0] = $NomColonnes
$header | Set-Content -Path $OutPath'\'$content'.csv' -Encoding:UTF8 #remplace la premiere ligne
"Operation Finished"
}
Catch{
#Remove-Item –path $OutPath'\'$content'.csv' #si vide detrui le fichier (xml vide)
"xml vide"
Add-Content -Path $OutPath'\'$content'.csv' -Value $NomColonnes"‘n" -Encoding:UTF8 #header avec un csv vide
#$NomColonnes | Out-File $OutPath'\'$content'.csv' -Encoding utf8
}
the "try" works when there is at least one line, if not it gave me and error, so the role of the catch is just to add the missing header ($nomcolonnes in the case) to an empty file
however, while in the files it looks normal.
later I need to remove the header of certain files to be able to merge them which I do here:
$InPath = Read-Host -Prompt 'Enter a the path to the csv'
$NomColonnes = '"Context","Proprety_Name","Design Value","Translation Value","","","",""'
Get-ChildItem $InPath -Filter '*HTML.csv' | #seulement les html
Foreach-Object {
$content = $_.FullName
#Write-Host $content
$header = Get-Content -Path $content
#Write-Host $header[0] "1st line"
#Write-Host $NomColonnes "nomcol"
if ($header[0] -eq $NomColonnes) #if first line = header then do the operation, otherwise don`t
{
get-content $content |
select -Skip 1 |
set-content "$content-temp" #recree le fichier sans la premiere ligne
move "$content-temp" $content -Force
#Write-Host $content
}else{
$header[0]
$header[1]
$header[2]
"no header to remove"
}
it select the ones tagged with html to remove their header it looks for header[0] and most will work but when it encounters one of the previously empty files, its like it reads columns.
the output of the:
$header[0]
$header[1]
$header[2]
is:
"
C
o
going further into it would spell out "Content,..."
I have no idea why it does this, I think the main culprit would be when I add the line. But I've tried everything I could think of.
ps: the line:
#Remove-Item –path $OutPath'\'$content'.csv'
was for another version that did away with the empty files