VBScript - Find specific term in between commas in .csv and append columns based on values found -


i trying append additional columns existing .csv file called original.csv, values appended should vary base on whether n or j part of value between 4th comma , 5th comma in original.csv (technically 5th column if file opened in excel). below codes wrote. don't work, it's reference only. in advance help.

rowcounter = 1 until objoriginal.atendofstream     stroriginal = objoriginal.readline     arroriginal = split(stroriginal, ",")     arrtype = split(arroriginal(5),",")     strtype = arrtype(1)      if istrue(instr(strtype,"n"))         stroriginal = objoriginal.readline & ",type n,usd"         objposition.writeline(stroriginal)     else         stroriginal = objoriginal.readline & ",type j,usd"         objposition.writeline(stroriginal)     end if     rowcounter = rowcounter + 1 loop 

the proper tool .csv files ado. need create new (text) table appending columns existing table sql statement like:

select s.*, 'whatever' & (iif([b]='j','-j', '-n')) [newcol] [dst.csv] [src.csv] s

and - in general - schema.ini file like

[src.csv] colnameheader=true format=delimited(;) col1=a integer col2=b char width 15  [dst.csv] colnameheader=true format=delimited(;) col1=a integer col2=b char width 15 col3=newcol char width 15 

to specify table structures unequivocally.

in code:

' absolute path .csv folder   dim ofs   : set ofs = createobject("scripting.filesystemobject")   dim sds   : sds     = ofs.getabsolutepathname("..\data\txt") ' connectionstring   dim scs   : scs     = join(array( _        "provider=microsoft.jet.oledb.4.0" _      , "data source=" & sds _      , "extended properties=""" & join(array( _             "text" _           , "hdr=yes" _      ), ";") & """" _   ), ";") ' database/connection   dim odb   : set odb = createobject("adodb.connection")   dim ssql    odb.open scs  ' show src   ssql = "select * [src.csv]"   wscript.echo ssql   wscript.echo odb.execute(ssql).getstring(adclipstring, , vbtab, vbcrlf, "null")  ' copy/append col new dst   if ofs.fileexists(ofs.buildpath(sds, "dst.csv")) ofs.deletefile ofs.buildpath(sds, "dst.csv")   ssql = "select s.*, 'whatever' & (iif([b]='j','-j', '-n')) [newcol] [dst.csv] [src.csv] s"   wscript.echo "--------------"   wscript.echo "exec:", ssql   odb.execute ssql  ' show dst   ssql = "select * [dst.csv]"   wscript.echo "--------------"   wscript.echo ssql   wscript.echo odb.execute(ssql).getstring(adclipstring, , vbtab, vbcrlf, "null") 

output:

select * [src.csv] 1       j 2       n 3       j 4       n  -------------- exec: select s.*, 'whatever' & (iif([b]='j','-j', '-n')) [newcol] [dst.csv] [src.csv] s -------------- select * [dst.csv] 1       j       whatever-j 2       n       whatever-n 3       j       whatever-j 4       n       whatever-n 

that way reduce risk of blunders like

  1. polluting code un-used (und un-usable) variables (rowcounter)
  2. trying split element array created split on same separator
  3. accessing next/wrong line .readline() twice

to zero


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -