Working with Text Files ColdFusion Help

The CFFILE tag provides a useful mechanism for working with text files and the text they contain.
Three actions are possible when working with text files: reading, writing, and appending. W.e.will present each in turn

Reading a Text File

Reading the contents of a text file is done using the ACTION= n READ’ attribute of the CFFILE tag. With the addition of the following two attributes, it is possible to read the entire contents of a file into a ColdFusion variable:

FILE Specifies the name of the file (including the path) to be read; this is a required attribute.
VARIABLE Specifies the name of the variable in which to store the contents of the text file; this is a required attribute.

After the contents of a file are stored ina variable, they can be manipulated like any other string and array data, using the functions discussed in Chapter 14, “Working with Cold Fusion Data Structures.” The data can also be displayed using the standard CFOUTPUT tag.

The following example reads the contents of the text file c: \ temp\ test. txt into a variable named TestVar and then displays the contents for the user:

<CFFILE ACTIONz’Read’
FILE-‘c:\temp\test.txt’
VARIABLE-‘TestVar”)
<Hl>The contents of test.txt</Hl

Writing to a File

As would be expected, writing to a file is achieved using the ACTION=”WRITEn attribute of the CFFIlE tag. When using this action, it is possible to create text files for numerous reasons, including reports, error messages when problems occur in a template, or any other task that benefits from the creation of a text file. Dynamic data can be included in the resulting text file.

When writing to a text file, four other attributes of the CFFIlE tag are available:

FILE Specifies the name of the file to be created (including the complete path). This is a required attribute.
OUTPUT Specifies the contents of the file to be created. This is a required attribute.
MODE Specifies the permissions that should be applied to a file when it is saved. This attribute is not applied in the Wiridows version of ColdFusion. In the Solaris version of ColdFusion, possible values are numerical octal triplets that are valid with the Unix chmod command.
ATIRIBUTES This attribute mirrors the ATIRIBUTESattribute available when using
ACTION-‘UPLOADn  It specifies a comma-delimited list of file attributes that will be applied to the file resulting from a write action. The comma-delimited list can be chosen from the following list of file attributes: ReadOn1y, Temporary, Arch; ve, Hidden, System,”~d Norma1. When this attribute is not used, the original source file’s attributes are maintained for the destination file. By way of example, the following tag creates a new text file called c: \ temp\newtest. txt, “and stores dynamic content in it from a form”submitted by a user:

<CFFILE ACTION-‘WRITE’ .
FILE-‘c:\temp\newtest.txt’
OUTPUT-‘The follow1ng form data was submitted
Name: IForm.Namel
E-mail: IForm.Email’)

Notice that the value of the OUTPUTattribute spans multiple lines. Each line break will be reflected in the resulting text file, and dynamic data (such as #Form. EIIai1#) will reflect the value of the parameters. If the user submits the form with the n<1;IDUese rname
and an e-mail address of userOsome. host, then the resulting text file will look like this:

The following data was submitted
Name: Username
E-mail: userOsome.host

Appending to a File

Appending to afile, which is achieved using the ACTION=”.APPENDII attribute of the CFFILE tag, is similar to writing to a text file, except that instead of creating a new file that is initially empty, the output content is added to the end of an existing text file. This is particularly useful for creating log files for your applications. For instance, your application may want to log each time a user performs a particular action or each time a certain series of events occur. These entries can be accumulated in a single log file, which can then be reviewed at regular intervals to ensure the normal operation of the application.

Appending to a file is similar to writing and makes use of three attributes: FILE Specifies the name of the file to be created-(including the complete path). This is a required attribute.

OUTPUT Specifies the contents of the file to be created. This is a required attribute.
ATTRIBUTES This attribute mirrors the ATTRIBUTESattribute available when using
ACTION'”UPLOAD‘.It specifies a comma-delimited list of file·attributes that will be applied to the file resulting from a write action. The comma-delimited list can be chosen from the following list of file attributes: ReadOn1y, Temporary, Archive, Hdden, System, and Normal. When this attribute is not used, the original source file’s attributes are maintained for the destination file.
For instance, the following example appends one line of text to an existing file:

<CFFILE ACTION-‘APPENQ’
FILE-‘c:\temp\log.txt’
OUTPUT-‘Another line of text’)

Posted on November 14, 2015 in File Management

Share the Story

Back to Top
Share This