On 10 Nov 2006 09:41:51 -0800,
mgrogan@gia.edu wrote:
>I truly wish import-csv could handle csv files without headers. Maybe
>it could come up with default field names or something...
Hi,
This function should do what you need, with a little adaptation
perhaps.
I have put comments in it so it should be fairly easy to see what it's
doing.
function global:MyimportCSV{
param($NoHeaderFile)
# Finds the number of columns in the file with no headers
$line = get-content $NoHeaderFile -totalCount 1
$array = $line.Split(',')
# Constructs a header line of the right length
$HeaderLine = ""
for($i = 0; $i -lt ($array.length)-1; $i++){
$HeaderLine += "Header($i),"
}
$HeaderLine += "Last Header"
# Gets the content of the file with no headers
$array = get-content $NoHeaderFile
# Outputs the header line to Temp.csv
set-content Temp.csv $HeaderLine
# Adds the content of the file with no headers to Temp.csv
foreach ($element in $array){
add-content Temp.csv $element
}
# Imports CSV as if the file with no headers had headers.
import-csv Temp.csv
}
I am sure that there are other ways of approaching this but the
preceding code does work for me.
Andrew Watt MVP