• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 512
  • Last Modified:

How do I close up a blank line in cffile output?

In the attached code snippet I am creating a .csv file for downloading to Word.

On some records the "Address2" field and "Fax" fields are blank. When these fields are blank the output contains a blank line.  I want to close up all blank lines from the output.

Thanks in advance for any help.
<cfoutput>
  <cfloop query="Recordset1">
    <cffile action="append"
    addnewline="yes"
    file="#tempfile#"
    output="
#TRIM(RecordSet1.company_name)#
#TRIM(RecordSet1.fname)# #TRIM(RecordSet1.lname)# #IIF("Recordset1.certifications" IS NOT "",TRIM("RecordSet1.certifications"),"")#
#TRIM(RecordSet1.address1)# 
#IIF("RecordSet1.address2" neq "",TRIM("RecordSet1.address2"),DE(""))#
#TRIM(RecordSet1.city)#, #TRIM(RecordSet1.st)# #TRIM(RecordSet1.zip)#
Phone: #TRIM(RecordSet1.phone)# #IIF("RecordSet1.ext" IS NOT "",TRIM("RecordSet1.ext"),DE(""))#
#IIF(TRIM(RecordSet1.fax) neq "",DE("Fax: "),DE(""))##IIF(TRIM(RecordSet1.fax) neq "",TRIM("RecordSet1.fax"),DE(""))#
#IIF("RecordSet1.email" neq "",TRIM("RecordSet1.email"),DE(""))#
#IIF("RecordSet1.website" neq "",TRIM("RecordSet1.website"),DE(""))#
#IIF("RecordSet1.description" neq "",TRIM("RecordSet1.description"),DE(""))#"
>
     
</cfloop>
</cfoutput>

Open in new window

0
edfm
Asked:
edfm
  • 3
1 Solution
 
black0psCommented:
Maybe try saving the content before you put it in the cffile. Put it in a variable, then output the variable. This way you can use <cfif> instead of all of the iif().
<cfoutput>
<cfsavedcontent variable="myVar">
#TRIM(RecordSet1.company_name)#
#TRIM(RecordSet1.fname)# #TRIM(RecordSet1.lname)# #IIF("Recordset1.certifications" IS NOT "",TRIM("RecordSet1.certifications"),"")#
#TRIM(RecordSet1.address1)# 
#IIF("RecordSet1.address2" neq "",TRIM("RecordSet1.address2"),DE(""))#
#TRIM(RecordSet1.city)#, #TRIM(RecordSet1.st)# #TRIM(RecordSet1.zip)#
Phone: #TRIM(RecordSet1.phone)# #IIF("RecordSet1.ext" IS NOT "",TRIM("RecordSet1.ext"),DE(""))#
#IIF(TRIM(RecordSet1.fax) neq "",DE("Fax: "),DE(""))##IIF(TRIM(RecordSet1.fax) neq "",TRIM("RecordSet1.fax"),DE(""))#
#IIF("RecordSet1.email" neq "",TRIM("RecordSet1.email"),DE(""))#
#IIF("RecordSet1.website" neq "",TRIM("RecordSet1.website"),DE(""))#
#IIF("RecordSet1.description" neq "",TRIM("RecordSet1.description"),DE(""))#
</cfsavedcontent>
  <cfloop query="Recordset1">
    <cffile action="append"
    addnewline="yes"
    file="#tempfile#"
    output="#myVar#">
     
</cfloop>
</cfoutput>

Open in new window

0
 
edfmAuthor Commented:
Thanks to blackOps for putting for the suggestion.

The solution offered by blackOps put me on the right track to a solution. I have attached the code that worked for me.  Instead of trying to close up the blank lines created by Carriage returns created by an "Enter"  after each link of code, I put all the code on one line and used cfif statement to insert a Chr(10) line feed at the desired locations.  
<cffile action="write"
	addnewline="yes" 
	file="#tempfile#" 
    output=" ">
 
 
  <cfloop query="Recordset1">
<cfoutput>
<cfsavecontent variable="myVar"><cfif #TRIM(RecordSet1.company_name)# GT "">#TRIM(RecordSet1.company_name)##Chr(10)#</cfif><cfif #TRIM(Recordset1.certifications)# GT "">#TRIM(RecordSet1.fname)# #TRIM(RecordSet1.lname)# #TRIM(Recordset1.certifications)##Chr(10)#<cfelse>#TRIM(RecordSet1.fname)# #TRIM(RecordSet1.lname)##Chr(10)#</cfif><cfif #RecordSet1.address2# CONTAINS "Suite">#TRIM(RecordSet1.address1)#, #TRIM(RecordSet1.address2)##Chr(10)#<cfelse>#TRIM(RecordSet1.address1)##Chr(10)#</cfif><cfif (#TRIM(RecordSet1.address2)# GT "") AND (#TRIM(RecordSet1.address2)# DOES NOT CONTAIN "Suite")>#TRIM(RecordSet1.address2)##Chr(10)#<cfelse></cfif>#TRIM(RecordSet1.city)#, #TRIM(RecordSet1.st)# #TRIM(RecordSet1.zip)##Chr(10)#<cfif #TRIM(RecordSet1.ext)# GT "">Phone: #TRIM(RecordSet1.phone)#  Ext: #TRIM(RecordSet1.ext)##Chr(10)#<cfelseif #TRIM(RecordSet1.phone)# GT "">Phone: #TRIM(RecordSet1.phone)##Chr(10)#</cfif><cfif #TRIM(RecordSet1.fax)# GT "">Fax: #TRIM(RecordSet1.fax)##Chr(10)#<cfelse></cfif><cfif #TRIM(RecordSet1.email)# GT "">#TRIM(RecordSet1.email)##Chr(10)#<cfelse></cfif><cfif #TRIM(RecordSet1.website)# GT "">#TRIM(RecordSet1.website)##Chr(10)#<cfelse></cfif><cfif #TRIM(RecordSet1.description)# GT "">#TRIM(RecordSet1.description)#<cfelse></cfif>
</cfsavecontent>
</cfoutput>
    <cffile action="append"
    addnewline="yes"
    file="#tempfile#"
    output="#myVar#">
     
</cfloop>

Open in new window

0
 
edfmAuthor Commented:
Thanks for the help on this. I had to move things around a bit and put everything on one line and then insert #chr(10)# to create the line feeds at the desired places. But the idea of using cfsavecontent allowed me to use cfif statement to make it work. I posted the correct code in a comment on the questions.
0
 
edfmAuthor Commented:
Here is the final code that worked for this solution.  

To close up blank lines in cffile output I eliminated all key board generated carriage returns in the cfsavecontent section creating one long line of code. Then used #chr(10)# generated line feeds controlled by cfif statements to get the desired results.

<cffile action="write"
	addnewline="yes" 
	file="#tempfile#" 
    output=" ">
 
 
  <cfloop query="Recordset1">
<cfoutput>
<cfsavecontent variable="myVar"><cfif #TRIM(RecordSet1.company_name)# GT "">#TRIM(RecordSet1.company_name)##Chr(10)#</cfif><cfif #TRIM(Recordset1.certifications)# GT "">#TRIM(RecordSet1.fname)# #TRIM(RecordSet1.lname)# #TRIM(Recordset1.certifications)##Chr(10)#<cfelse>#TRIM(RecordSet1.fname)# #TRIM(RecordSet1.lname)##Chr(10)#</cfif><cfif #RecordSet1.address2# CONTAINS "Suite">#TRIM(RecordSet1.address1)#, #TRIM(RecordSet1.address2)##Chr(10)#<cfelse>#TRIM(RecordSet1.address1)##Chr(10)#</cfif><cfif (#TRIM(RecordSet1.address2)# GT "") AND (#TRIM(RecordSet1.address2)# DOES NOT CONTAIN "Suite")>#TRIM(RecordSet1.address2)##Chr(10)#<cfelse></cfif>#TRIM(RecordSet1.city)#, #TRIM(RecordSet1.st)# #TRIM(RecordSet1.zip)##Chr(10)#<cfif #TRIM(RecordSet1.ext)# GT "">Phone: #TRIM(RecordSet1.phone)#  Ext: #TRIM(RecordSet1.ext)##Chr(10)#<cfelseif #TRIM(RecordSet1.phone)# GT "">Phone: #TRIM(RecordSet1.phone)##Chr(10)#</cfif><cfif #TRIM(RecordSet1.fax)# GT "">Fax: #TRIM(RecordSet1.fax)##Chr(10)#<cfelse></cfif><cfif #TRIM(RecordSet1.email)# GT "">#TRIM(RecordSet1.email)##Chr(10)#<cfelse></cfif><cfif #TRIM(RecordSet1.website)# GT "">#TRIM(RecordSet1.website)##Chr(10)#<cfelse></cfif><cfif #TRIM(RecordSet1.description)# GT "">#TRIM(RecordSet1.description)#<cfelse></cfif>
</cfsavecontent>
</cfoutput>
    <cffile action="append"
    addnewline="yes"
    file="#tempfile#"
    output="#myVar#">
     
</cfloop>

Open in new window

0
 
duncancummingCommented:
Can I make some suggestions?  

Firstly, you don't need to wrap variables in # # inside most cf tags, such as CFIF.  e.g.
<cfif #TRIM(RecordSet1.company_name)# GT "">
can be written as
<cfif TRIM(RecordSet1.company_name) GT "">
This is considered best practice, takes less typing, should run fractionally quicker, and is easier to read.

Secondly, I'm not sure about the method you're using for checking string length
<cfif TRIM(RecordSet1.company_name) GT "">
While technically I suppose that would work, I think what you actually mean to do is
<cfif Len(TRIM(RecordSet1.company_name)) GT 0>
or perhaps just
<cfif TRIM(RecordSet1.company_name) NEQ "">
i.e. either the string has length > 0, or the string is not an empty string.  To say the string is greater than an empty string is a slightly odd comparison.  

Thirdly another way to do this would be to append each line of your file to a struct, list or array, then output that to your file.  I'd recommend the array.
<cfset fileArray = ArrayNew(1)>
<cfif Len(TRIM(RecordSet1.company_name))>
	<cfset ArrayAppend(fileArray, "#TRIM(RecordSet1.company_name)#")>
</cfif>
<cfif Len(TRIM(Recordset1.certifications))>
	<cfset ArrayAppend(fileArray, "#TRIM(RecordSet1.fname)# #TRIM(RecordSet1.lname)# #TRIM(Recordset1.certifications)#")>
<cfelse>
	<cfset ArrayAppend(fileArray, "#TRIM(RecordSet1.fname)# #TRIM(RecordSet1.lname)#")>
</cfif>
etc
 
<cfloop index="i" from="1" to="#ArrayLen(fileArray)#">
	<cffile action="append"
		addnewline="yes"
		file="#tempfile#"
		output="#fileArray[i]#">
</cfloop>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now