Helpful Information
 
 
Category: ColdFusion Development
Simple Write to File

Ok, I am completely new to ColdFusion. Completely. But I am on a small project right now where I have a form on a web page and I need to write the results of that form to a flat text file...the file can be comma delimited or tab...doesn't matter.

I have the ColdFusion MX Bible, but I can't seem to find a quick answer. I can do this in seconds with PHP...but this is all news to me.

Can someone point me to an easy tutorial or show me some code to get me on my way? Thanks.

You can do this with one line of code using the <cffile> tag.

So no other ColdFusion code on the page? I assume I have to save the file as a .cfm page, correct? Thanks.

No you can write out the file with any file extension you want, but it will basically be a text file.

<cffile action="WRITE" file="c:\myfile.txt" output="What I want to write to the file. This can be a variable, of course.">

Actually, I meant save the page with the form as a cfm page. And the c:\ reference? Is that the same as directories within my web server? One last thing....variables are noted as #variable#, right? Thanks a ton for all this help!!!!

Maybe I'm missing something. Lets break it down:

myformpage.cfm has HTML and form elements in it...text boxes, select boxes, etc. The action of the form points to myformpage_action.cfm or something. (the names can obviously be anything you need them to be).

The user submits the form which issues an HTTP POST to the myformpage_action.cfm page.

On myformpage_action.cfm, all of the values that the user entered into the form are available in the FORM scope. So if myformpage.cfm had a form field called "username", the myformpage_action.cfm could reference that as #form.username#.

Now you could do this:

<cffile action="WRITE" file="c:\myformdata.txt" output="The user name is #form.username#.">

The path to the file can be whatever you need it to be, but yes, it is a path on the web server. CF (nor any other app server) has access to save files to the client's hard drive.

You can also use action="APPEND" to append the data to an existing text file if you don't want to overwrite it.

It's not you that's missing something, it's me....I'm a complete CF newbie. here is what I have for the form on this page:


<cffile action="WRITE" file="/myformdata.txt" output="#form.username#" action="APPEND">
<form action="elertBGthanks.cfm" method="post" name="form">
<table border="0" cellspacing="3" cellpadding="3">
<tr>
<td align="right" valign="top">User Name</td>
<td align="left" valign="top"><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td align="right" valign="top">&nbsp;</td>
<td align="left" valign="top"><table width="100%" border="0" cellspacing="3" cellpadding="3">
<tr align="left">
<td align="right"> <input type="submit" name="Submit" value="Submit">
</td>
<td> <input name="Reset" type="reset" id="Reset" value="Reset"></td>
</tr>
</table></td>
</tr>
</table>
</form>

basically nothing is working. I hit submit and I get a page cannot be displayed error for elertBGthanks.cfm. And I have no file on my web server called myformdata.txt. Thanks again for the advice.....

I think that the problem is that the CFFILE tag (which does the write of the form data to the file) has to be on the ACTION page. Where you have the CFFILE tag now, there is no form data yet. Only when the submit the form is the form data available. Try moving the CFFILE tag to the action page (elertBGthanks.cfm).

Man...I just can't get it to work. I can get the thanks page to display properly when I type in the URL, but when I get to it from the first page, it won't work. Thanks for the help, but I'm going to see if I can get this person to move the site to a Linux machine so I can use PHP. Thanks, though!

This is a quick and dirty solution that will work with any form. As long as the code below (that resided on the action page) posts the dat using a HTTP POST method this should work file.

<!--- Check fi FIELDNAMES is defined (CF passes along list of fieldnames with all form POSTS) --->
<CFIF IsDefined("FIELDNAMES")>

<!--- Set empty string to hold file content --->
<CFSET Str = "">

<!--- Loop over form fields and build string to write to file, each item seperated by CR + LF --->
<CFLOOP INDEX="Index" LIST="#FIELDNAMES#">
<CFSET STr = Str & Chr(13) & Chr(10) & Index & " : " & Evaluate(Index)>
</CFLOOP>

<!--- replace first CR + LF with nothing (remove) --->
<CFSET Str = Replace(Str, Chr(13) & Chr(10), "", "ONE")>

<!--- write content of STR to file --->
<CFFILE ACTION="WRITE" FILE="text01.txt" OUTPUT="#STR#" ADDNEWLINE="NO">

</CFIF>

OR

<!--- Check fi FIELDNAMES is defined (CF passes along list of fieldnames with all form POSTS) --->
<CFIF IsDefined("FIELDNAMES")>

<!--- Loop over form fields and build string to write to file, each item seperated by CR + LF --->
<CFLOOP INDEX="Index" LIST="#FIELDNAMES#">
<CFFILE ACTION="APPEND" FILE="text02.txt" OUTPUT="#Index# : #Evaluate(index)#" ADDNEWLINE="YES">
</CFLOOP>
</CFIF>

Hi.
I am trying to do the same thing... BUT, I want the end result to be a comma delimited file with quotation marks as the text qualifier.... Like:

"FirstName","LastName","Date"
"Geddy","Lee","04-04-07"
"Neal","Peart","04-04-08"
"Alex","Lifeson","04-04-09"

Can you tell me how to do that? I am close, but I cannot get quotation marks in my output, and also the "AddNewLine=Yes" seems to do nothing at all.

Append the output attribute to something like the following:-

<CFFILE ACTION="APPEND" FILE="text02.txt" OUTPUT=" '#form.username#','#form.password#' " ADDNEWLINE="YES">

Will that place quotes around each?

Yes, it should.

To add to this... how would you replace parts of the data in the text file? Lets say you selected a record to edit in a form, you submit, and want just that one line of record to update. Is this possible with text files? I am guessing probably not... So how would I approach this...

You would need to regenerate the contents of the text file and replace what is there. Which, again, is another of the many reasons why trying to use a text file as some type of database is such a bad idea. You're also going to run into major problems if different requests or threads access the file simultaneously. It will almost certainly result in completely corrupted data.

Understood. I know what you mean and I brought this up but I still need to get it done and I wish to think this is for training purpose where the next step for me is to do the same thing with a database.

Here are the steps I am thinking to do this...
1. Create an 2D array
2. Loop through the pipe delimted text file
3. Separate by line and pipes
4. Delete selected record passed from edit page
5. append the updated recorded passed from the edit page
6. sort the 2 D array by code
7. Write to text file

I see some problems as I wrote this... maybe this is the wrong approach... :confused:


You would need to regenerate the contents of the text file and replace what is there. Which, again, is another of the many reasons why trying to use a text file as some type of database is such a bad idea. You're also going to run into major problems if different requests or threads access the file simultaneously. It will almost certainly result in completely corrupted data.

That sounds about like what it will take. What do you think the problem is?

I understand that you want to take this as a training exercise, but I have to tell you that doing this with a database would take about 30 minutes to do, compared to the days you're spending on this text file solution.










privacy (GDPR)