0

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

Alan
  • 5
  • 2
  • If your file contains only one line, `Get-Content` will read that output as a single string rather than an array containing one string. You can use `$header = @(Get-Content -Path $content)` to ensure you always work with an array. – AdminOfThings Dec 08 '20 at 16:58
  • Does this answer your question? [Array element types vary depending quantity of elements](https://stackoverflow.com/questions/42729703/array-element-types-vary-depending-quantity-of-elements) – iRon Dec 08 '20 at 17:47
  • the $header = @(Get-Content -Path $content) gives me the header that I want, but the previously empty files still don`t work. move "$content-temp" $content -Force it can`t find $content-temp. I think it might have to do with the skip 1, It has no where to skip to – Alan Dec 08 '20 at 20:19

1 Answers1

0

I found how to deal with it.

thing is, those empty html.csv wont be used, since after the skip they would be empty, and the code after always worked without them since I removed them earlier.

So what I did was this:

 Get-ChildItem $InPath -Filter '*HTML.csv' |  #seulement les html
Foreach-Object {
    $content = $_.FullName
    $header = @(Get-Content -Path $content) #gets the first line

    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
        try{
        move "$content-temp" $content -Force -ErrorAction Stop
        "header removed"
        }
        catch{
            Remove-Item -Path $content  #remove the empty csv since it won't have a use
            "empty"
        }
        #Write-Host $content
    }else{
    "no header to remove"
    }

first the $header = @(Get-Content -Path $content) helped because of what I said in the question. the key thing that I did was delete the empty html with a try catch. they are meant to be combined later on, but I would just add an empty file to another file and it would serve no purpose, so delete them.

as far as to why bother keeping them, its because some non-html were also removed before, while now its only the html. Like that, I can have the right amount of csv.

Hope this helps someone else with the same issue.

Alan
  • 5
  • 2