3 Replies Latest reply on Sep 26, 2006 12:56 PM by DocSun

    Parsing files in Coldfusion

    DocSun
      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
        • 1. Re: Parsing files in Coldfusion
          Wilgeno_wnt
          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.
          • 2. Re: Parsing files in Coldfusion
            Dan Bracuk Level 5
            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)

            • 3. Re: Parsing files in Coldfusion
              DocSun Level 1
              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