Adobe ColdFusion 8

FileWrite

Description

If you specify a file path, writes the entire content to the file. If you specify a file object, writes text or binary data to the file object.

Category

System functions

Function syntax

FileWrite(filepath, data [, charset])

OR

FileWrite(fileobj, data)

See also

FileCopy, FileDelete, FileExists, FileMove, cffile

History

ColdFusion 8: Added this function.

Parameters

Parameter

Description

charset

The character encoding in which the file contents is encoded. The following list includes commonly used values:

  • utf-8
  • iso-8859-1
  • windows-1252
  • us-ascii
  • shift_jis
  • iso-2022-jp
  • euc-jp
  • euc-kr
  • big5
  • euc-cn
  • utf-16

If the file starts with a byte order mark and you set this attribute to a conflicting character encoding, ColdFusion generates an error.

data

Content of the file or file object to create.

fileobj

Name of the file object to write.

filepath

Pathname of the file to write.

If not an absolute path (starting a with a drive letter and a colon, or a forward or backward slash), it is relative to the ColdFusion temporary directory, which is returned by the GetTempDirectory function.

Example

<h3>FileWrite Example</h3>
<!--- This example gets the email addresses of employees, --->
<!--- creates a file object that contains the e-mail addresses, --->
<!--- read the file object, and then creates a text file with a --->
<!--- list of e-mail addresses. --->

<cfquery name="getemployees" datasource="cfdocexamples">
SELECT EMAIL
FROM Employees
</cfquery>

<cfset companymail = "">

<cfloop query = "getemployees">
 <cfset companymail = companymail & #EMAIL# & ";" & " ">
</cfloop>

<cfscript>
FileWrite("mail_list", "#companymail#");
mlist = FileRead("mail_list");
FileWrite("c:\temp\mail_list.txt", "#mlist#");
</cfscript>