Ignore the previous posts, there were a few typos.
If the ';' separated file has no header row, split each line on each ';' and
capture this array in a variable, then create a collection of PSObjects and
assign the split values to their corresponding property.
If the ';' separated file has a header row, you can modify the ';' separated
content -- and set the modified content to a temp CSV file-- by splitting
each line on each ';', if any of the split strings matches a ',' double quote
it and join all split strings back with a ',' to rebuild the line. Then you
can import the CSV data into a variable and delete the temp CSV file.
Also, if your encoding is not Unicode --PowerShell's default-- pass it to
Get-Content and Set-Content statements through their -Encoding parameter.
Thanks for the reference Shay :)
# w/o header row
$csv = gc <path to CSV file> | # <-- fullpath to the ';' separated file
% {
$val = $_.split(';')
$obj = new-object psObject
$obj | add-member noteProperty Fruit $val[0] -p |
add-member noteProperty Qty $val[1] -p |
add-member noteProperty Date $val[2] -p
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# with header row, two versions
$csv | ft -a
# using [string]'s Join Static Method to rebuild each line
$tempCSV = [system.IO.Path]::getTempFileName()
$csvFile = <path to CSV file> # <-- full path to the ';' separated file
sc $tempCSV (gc $csvFile | % {[string]::join(',', ($_.split(';') |
% {if ($_ -match ',') {'"' + $_ + '"'} else {$_}}))})
# import the data
$csv = import-csv $tempCSV
# delete The temp CSV file
ri $tempCSV
$csv | ft -a
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# using PowerShell's $OFS --output field separator-- and
# string expansion on a subexpression to rebuild each line
$tempCSV = [system.IO.Path]::getTempFileName()
$csvFile = <path to CSV file> # <-- full path to the ';' separated file
& {$ofs = ','
sc $tempCSV (gc $csvFile | % {"$($_.split(';') |
% {if ($_ -match ',') {'"' + $_ + '"'} else {$_}})"})
}
# import the data
$csv = import-csv $tempCSV
# delete The temp CSV file
ri $tempCSV
$csv | ft -a
--
Kiron