From: DocSun on
I have a data file that looks like this:
70-80-90 File status

File Number File Name Type Date
00001 myfile.txt text 09222006
00029 myfile1.txt text 09222006
00870 myfile2.txt text 09242006
05579 myfile3.txt text 09232006
20475 myfile4.txt text 09252006

Report Finished

I need to parse this file to just grab the data lines .. so like this:
00001 myfile.txt text 09222006
00029 myfile1.txt text 09222006
00870 myfile2.txt text 09242006
05579 myfile3.txt text 09232006
20475 myfile4.txt text 09252006

Is that possible with coldfusion? How?

Thanks,
DocSun

From: Wilgeno on
sure is possible. The file is read in via cffile and is really a long string.
You can look at is as a list that is deimated via EOL characters and each line
is a list delimated by spaces.

Here are some quick control codes that you can use to find various spcial
charaters in text files
EOL = #chr(10)#;
TAB = #chr(9)#;
CR = #chr(13)#;
QUOTE = #chr(34)#;
SQUOTE = #chr(39)#;
POUND = #chr(35)#;
COMMA = #chr(44)#;

Basicly listlen(filetext,EOL) should give you an accurate count of lines in
your file. The code snippet I added below should take your file and produce a
query result set with each line of your file split into four data columns.

<!--- set global constants --->
<CFSCRIPT>
EOL = #chr(10)#;
TAB = #chr(9)#;
CR = #chr(13)#;
SPACE=#chr(32)#;
</CFSCRIPT>
<!--- end setting global constants --->

<cfset myresults =
QueryNew("item1,tem2,item3,item4","varchar,varchar,varchar,varchar")>

<cffile action="READ" file="/path_to_file/myfile.txt" variable="filedata">
<cfloop index="current_row" list="#filedata#" delimiters="#EOL#">
<cfscript>
QueryAddRow(myresults ,1);
QuerySetCell(myresults , 'item1', listgetat(current_row,"1",#SPACE#)));
QuerySetCell(myresults , 'item2', listgetat(current_row,"2",#SPACE#));
QuerySetCell(myresults , 'item3', listgetat(current_row,"3",#SPACE#));
QuerySetCell(myresults , 'item4', listgetat(current_row,"4",#SPACE#));
</cfscript>
</cfloop>

From: Dan Bracuk on
There are lots of ways. I assume you can read the file and read each line
separately. Then you have choices. You could,

1. see if the line contains the string .txt.
2. rearrange the last 8 characters and see if they form a valid date.
3. see if the first three characters represent an integer (don't worry about
leading 0's)



From: DocSun on
Thanks Wilgeno, Your tips helped a lot. I couldn't get your exact code to work
but in playing with it, I figure out atleast how to get each record one by one.
So my code now looks like this:

<cffile action = "read"
file = "C:\data.txt"
variable = "rc">
<!--- set global constants --->
<CFSCRIPT>
EOL = #chr(10)#;
TAB = #chr(9)#;
CR = #chr(13)#;
SPACE=#chr(32)#;
</CFSCRIPT>
<!--- end setting global constants --->

<cfset i = 0>
<cfset total_rows = #listlen(rc, EOL)#>
<cfset starting_row = 5>

<cfloop index="current_row" list="#rc#" delimiters="#EOL#">
<cfoutput>#HTMLCodeFormat(listgetat(current_row, "1"), -1)#</cfoutput>
</cfloop>

What I can't figure out how to do is just make this output only the rows
starting_row to total_rows.

Any tips?

Thanks,
DocSun

 | 
Pages: 1
Prev: Apache HTTPClient
Next: consuming a webservice