I can change the column order of text in a file:
001 AALTON Alan 25 Every Street
002 BROWN James 101 Browns Road
003 CAMPBELL Colin 57 Camp Avenue
to have the first names listed before surnames using the following:
$Data = Import-CSV C:\test\test6\sqlInsertList.txt -Delimiter ' ' -Header "Col1","Col2","Col3","Col4","Col5","Col6"
# attempt to output with Surname/FirstName (columns 2 & 3) reversed
[array]$fileStore = $Data | Select-Object Col1,Col3,Col2,Col4,Col5,Col6 | Export-CSV C:\test\test6\newSqlInsertList.txt -append
The output being:
"Col1","Col3","Col2","Col4","Col5","Col6"
"001","Alan","AALTON","25","Every","Street"
"002","James","BROWN","101","Browns","Road"
"003","Colin","CAMPBELL","57","Camp","Avenue"
But if I'm to process this output to a string format I am stuck trying to expand each Colx property like:
$Data = Import-CSV C:\test\test6\sqlInsertList.txt -Delimiter ' ' -Header "Col1","Col2","Col3","Col4","Col5","Col6"
# attempt to output with Surname/FirstName (columns 2 & 3) reversed
[array]$fileStore = $Data | Select-Object Col1,Col3,Col2,Col4,Col5,Col6
# first attempt worked
$test = $fileStore[1] | Select-Object -ExpandProperty Col1
$test += ' '
$test += $fileStore[1] | Select-Object -ExpandProperty Col3
$test += ' '
$test += $fileStore[1] | Select-Object -ExpandProperty Col2
$test += ' '
$test += $fileStore[1] | Select-Object -ExpandProperty Col4
$test += ' '
$test += $fileStore[1] | Select-Object -ExpandProperty Col5
$test += ' '
$test += $fileStore[1] | Select-Object -ExpandProperty Col6
$test
To output:
002 James BROWN 101 Browns Road
I can't store the objects Colx in an array to loop through them. So the solution I have so far is cumbersome and simply doesn't seem right.
The solutions seem to be:
- Somehow intercept the
Export-CSV C:\test\test6\newSqlInsertList.txt -appendto remove double quotes & commas - Or find a way to expand all the column properties in a better way than I have shown
I would appreciate if anyone has a suggestion.