Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read a text file line by line and assign them to variables?

Status
Not open for further replies.

accuransx

Programmer
Sep 23, 2000
62
0
0
MY
I have a csv file. How to read the data line by line, trim double quotes and commas, and assign data to variables?
 
Why my CF Server cannot recognize my CFHTTP tag as a query? I got this error msg:

Error Diagnostic Information
QUERY

The QUERY attribute of the tag does not specify the name of an available query

The error occurred while processing an element with a general identifier of (CFOUTPUT), occupying document position (9:1) to (9:29).


Date/Time: 10/09/01 16:56:18
Browser: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)
Remote Address: 192.168.0.1
HTTP Referer:
 
This is my code:

---start-of-page---

Upload

<CFHTTP url=&quot; method=&quot;get&quot; delimiter=&quot;,&quot; textqualifier=&quot;&quot;&quot;&quot; columns=&quot;a,b&quot; name=&quot;UploadData&quot;>

<CFOUTPUT>
No. of Records : #UploadData.RecordCount# records
</cfoutput>

<cfoutput query=&quot;UploadData&quot;>
<BR>#a# #b#<br>
</cfoutput>

</cfhttp>



<!---

I've tried this code before, but failed to chop the data.
I was trying to read from a text file and copy the data to my Access DB

<cffile action=&quot;READ&quot; file=&quot;D:\Applications\Kuittho\Data\data001.txt&quot; variable=&quot;FNama&quot;>
<cfoutput>#LTRIM(FNama)#</cfoutput>

--->

---end-of-page---
 
The closing </CFHTTP> is in the wrong place.

Try:

<CFHTTP URL=&quot; METHOD=&quot;get&quot; DELIMITER=&quot;,&quot;
TEXTQUALIFIER=&quot;&quot;&quot;&quot; COLUMNS=&quot;a,b&quot;
NAME=&quot;UploadData&quot;/>
[COLOR=666666]<!--- /> is the same as </CFHTTP> --->[/color]

<CFOUTPUT>
No. of Records : #UploadData.RecordCount# records
</CFOUTPUT>

<CFOUTPUT QUERY=&quot;UploadData&quot;>
[COLOR=000080]<BR>[/color]#a# #b#[COLOR=000080]<BR>[/color]
</CFOUTPUT>

[COLOR=666666]<!--- </CFHTTP> This is in the wrong place --->[/color] - tleish
 
Still cannot recognize my UploadData query. Err msg:


Error Occurred While Processing Request
Error Diagnostic Information

An error occurred while evaluating the expression:


#UploadData.RecordCount#



Error near line 8, column 19.
--------------------------------------------------------------------------------

Error resolving parameter UPLOADDATA.RECORDCOUNT


ColdFusion was unable to determine the value of the parameter. This problem is very likely due to the fact that either:

You have misspelled the parameter name, or
You have not specified a QUERY attribute for a CFOUTPUT, CFMAIL, or CFTABLE tag.


The error occurred while processing an element with a general identifier of (#UploadData.RecordCount#), occupying document position (8:18) to (8:41).


 
Can you post the first 5 or 10 lines of the txt file you are querying? - tleish
 
This is my dummy data from data001.txt:

&quot;AAA&quot;,&quot;Sudah Lulus&quot;
&quot;ABC&quot;,&quot;Tidak Lulus&quot;
&quot;DEF&quot;,&quot;Sudah Lulus&quot;
&quot;GHI&quot;,&quot;Tidak Lulus&quot;
&quot;KLM&quot;,&quot;Sudah Lulus&quot;

 
Using:
<CFHTTP URL=&quot;
Code:
[URL unfurl="true"]http://badang:301/data/data001.txt[/URL]
&quot;

METHOD=&quot;get&quot; DELIMITER=&quot;,&quot;
TEXTQUALIFIER=&quot;&quot;&quot;&quot; COLUMNS=&quot;a,b&quot;
NAME=&quot;UploadData&quot;/>


<CFOUTPUT>
No. of Records : #UploadData.RecordCount# records
</CFOUTPUT>

<CFOUTPUT QUERY=&quot;UploadData&quot;>
[COLOR=000080]<BR>[/color]#a# #b#[COLOR=000080]<BR>[/color]
</CFOUTPUT>

With a txt file:

[tt]dummy text
&quot;AAA&quot;,&quot;Sudah Lulus&quot;
&quot;ABC&quot;,&quot;Tidak Lulus&quot;
&quot;DEF&quot;,&quot;Sudah Lulus&quot;
&quot;GHI&quot;,&quot;Tidak Lulus&quot;
&quot;KLM&quot;,&quot;Sudah Lulus&quot;[tt]


Worked fine for me, it printed:

No. of Records : 4 records
ABC Tidak Lulus

DEF Sudah Lulus

GHI Tidak Lulus

KLM Sudah Lulus - tleish
 
I've had a very similar problem. I was uploading the CSV file using a web page. Using your test data, and assuming you called the file upload element of the form &quot;datafile&quot; (i.e. <INPUT TYPE=&quot;FILE&quot; NAME=&quot;datafile&quot;> ) you could use the following code:
Code:
<CFLOOP INDEX=&quot;Line&quot; LIST=&quot;#Form.datafile#&quot; 
DELIMITERS=&quot;#Chr(10)##Chr(13)#&quot;>

  <CFSET IDcode = ListGetAt(Line,1)>
  <CFSET Name = ListGetAt(Line,2)>

</CFLOOP>
Setting the delimiters to Chr(10) and Chr(13) catches end-of-line characters. Of course, this still leaves the double-quotes. To remove them you could do something like:
Code:
<CFSET IDcode = Replace(IDcode,'&quot;','','ALL')>
Alternatively you could combine the ListGetAt and the Replace to give you:
Code:
<CFSET IDcode = Replace(ListGetAt(Line,1),'&quot;','','ALL')>
Hope this helps.

Scott.
 
Try this conventional way

<cfset crlf = chr(13) & chr (10)>
<cffile action=&quot;READ&quot; file=&quot;data001.txt&quot; variable=&quot;filecontents&quot;>
<CFOUTPUT>
No. of Records : #listlen(filecontents,crlf)# records
</CFOUTPUT>
<cfloop list=&quot;#filecontents#&quot; delimiters=&quot;#crlf#&quot; index=&quot;logline&quot;>
<BR>#listgetat(logline,1,&quot;,&quot;)# #listgetat(logline,2,&quot;,&quot;)#<BR>
</cfloop>
 
I get this respond:

Upload No. of Records : 6 records
&quot;AAA&quot; &quot;Sudah Lulus&quot;

&quot;ABC&quot; &quot;Tidak Lulus&quot;

&quot;DEF&quot; &quot;Sudah Lulus&quot;

&quot;GHI&quot; &quot;Tidak Lulus&quot;

&quot;KLM&quot; &quot;Sudah Lulus&quot;








--------------------------------------------------------------------------------

Error Occurred While Processing Request
Error Diagnostic Information

An error occurred while evaluating the expression:


#listgetat(logline,2,&quot;,&quot;)#



Error near line 10, column 33.
--------------------------------------------------------------------------------

In function ListGetAt(list, index [, delimiters]) the value of index, which is 2, is not a valid index for the list given as a the first argument (this list has 1 elements). Valid indexes are in the range 1 through the number of elements in the list


The error occurred while processing an element with a general identifier of (#listgetat(logline,2,&quot;,&quot;)#), occupying document position (10:32) to (10:57).


I think my last loop/termination point problem. If it works i need to assign this to variable and then upload the stored value in my SQL tables. Any easier way?
 
Sorry,
The loop section should be
<cfloop list=&quot;#filecontents#&quot; delimiters=&quot;#crlf#&quot; index=&quot;logline&quot;>
<BR>#listgetat(filecontents,1,&quot;,&quot;)# #listgetat(filecontents,2,&quot;,&quot;)#<BR>
</cfloop>

The above loop will display the result, if you want to insert into a table/database, use cfquery inside the loop.

<cfloop list=&quot;#filecontents#&quot; delimiters=&quot;#crlf#&quot; index=&quot;logline&quot;>
<cfquery name=&quot;namaquery&quot; datasource=&quot;namadatasource&quot;>
INSERT INTO namatable (namafiled1, namafield2)
VALUES (#listgetat(filecontents,1,&quot;,&quot;)#,#listgetat(filecontents,2,&quot;,&quot;)#)
</cfquery>
</cfloop>
 
I moved </cfoutput> to the right place, so my new code:

<cfset crlf = chr(13) & chr (10)>
<cffile action=&quot;READ&quot; file=&quot;D:\Applications\Kuittho\Data\data001.txt&quot; variable=&quot;filecontents&quot;>
<CFOUTPUT>
No. of Records : #listlen(filecontents,crlf)# records
<cfloop list=&quot;#filecontents#&quot; delimiters=&quot;#crlf#&quot; index=&quot;logline&quot;>
<BR>#listgetat(filecontents,1,&quot;,&quot;)# #listgetat(filecontents,2,&quot;,&quot;)#<BR>
</cfloop>
</CFOUTPUT>

but i only have the wrong result like this:

No. of Records : 7 records
&quot;AAA&quot; &quot;Sudah Lulus&quot; &quot;ABC&quot;

&quot;AAA&quot; &quot;Sudah Lulus&quot; &quot;ABC&quot;

&quot;AAA&quot; &quot;Sudah Lulus&quot; &quot;ABC&quot;

&quot;AAA&quot; &quot;Sudah Lulus&quot; &quot;ABC&quot;

&quot;AAA&quot; &quot;Sudah Lulus&quot; &quot;ABC&quot;

&quot;AAA&quot; &quot;Sudah Lulus&quot; &quot;ABC&quot;

&quot;AAA&quot; &quot;Sudah Lulus&quot; &quot;ABC&quot;


 
Do anybody else know how to read a csv file and upload the file data into my ODBC Access db? Or if anybody could help me for my CFHTTP to work and my problem will solved, please i'm really stuck on this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top