vb.net - CSV duplicate field names -
i have csv file extract core system. recent development change system has made there csv file data contain duplicate column/field names.
so fields appear more once.
- creator first name
- creator last name
- creator salary number
- revenue
- contact
- sales team
- channel
this data upload via ssis/dts package. package won't run or work duplicate field names. need remove or rename duplicate fields.
so thinking of creating c# or vb script renames duplicate field names post fix of 1 when counts field name of more 1.
is there easy way of doing through microsoft.visualbasic.fileio.textfieldparser or similiar? not sure if has encountered similiar issue.
i have added script task runs visual basic 2008 code (below) wip;
public sub main() 'check if file exist dim filename string = dts.variables.item("filename").value if not system.io.file.exists(filename) exit sub end if dim csvfilearray(1, 1) string dim newcsvfilearray(1, 1) string dim streamreader streamreader = file.opentext(filename) dim strlines() string strlines = streamreader.readtoend().split(environment.newline) ' redimension array. dim num_rows long dim num_cols long 'dim counter integer = 0 '' >> idea add array 3rd dimension can count each field dim fieldcounter integer = 0 dim strline() string num_rows = ubound(strlines) strline = strlines(0).split(",") num_cols = ubound(strline) redim csvfilearray(num_rows, num_cols) redim newcsvfilearray(num_rows, num_cols) ' copy data array. dim fields integer dim rows integer rows = 0 num_rows - 1 strline = strlines(rows).split(",") fields = 0 num_cols - 1 csvfilearray(rows, fields) = strline(fields) next next dim currentfield string = "" dim comparrisionfield string = "" dim newrows integer = 0 dim newfields integer = 0 rows = 0 ' compare current array if match in new array rows = 0 num_rows - 1 fields = 0 num_cols - 1 currentfield = csvfilearray(rows, fields) if rows = 0 ' if dealing fields i.e row=0 newfields = 0 num_cols - 1 comparrisionfield = newcsvfilearray(newrows, newfields) if string.isnullorempty(currentfield) else if currentfield.equals(comparrisionfield) if currentfield <> "" fieldcounter = fieldcounter + 1 ' if have added column, append number if fieldcounter >= 1 currentfield = currentfield + " " + cstr(fieldcounter) end if end if end if end if next else ' means dealing rows i/e not row = 0 currentfield = currentfield end if newrows = 0 newfields = 0 fieldcounter = 0 ' save currentfield in same layout initial file newcsvfilearray(rows, fields) = currentfield next next ' amend duplicate field names dim sw new system.io.streamwriter(left(filename, len(filename) - len(right(filename, 4))) + "_amended.csv") rows = 0 dim currentline string = "" rows = 0 num_rows - 1 fields = 0 num_cols - 1 ' save data filesystem if fields = 0 currentline = newcsvfilearray(rows, fields) else currentline = currentline + "," + newcsvfilearray(rows, fields) end if next if currentline <> "" sw.writeline(currentline.replace(vbcr, "").replace(vblf, "")) end if next sw.close() dts.taskresult = scriptresults.success end sub
Comments
Post a Comment