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. As a result, you can do the following things:

Reading a text file

You can use cffile to read an existing text file. The file is read into a local variable that 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 string replacement functions to modify the contents.

To read a text file:

  1. Create a ColdFusion page with the following content:
    <html>
    <head>
    	<title>Read a Text File</title>
    </head>
    
    <body>
    Ready to read the file:<br>
    <cffile action="read"
    	file="C:\inetpub\wwwroot\mine\message.txt"
    	variable="Message">
    
    <cfoutput>
    	#Message#
    </cfoutput>
    </body>
    </html>
    
  2. Replace C:\inetpub\wwwroot\mine\message.txt with the location and name of a text file on the server.
  3. Save the file as readtext.cfm in the myapps directory under your web_root and view it in the browser.

Writing a text file on the server

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.

To create a form in to capture data for a text file:

  1. Create a ColdFusion page with the following content:
    <html>
    <head>
    	<title>Put Information into a Text File</title>
    </head>
    
    <body>
    <h2>Put Information into a Text File</h2>
    
    <form action="writetextfileaction.cfm" method="Post">
    	<p>Enter your name: <input type="text" name="Name" size="25"></p>
    	<p>Enter the name of the file: <input type="text" name="FileName" size="25">.txt</p>
    	<p>Enter your message:
    	<textarea name="message"cols=45 rows=6></textarea>
    	</p>
    	<input type="submit" name="submit" value="Submit"> 
    </form>
    </body>
    </html>
    
  2. Save the file as writetextfileform.cfm in the myapps directory under your web_root.

Note: The form will not work until you write an action page for it (see the next procedure).

To write a text file:

  1. Create a ColdFusion page with the following content:
    <html>
    <head>
    	<title>Write a Text File</title>
    </head>
    <body>
    <cffile action="write"
    	file="C:\inetpub\wwwroot\mine\#Form.FileName#.txt"
    	output="Created By: #Form.Name#
    #Form.Message# ">
    </body>
    </html>
    
  2. Modify the path C:\inetpub\wwwroot\mine\ to point to a path on your server.
  3. Save the file as writetextfileaction.cfm in the myapps directory under your web_root.
  4. View the file writetextfileform.cfm in the browser, enter values, and submit the form.

    The text file is written to the location you specified. If the file already exists, it is replaced.

Appending a text file

You can use cffile to append additional text to the end of a text file; for example, when you create log files.

To append a text file:

  1. Open the writetextfileaction.cfm file.
  2. Change the value for the action attribute from write to append so that the file appears as follows:
    <html>
    <head>
    	<title>Append a Text File</title>
    </head>
    <body>
    <cffile action="append"
    	file="C:\inetpub\wwwroot\mine\message.txt"
    	output="Appended By: #Form.Name#">
    </body>
    </html>
    
  3. Save the file as writetextfileaction.cfm in the myapps directory under your web_root.
  4. View the file in the browser, enter values, and submit the form.

    The appended information displays at the end of the text file.