powershell - Add header to multiple CSV files -
i'm looking way add header line multiple csv files. problem code below add empty row @ end of each file. don't understand why there empty line need delete lines.
$header="column1,column2,column3,column4,column5,column6" get-childitem .\ -recurse -filter *.csv| foreach-object { $header+"`r`n"+ (get-content $_.fullname | out-string) | set-content -path $_.fullname }
the canonical way import files specifying headers , re-export them:
$header = 'column1', 'column2', 'column3', 'column4', 'column5', 'column6' get-childitem .\ -recurse -filter '*.csv' | foreach-object { $file = $_.fullname (import-csv -path $file -header $header) | export-csv -path $file -notype }
export-csv
add double quotes around fields of csv, though. also, parsing data objects have performance impact. if don't want double quotes added or pressed performance solution suggested @petseral in comments question might better approach you:
$header = 'column1,column2,column3,column4,column5,column6' get-childitem .\ -recurse -filter '*.csv' | foreach-object { $file = $_.fullname @($header; get-content -path $file) | set-content -path $file }
Comments
Post a Comment