1

I am processing a .txt file, which needs to be repeated every 10 lines. I then want to turn this into a .csv file. However, some of the .txt file contains commas which I want to keep in a single cell in the resulting .csv file. I think my approach to exporting using StreamWriter may be a limiting factor here, how can I refactor this code so that the resulting csv ignores commas within values?

$input = "<file path>" 
$output = "<file path>"
$fileContents = Get-Content -Path $input
$writeToOutput = $true

if($writeToOutput){
    $sw = New-Object System.IO.StreamWriter $output
    $sw.WriteLine("Header1,Header2,...,HeaderN")
}

$ReadCount = 10
 
Get-Content -Path $input -ReadCount $readCount | ForEach-Object {

    $serviceNameStr = $_[0].ToString()
    $serviceName = $serviceNameStr.Substring($serviceNameStr.IndexOf(": ")+2)

    $descStr = $_[1].ToString()
    $desc= $descStr.Substring($descStr.IndexOf(": ")+2)
    # $desc = "this is used for a, b, and c"

    #... the remaining 8 lines to read/process

    if($writeToOutput){
        $sw.WriteLine("$serviceName,$desc,...,$lastVar")
        # $desc will not be contained to a single cell but rather "this is used for a" | " b" | " and c"
    }
}

if($writeToOutput){
    $sw.Close()
}

If I need to use Export-Csv to ignore the commas, how do I translate what I'm doing to use Export-Csv? How can I "stage" the 10 lines which need to be converted into a single .csv row?

1
  • Try : $sw.WriteLine(string.Join(",", @($serviceName,$desc,...,$lastVar))
    – jdweng
    Commented Sep 28, 2023 at 20:09

1 Answer 1

2

Ok figured it out, maybe it will help somebody coming from google. You have to "stage" the data in a PSCustomObject:

$input = "<file>" 
$output = "<file>"

$ReadCount = 10
 
# for every 10 lines in my input .txt, do something with that data
$data = Get-Content -Path $input -ReadCount $readCount | ForEach-Object {
    # every 10 lines are from $_[0] - $_[9]
    $serviceName = $_[0].ToString()
    $desc = $_[1].ToString()
    # ...
    $lastVar= $_[9].ToString()

    # this will be a single row in the resulting .csv
    # "key" = "value" results in "column name" = "cell content"
    [PSCustomObject]@{
        "Service Name" = $serviceName.Substring($serviceName.IndexOf(": ")+2)
        "Description" = $desc.Substring($desc.IndexOf(": ")+2)
        # ...
        "Last Var" = $lastVar.Substring($lastVar.IndexOf(": ")+2)
        # should be noted that in my case, the order in the .txt file was not the desired order of the columns in the output .csv
    }

}

$data | Export-Csv $output -NoTypeInformation

For some reason I can't use -Header on the Export-Csv as I've seen in some other examples, but that doesn't matter because it just uses the key in PSCustomObject to name the column, so I just have to set the column header title there.

1
  • Export-Csv exports from an object to a CSV file. The object members already have names. Export-Csv does not have a -Header parameter since it does not need one. The QUOTATION MARK characters are not needed on the object member names.
    – lit
    Commented Sep 28, 2023 at 21:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.