<CFX_FileReadLines>

This tag is similar to the CFFILE ACTION= "READ" functionality, in that it reads from an ASCII file and returns the text for your use in ColdFusion templates. What's different is that this tag allows you to read in only part of a file at once, or just a single line of the file.  It returns the text to you as a query rather than as a single text variable, which allows you to use convenient syntax in your code to refer to the file's contents in a line-by-line fashion.

Note: This tag was compiled using Delphi 4 and Vadim's DelphiCFX package. If you enjoy the tag's performance, consider trying these tools out yourself, or buy me a little gift of some kind, or both! :) -nate

Parameters

FILE

Required. The full filesystem-style path to the text file.

NAME

Required. The name of the query result set to put the file's contents into. Each line of the file will be a separate row in the query.  So if NAME="MyQuery", then you could use QUERY= "MyQuery" in a CFOUTPUT tag to display the contents of the file.

The query will always contain two columns:

  • LineNum - The line number of the file.  The first line of the file has a LineNum of 1, and so on.  Note that this will be the same as the automatic #CurrentRow# variable unless you specify a STARTROW parameter.
     
  • Text - The actual text from that line of the file. Note that you will be able to refer directly to a particular line in your file by referring to the TEXT column with array-style syntax. For instance, the fifth line of your file would be available with the following (assuming that you didn't specify a STARTROW parameter):
    MyQuery.Text[5]
STARTROW

Optional. Defaults to 1.

The line number to start at. So if STARTROW= "10",the data in the 10th line of the file will be in the first row of the returned query.

MAXROWS

 

Optional. If provided, provides the maximum number of lines that should be read in from the file. Useful if you have a very large file and don't want to read in the whole thing at once because it would take up too much of your system's resources. See example below.

After the tag executes, the following variables will be populated for you:

Example
The following example demonstrates how to use STARTROW/MAXROWS and related variables to create a "data chunking" loop. The file is read in 10-line chunks. Each chunk of lines can be processed or output in whatever way you want, and the loop will execute as many times as needed until the end of the file is reached. Note that while this tag is compatible with ColdFusion 2.0, this template wouldn't work because it would result in multiple queries with the same name, which was not allowed in 2.0.

<CFSET File_StartRow = 1>
<CFSET File_ReachedEOF = "No">

<CFLOOP CONDITION="NOT File_ReachedEOF">

  <CFX_FileReadLines
    FILE="d:\CFUSION\BIN\cf40p.ini"
    NAME="MyQuery"
    MAXROWS="10"
    STARTROW="#File_StartRow#">
    
  <CFOUTPUT>
    <P><B>Rows Read:</B> #File_LinesRead#<BR>
    <B>ReachedEOF:</B>   #File_ReachedEOF#<BR>
    <B>StartRow:</B>     #File_StartRow#<BR>
  </CFOUTPUT>
    
  <CFOUTPUT QUERY="MyQuery">
    <LI> Row #CurrentRow#(#LineNum#): #Text#
  </CFOUTPUT>
  
</CFLOOP>
 

This would cause something like the following to be displayed:

Rows Read: 10
ReachedEOF: NO
StartRow: 11

  • Row 1(1): [Program]
  • Row 2(2): Company=Allaire Corporation
  • Row 3(3): Product=ColdFusion Pro
  • Row 4(4): Version=Version 4.0
  • Row 5(5): Copyright=Copyright ©1995-98 Allaire Corp.
  • Row 6(6): AppName=CF40PEval
  • Row 7(7): Phone=(888) 939-2545
  • Row 8(8): Prefix=CF40P
  • Row 9(9):
  • Row 10(10): [User]

    Rows Read: 10
    ReachedEOF: NO
    StartRow: 21

  • Row 1(11): UserName=ColdFusion Professional Eval User
  • Row 2(12): UserCompany=
  • Row 3(13): RegNum=342404764643
  • Row 4(14):
  • Row 5(15): [Messages]
  • Row 6(16): PhoneNum=(888) 939-2545
  • Row 7(17): NewTrial=Your 30-day evaluation period for Allaire ColdFusion Professional is about to begin.
  • Row 8(18): RemainingTime=Cold Fusion Professional will expire in
  • Row 9(19): Purchase=You may purchase the full version of ColdFusion Professional now by pressing the "Purchase" button. Otherwise, press "OK".HasExpired=Allaire ColdFusion Professional Evaluation has expired. Press the "Purchase" button to unlock ColdFusion.
  • Row 10(20): Order=Call (888) 939-2545 to obtain the code to unlock this copy of ColdFusion Professional.

    Rows Read: 10
    ReachedEOF: NO
    StartRow: 31

  • Row 1(21): Recommend=
  • Row 2(22): Operator=
  • Row 3(23):
  • Row 4(24): [Responses]
  • Row 5(25): New=Welcome! Your evaluation period is about to begin.
  • Row 6(26): Trial=You may purchase this product at any time.
  • Row 7(27): Expired=Your evaluation period has expired. Please contact Allaire Corporation.
  • Row 8(28): Purchased=Thank you for your purchase.
  • Row 9(29):
  • Row 10(30): [Registration]

    Rows Read: 4
    ReachedEOF: YES
    StartRow: 35

  • Row 1(31): User=
  • Row 2(32): Company=
  • Row 3(33):
  • Row 4(34):