• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Parsing files in Coldfusion

New Here ,
Sep 26, 2006 Sep 26, 2006

Copy link to clipboard

Copied

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
TOPICS
Advanced techniques

Views

793

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 26, 2006 Sep 26, 2006

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 26, 2006 Sep 26, 2006

Copy link to clipboard

Copied

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 26, 2006 Sep 26, 2006

Copy link to clipboard

Copied

LATEST
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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation