ColdFusion Programming/Form text results

Advertisement


Question
Subject   Form results in a text file
Question   QUESTION: Hi, I am using the cffile tag to write to a text file.  The resulting text file is uploaded by a peoplesoft application for further processing.

My question is how do I script coldfusion to provide certain attributes like start position, size, etc.?

I would like the form field data in the text file to display in a specific order and position number like, FirstName (start position 7)  (size 30)
LastName (start position 37)  (size 30)
etc,.

Thanks
Michael

ANSWER: This is not easy and probably won't work very well.  The only way to do it tho is to create a variable called something like myFile and then add the form field data in the order you want it. That part is a piece of cake.

Trying to get it to have a specific start position will require coding to count the length of the input data and then add on spaces ( ) to the end of the string to make it equal to the size. This will make the next input string start in the right place.  If your first field does not start on the 1 spot then you will have to start the string with spaces to put the first one where you want it to start.

On the other hand if it is greater than the size you have to truncate it which is much easier to do.

Once you have that all into myFile, you write it to the text file.

---------- FOLLOW-UP ----------

QUESTION: Do you have any examples?  How would the script look like?

ANSWER: <--- Depends on if it starts at 1 or not IF it starts at say position 3 then it would be. So to start at the first space put in NO spaces. --->
<cfset myString = "  ">
<!--- Now grab the first form field you want, say form.FName and you want it to be 30 characters long starting at position 3. It will start at position 3 because of the 2 blank spaces --->
<cfset myString = "#myString##form.FName#">
<!--- Now find out how many spaces to make it 30 long total --->
<cfset blankLen = 30-#Len(form.FName)#>
<!--- Now create a loop to build the blank spaces --->
<cfloop from="1" to="#blankLen#" index="x">
<cfset myString = "#myString# ">
</cfloop>

You would have to do this for every form field. This is very rough and I didn't verify that it will work perfectly. It is just an example of how it can be done. I'm sure there are better ways.


---------- FOLLOW-UP ----------

QUESTION: Hi,  I tried this setup and it did not work.  Do you know if JavaScript might have a solution?

--------------------------------------------------------------------------------

Answer   Javascript would be doing the exact same thing just different language. There is no easy way to do this.
What did NOT work?

----------------------------------------------------

Here is my code

<cfset myString = "">


<cfset myString = "#myString##form.Name#">


<cfset blankLen = 30-#Len(form.Name)#>


<cfloop from="1" to="#blankLen#" index="x">
<cfset myString = "#myString#">
</cfloop>


<cfset todayDate = Now()>
  

<cfif IsDefined("form.formsubmit") is "Yes">
  
<cfif form.action is "new">
  
<cffile          action="Write"
        file="C:\inetpub\wwwroot\MAkande\Form#DateFormat(todayDate, "mmdd")#.log"
output="#myString#"

variable="writeText">  
  </cfif>
 
<cfif form.action is "add">
   
<cffile          action="Append"
        file="C:\inetpub\wwwroot\MAkande\Form#DateFormat(todayDate, "mmdd")#.log"
output="#myString#"

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

</cfif>

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


<cfif FileExists("C:\inetpub\wwwroot\MAkande\Form#DateFormat(todayDate, "mmdd")#.log") is "Yes">
  <cfset fileExists="yes">
</cfif>

------------------------------------------------------

At the moment the form data saves to a text file that does not place the results as I want them to be.  The results are as follows

     akande               mike                       2

The spacing and length of the data does not reflect the code.  

Answer
Here is the problem it looks like

<cfloop from="1" to="#blankLen#" index="x">
<cfset myString = "#myString#">
</cfloop>

You have to add a blank space to myString each loop thru. I tried putting & n b s p ; in there but it didn't show up if I put all the letters together.

Right now it appears that you are just setting myString = myString which does nothing.

You may have to adjust the number of blank spaces added by doing blankLen-1.

Another way to do this without having to do a loop is use the RepeatString function.

So you would create your blanks by doing this:

<cfset myString = "#myString#RepeatString(' ',#blankLen#)">

Once again you may have to put & n b s p ; in there (removing the blanks.

ColdFusion Programming

All Answers


Answers by Expert:


Ask Experts

Volunteer


Donald Hammond

Expertise

Expert in ColdFusion, Flash ActionScript, XML, and SQL.

Experience

Cold Fusion, Flash Action Script

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