' This program will parse a TAB Delimited file
$main
def version$
string:version$="1.0"

autodefine "off"

def rtn:string
def File_Path:string
def File_Name_I, File_Name_O:string
def i, j, k, l, max:int
def ln[5500]:istring
def c, q, t, fldn, tmp:string
def I_file,O_file:file
def fs[400]:string
def cl[20]:int
def v:double

cl[0] = 32,34,35,83,84,85,88,92,93,94,97,99,100,102,103,105,187,188,189

' = ,
c = ","
' = tab
t = chr$(9)
' = "
q = "\""

File_Path = "C:\\TEMP\\NewFiles\\DHL Invoices\\"

File_Name_I = File_Path + "DHLInvoice16.CSV"
File_Name_O = File_Path + "DHLInv.CSV"

openconsole

if (openfile(I_file,File_Name_I,"R") <> 0)
	print "File: ", File_Name_I, " Not able to Open Input"
	goto Aborted
endif

if (openfile(O_file,File_Name_O,"W") <> 0)
	print "File: ", File_Name_O, " Not able to Open Output"
	goto Aborted
endif

while (read(I_file,ln) = 0)
	gosub Get_Field_Values
	ln = ""
	for l = 0 to 18
		ln += fs[cl[l]] + c
	next l
	v = (val(fs[187]) * val(fs[188]) * val(fs[189]))/194
	ln += ltrim$(str$(int(v))) + "," + q + date$("yyyy-MM-dd") + q
	write O_file, ln
	for i = 0 to 299
		fs[i] = ""
	next i
endwhile

closefile I_file
closefile O_file

goto Final

label Aborted
print "---Program Aborted---"
print " "
input "Press Return to end: ",rtn

label Final
'input rtn
closeconsole
END

'___________________
sub Get_Field_Values
'-------------------
j = 0
l = instr(ln,t)
while (l>0)
	fldn = mid$(ln,1,l-1)
	j += 1
	fs[j] = fldn
	ln = mid$(ln,l+1)
	l = instr(ln,t)
endwhile
j += 1
fs[j] = ln
fldn = ""
return
endsub