User Guide Cancel

FileWrite

 

Note:

You can find the CFFiddle demo of this function and other file functions as part of a project that is shared with you.

Click the button below to launch CFFiddle.

To copy the project in your workspace in CFFiddle, follow the steps below:

  1. Log in with your Gmail or Facebook credentials.
  2. Navigate to the project in the left pane.
  3. Once you make some changes in any cfm in the project, a pop up displays asking you to save the project.
  4. Give the project a suitable name and click Save.
  5. Create a folder named dir1 and upload a text file, myfile.txt.

Description

If you specify a file path, writes the entire content to the specified on-disk or in-memory file. If you specify a file object, writes text or binary data to the file object.

Category

System functions

Function syntax

FileWrite(file, data [, charset])

OR

FileWrite(file, data)

See also

FileCopyFileDeleteFileExistsFileMovecffile

History

ColdFusion (2018 release) Update 5 and ColdFusion (2016 release) Update 12: Changed parameter name filePath to file.

ColdFusion (2018 release): Introduced named parameters.

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.

file

Name of the file object to write.

Pathname of the on-disk or in-memory file to write. If not an absolute path (starting 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.

Usage

Use the following syntax to specify an in-memory file, which is not written to disk. In-memory files speed processing of transient data.

ram:///filepath

The filepath can include directories, for example ram:///petStore/images/poodle.jpg. Create the directories in the path before you specify the file. For more information on using in-memory files, see Working with in-memory files in the Developing ColdFusion Applications.

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>
<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>
<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>

Example with named parameters

<cfscript>
myFile=GetDirectoryFromPath(GetCurrentTemplatePath()) & "file.txt"
FileWrite(file=myFile, data="Hello World!",charset= "UTF-8")
</cfscript>
<cfscript> myFile=GetDirectoryFromPath(GetCurrentTemplatePath()) & "file.txt" FileWrite(file=myFile, data="Hello World!",charset= "UTF-8") </cfscript>
<cfscript> 
 myFile=GetDirectoryFromPath(GetCurrentTemplatePath()) & "file.txt" 
 FileWrite(file=myFile, data="Hello World!",charset= "UTF-8") 
</cfscript>

Get help faster and easier

New user?