1 .. $Count | ForEach-Object {
$i = $_.ToString($Length)
$Offset = $BatchSize * ($_ - 1)
$outputFile = $ParentDirectory + "\" + $strBaseName + "-" + $i + $strExtension
If($_ -eq 1) {
$objFile | Select-Object -First $BatchSize | Export-Csv $outputFile -NoTypeInformation -Encoding UTF8
} Else {
$objFile | Select-Object -First $BatchSize -Skip $Offset | Export-Csv $outputFile -NoTypeInformation -Encoding UTF8
}
}
I have a .txt with a comma on row 3 below. My code is stripping anything after the comma as seen below. how do I fix it? My file is pipe delimited.
Original file contains
|Header1|Header2|Header3|Header4|
|320|A1| |0900|
|320|A2|This, comma is needed|0900|
|320|A3| |0700|
|320|A4|f2|0900|
|320|A5| |0700|
|320|L2|c6|0900|
After splitting into 2 files -notice the missing text after "this,"
file1
|Header1|Header2|Header3|Header4|
|320|A1| |0900|
|320|A2|This,
|320|A3| |0700|
file2
|Header1|Header2|Header3|Header4|
|320|A4|f2|0900|
|320|A5| |0700|
|320|L2|c6|0900|
Please advise. Thanks
I tried to use delimiter and replace commands. Didn't work
It looks like when you imported your delimited file into
$objFile, you forgot to pass-Delimiter '|'to theImport-Csvcall, which would not interpret your|-separated file properly, given thatImport-Csv- as well asExport-Csv- default to,as the separator.Thus, the solution is to use
-Delimiter '|'in both yourImport-CsvandExport-Csvcalls.As for what you tried:
Here's a minimal example that demonstrates the problem with omitting
-Delimiter '|', using the in-memory CSV processing cmdlets,ConvertFrom-CsvandConvertTo-Csv:Output (note the missing
, and morepart, and how the output lines as a whole are double-quoted):Since header row
Field1|Field2contains no,it became a single property in the resulting objects, literally namedField1|Field2.Since the data row happened to contain
,it was parsed as two fields, and since there is only one column, the extra field was simply discarded.