BackUp LevelNext

Reading, Writing, and Appending to a Text File

In addition to managing files on the server, you can use CFFILE to read, create, and modify text files.

This gives you the ability to

Read a text file (ACTION="READ")

You can use CFFILE to read an existing text file. The file is read into a dynamic parameter which you can use anywhere in the application page. For example, you could read a text file and then insert its contents into a database. Or you could read a text file and then use one of the find and replace functions to modify the contents.

Example

The following example will create a variable named "Message" which will contain the contents of the file "update.txt:"

<CFFILE ACTION="Read"
    FILE="C:\Web\message.txt"
    VARIABLE="Message">

The variable "Message" could then be used in the application page. For example, you could display the contents of the message.txt file in the final Web page:

<CFOUTPUT>#Message#</CFOUTPUT>

Write a text file (ACTION="WRITE")

You can use CFFILE to write a text file based on dynamic content. For example, you could create static HTML files or log actions in a text file.

Example

The following example creates a file with the information a user entered into an HTML insert form:

<CFFILE ACTION="Write"
    FILE="C:\files\updates\#Form.UpdateTitle#.txt"
    OUTPUT="Created By: #Form.FullName#
    Date: #Form.Date#
    #Form.Content# ">

If the user submitted a form in which:

UpdateTitle="FieldWork"
FullName="John Lunch"
Date="10/1/98"
Content="We had a wonderful time in Cambridgeport."

ColdFusion would create a file named FieldWork.txt in the c:\files\updates\ directory. And the file would contain the text:

Created By: John Lunch
Date: 10/1/98
We had a wonderful time in Cambridgeport.

Append to a text file (ACTION="APPEND")

CFFILE can be used to append additional text to the end of an existing text file, for example, when creating log files.

Example

The following example will append the text string "But Davis Square was more fun." to the file FieldWork.txt which was created in the previous example:

<CFFILE ACTION="Append"
    DESTINATION="C:\files\updates\FieldWork.txt"
    OUTPUT="<B>But Davis Square was more fun.</B>">

BackUp LevelNext

allaire

AllaireDoc@allaire.com
Copyright © 1998, Allaire Corporation. All rights reserved.