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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading data from a text file

Status
Not open for further replies.

Guest_imported

New member
Joined
Jan 1, 1970
Messages
0

How do I obtain specific data from a text file line? For example, I have to create a file with specific data in another text file. The data I need is always in characters 20-29 of a line in the file. What I need to do is just retrieve this data to create this other file. I was using CSV files in the past but now my new customers only provide me text files.

Anyone know what I need to to extract data from a specific spot in a text file? I know I can do this in VB, but can I do it in Coldfusion?

Thanks for any help.
 
Assuming the txt file is on the local server or network, you would use the <CFFILE> tag for all ColdFusion file functions (read, write, append, delete, etc).

Then you the #Mid(string, start, count)# function to get the data. For example:

[COLOR=666666]<!--- Read txt file into variable vText --->[/color]
<cffile action=&quot;READ&quot;
file=&quot;c:\file.txt&quot;
variable=&quot;vText&quot;>


[COLOR=666666]<!--- Write Lines 20 to 29 to newFile.txt --->[/color]
<cffile action=&quot;WRITE&quot;
file=&quot;c:\newFile.txt&quot;
output=&quot;#Mid(vText, 20, 10)#&quot;>
- tleish
 

Since this was such a help yesterday....

Whats the easiest way to loop through the lines in a text file. I'm trying to use the <CFLOOP list=&quot;#DATAFILE#&quot; index=&quot;line&quot;> function with my text file as my list variable. This runs once fine but doesn't continue looping through, perhaps due to my lack of delimiters? If so what should I use for my delimiter?

This is basically what my could looks like right now:

<CFLOOP list=&quot;#DATAFILE#&quot; index=&quot;line&quot;>
<CFSET text9=mid(line,21,9)>
<CFFILE action=&quot;APPEND&quot; addnewline=&quot;Yes&quot; file=&quot;d:\Upload\#File.serverfilename#.txt&quot; output=&quot;#text9#&quot;>
</CFLOOP>

Is there another way or function out there that would best work for me? I understand I may be way off base for the optimal solution here. Thanks for any help.


 

Actually I just found the answer I needed.....

BY adding &quot; delimiters=&quot;#chr(10)##chr(13)#&quot; &quot; I was able to solve my problem.

If this is not a good way to do this, I appreciate any further feedback.

Thanks again.

 
That's the best method to loop through lines that I'm aware of. - tleish
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top