Anand, thanks for the quick response. The values won't always be a perfect count...take the address for instance. It will be a different length most times.
Main Topics
Browse All TopicsI have a service supplier that sends us data on a disk (at least for now) in a csv format. Here is an example of that data:
"N","MYWAY","6439116","16"
"N","MYWAY","6439116","16"
What I would like to do with this data is to read it from the disk and write it to a SQL table. I have the following code to get the file from the user's machine.
<form name="frm_UPLOAD" action="ACTION_LOAD_DAILY_
<input name="filename" size="40" type="file"><br>
<input type="Submit" value="Upload">
</form>
Then here is where I get stuck...this is my action page.
<cfoutput>
<cfset #loop_count# = 1>
<cffile ACTION="READ" FILE="#form.filename#" VARIABLE="DISK_DATA">
<cfloop Index="LOOP_COUNTER" list="#DISK_DATA#" delimiters=""",""">
This is where I would insert
</cfloop>
</cfoutput>
I'm having trouble with the null values and determining where the record ends because it isn't fixed width. Also, the null values aren't being counted in the loop so counting characters doesn't seem to work.
Perhaps I'm doing it all wrong...I guess I'm just looking to see if there is a simple CF solution. Example code would be very beneficial.
Thanks!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I think Anand is right
you need to count the fields not the length of a field, fields or a record
Just loop over the variable and reset the counter if the end of a record is reached
<cfset fieldcounter = 0>
<cfloop Index="LOOP" list="#DATA#" delimiters=",">
.
.
.
<cfif fieldcounter IS 232> <!--- 232 fields end of record??? --->
<cfset fieldcounter = 0> <!--- reset counter --->
<cfelse>
<cfset fieldcounter = fieldcounter + 1>
</cfif>
.
.
.
</cfloop>
first replace consecutive ,, by ,NULL, like:
<cffile ACTION="READ" FILE="#form.filename#" VARIABLE="DISK_DATA">
<cfset DISK_DATA=replace(DISK_DAT
you need to loop over 2 lists like so:
<cfloop list="#DISK_DATA#" index="row" delimiters="#chr(13)##chr(
<cfloop list="#row#" index="col">
#col# is the column value in the current row
</cfloop>
</cfloop>
if you wanted to insert, you might not loop through the row, you could just go like:
<cfloop list="#DISK_DATA#" index="row" delimiters="#chr(13)##chr(
<cfquery name="insertdata" datasource="yourDSN">
insert into tablename (col1, col2, col3...)
values (#preservesinglequotes(rep
</cfquery>
</cfloop>
where \' is supposed to escape any single quotes already in the data..
just try this code.it will replace ,, with ,~,
that means if there is balnk in between comma, it will replace blank with ~.
once it is being converted i think it will be easy for u to insert into table.
<cfoutput>
<cfset thisrow = '"N","MYWAY","6439116","16
"N","MYWAY","6439116","16"
'>
<cfset mylist = "">
<cfloop index="i" from="1" to="#len(thisrow)#">
<cfset mylist = mylist & #mid(thisrow,i,1)#>
<cfif #mid(thisrow,i,1)# is ",">
<cfif (len(thisrow) GTE i+1) and mid(thisrow,i+1,1) is ",">
<cfset mylist = mylist & "~">
</cfif>
</cfif>
</cfloop>
#mylist#
<vfset thisrow = mylist>
</cfoutput>
You do have one other option.
Sql Server has a service called DTS. Which means import/export data from all kinds of sources, including csv or text files.
Depending on the complexity of the data, and the difference in table structures.
You could even run this as a scheduled job in sql server, once you got the data and incoming files as a regular predictable event.
With all the power Cold Fusion has, it sometimes helps for performance and stability to migrate stuff to the database, when it has the power to do so.
first replace consecutive ,, by ,NULL, like:
<cffile ACTION="READ" FILE="#form.filename#" VARIABLE="DISK_DATA">
<cfset DISK_DATA=replace(DISK_DAT
also replace the last one (which is not ,,)
<cfset DISK_DATA=replace(DISK_DAT
you need to loop over 2 lists like so:
<cfloop list="#DISK_DATA#" index="row" delimiters="#chr(13)##chr(
<cfloop list="#row#" index="col">
#col# is the column value in the current row
</cfloop>
</cfloop>
if you wanted to insert, you might not loop through the row, you could just go like:
<cfloop list="#DISK_DATA#" index="row" delimiters="#chr(13)##chr(
<cfquery name="insertdata" datasource="yourDSN">
insert into tablename (col1, col2, col3...)
values (#preservesinglequotes(rep
</cfquery>
</cfloop>
where \' is supposed to escape any single quotes already in the data..
Business Accounts
Answer for Membership
by: anandkpPosted on 2003-04-22 at 08:48:12ID: 8374147
read the file in a variable & then
repalce all consecutive ",," list with ",NULL,"
run a chk on the values that u wanna insert [ie have a demarkataion that tell u that these values are for the first record & then the next record starts]
(if u dont know abt the where the record ends then count the no of values u need to use - remember since u ahve replaced the ",," with ",NULL" the count shld be perfect all the time & shld help u with runniing a loop & inserting them properly in DB)
then run a loop on this & start inserting these values in DB till u reach the end of the list !
there ur DONE !!!
K'Rgds
Anand