ColdFusion Programming/Coldfusion form

Advertisement


Question
I have a form that requires the user to choose between two options, in order to write a new text file or append an existing file.  

My question is, how do I script the form to automatically append the file if it exists or create a new one if it does not?

Form sample -

<cfset todayDate = Now()>

<cfif IsDefined("form.formsubmit") is "Yes">
  
<cfset lnameString = "      ">
<cfset lnameString = "#form.lastName#">
<cfset lnameString = "#lnameString# ">
</cfloop>

<cfif form.action is "new">
   
<cffile          action="Write"
file="C:inetpubwwwrootForm#DateFormat(todayDate, "mmdd")#.log"
output="#lnameString#"

variable="writeText">  
</cfif>
 
<cfif form.action is "add">
<cffile          action="Append"
file="C:inetpubwwwrootForm#DateFormat(todayDate, "mmdd")#.log"
output="#lnameString#"

variable="appendText"
addnewline="Yes">
</cfif>
</cfif>

<cfparam    name="fileExists"    default="no">
<cfparam    name="appendText"    default="">

<cfif FileExists("C:inetpubwwwrootForm#DateFormat(todayDate, "mmdd")#.log") is "Yes">
<cfset fileExists="yes">
</cfif>

<form name="Form1" method="post" action="signup.cfm" language="javascript" onSubmit="if (!ValidatorOnSubmit()) return false;" id="Form1">


<select name="action">
<cfif fileExists is "no">
  <option value="new"></cfif>
<cfif fileExists is "yes">
  <option value="add"></cfif>
</select>
<input type="Hidden" name="formsubmit" value="yes">
                       <input type="image" name="ImageButton1" onClick="if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); " language="javascript" id="ImageButton1" title="Submit Information" src="button_submit.gif" alt="" border="0" />
        
</form>  

Answer
Hi

You can use FileExists method to findout the whether the file is exists or not if exists append else create new something like this

The following code will solve most of the coding rather you pass thru hidden field.

<cfif FileExists(yourfile)>
  <cffile  action="Append"
   file="C:inetpubwwwrootForm#DateFormat(todayDate, "mmdd")#.log"
   output="#lnameString#"
   variable="appendText"
   addnewline="Yes">
<cfelse>
   <cffile action="Write"
   file="C:inetpubwwwrootForm#DateFormat(todayDate, "mmdd")#.log"
   output="#lnameString#"
   variable="writeText">  
</cfif>

-Srini

ColdFusion Programming

All Answers


Answers by Expert:


Ask Experts

Volunteer


Srini

Expertise

I have extensive knowledge on coldfusion MX, cfc etc...

Experience

Contact me if you need any custom development on ASP.NET, ASP, Coldfusion, Powerbuilder

©2012 About.com, a part of The New York Times Company. All rights reserved.