We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Extra carriage return in <cffile> created tab delimited text file

rragledavis
rragledavis asked
on
Medium Priority
1,180 Views
Last Modified: 2013-12-24
I have created a script that converts a number of records in a database to a tab delimited text file and emails the file to the person who needs to import it into their own database. This was working a few weeks ago and some minor change (?) has caused it to regularlt return a file that adds an extra blank line between each record. I have poured over this code, viewed it in a text editor to see hidden characters, removed the hard return and substituted <cfset newline=Chr(13& Chr(10> and then used #newline# instead of the hard return. Here is the current code: (not including the query and with email addresses removed)

I am stumped.

<cfsetting enablecfoutputonly="yes"></head><body><cfsavecontent variable="admissions_file"><cfloop query="freshman"><cfoutput>"#Title#"      "#Name#"      "#Middle#"      "#Last#"      "#Address#"      "#Address2#"      "#City#"      "#State#"      "#Zip#"      "#Country#"      "#Email#"      "#Interest#"      "#Year#"      "#Date#"      "#ID#"      "#Trim(Degree)#"
</cfoutput></cfloop></cfsavecontent><cfoutput><cffile action="write" output="#admissions_file#" file="filepath\freshman.#DateFormat(Now(),'mmddyy')#.txt" nameconflict="overwrite"> </cfoutput><cfmail from="email@email.com" to="email@email.com" cc="email@email.com" subject="freshman.#DateFormat(Now(),'mmddyy')#.txt" mimeattach="filepath\freshman.#DateFormat(Now(),'mmddyy')#.txt">Your freshman catalog requests</cfmail>
Comment
Watch Question

Commented:
I ran into a similar problem and found the issue was the CFSAVECONTENT tag.  For some reason it was putting in hidden line break characters.  The only way I got around it was to use something else.  It's a great tag, but when you need to be precise in making delimited files it doesn't seem to be up to the task.

Maybe something like this will work instead:

<cfset admissions_file= "">

<cfloop query="freshman">
  <cfset admissions_file= "#Title#" & chr(9)>
  <cfset admissions_file= "#Name#" & chr(9)>
  <cfset admissions_file= "#Middle#" & chr(9)>
  <cfset admissions_file= "#Last#" & chr(9)>
  <cfset admissions_file= "#Address#" & chr(9)>
  <cfset admissions_file= "#Address2#" & chr(9)>
  <cfset admissions_file= "#City#" & chr(9)>
  <cfset admissions_file= "#State#" & chr(9)>
  <cfset admissions_file= "#Zip#" & chr(9)>
  <cfset admissions_file= "#Country#" & chr(9)>
  <cfset admissions_file= "#Email#" & chr(9)>
  <cfset admissions_file= "#Interest#" & chr(9)>
  <cfset admissions_file= "#Year#" & chr(9)>
  <cfset admissions_file= "#Date#" & chr(9)>
  <cfset admissions_file= "#ID#" & chr(9)>
  <cfset admissions_file= "#Degree#" & chr(10)>
</cfloop>

<cffile action="write" output="#admissions_file#" file="filepath\freshman.#DateFormat(Now(),'mmddyy')#.txt" nameconflict="overwrite">

Author

Commented:
Just tried and this just resets the value of admissions_file over and over until the last field of the last record so my data set ends up UG? Oh well. . .
Commented:
Oops...forgot one important piece of info:

<cfset admissions_file= "">

<cfloop query="freshman">
  <cfset admissions_file= admissions_file & "#Title#" & chr(9)>
  <cfset admissions_file= admissions_file & "#Name#" & chr(9)>
  <cfset admissions_file= admissions_file & "#Middle#" & chr(9)>
  <cfset admissions_file= admissions_file & "#Last#" & chr(9)>
  <cfset admissions_file= admissions_file & "#Address#" & chr(9)>
  <cfset admissions_file= admissions_file & "#Address2#" & chr(9)>
  <cfset admissions_file= admissions_file & "#City#" & chr(9)>
  <cfset admissions_file= admissions_file & "#State#" & chr(9)>
  <cfset admissions_file= admissions_file & "#Zip#" & chr(9)>
  <cfset admissions_file= admissions_file & "#Country#" & chr(9)>
  <cfset admissions_file= admissions_file & "#Email#" & chr(9)>
  <cfset admissions_file= admissions_file & "#Interest#" & chr(9)>
  <cfset admissions_file= admissions_file & "#Year#" & chr(9)>
  <cfset admissions_file= admissions_file & "#Date#" & chr(9)>
  <cfset admissions_file= admissions_file & "#ID#" & chr(9)>
  <cfset admissions_file= admissions_file & "#Degree#" & chr(10)>
</cfloop>

Sorry for the confusion.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
Hmm, if it consistently adds a blank line, could you just depend on that and take out your carriage return and use the one it's adding?

Like this:
<cfsetting enablecfoutputonly="yes"></head><body><cfsavecontent variable="admissions_file"><cfloop query="freshman"><cfoutput>"#Title#"     "#Name#"     "#Middle#"     "#Last#"     "#Address#"     "#Address2#"     "#City#"     "#State#"     "#Zip#"     "#Country#"     "#Email#"     "#Interest#"     "#Year#"     "#Date#"     "#ID#"     "#Trim(Degree)#"</cfoutput></cfloop></cfsavecontent>

Personally, I'd do this instead:
<cfset filename="filepath\freshman.#DateFormat(Now(),'mmddyy')#.txt">

(if you need the first line to have field names)
<cfset headers="Title     Name     Middle     Last     Address     Address2     City     State     Zip    Country     Email     Interest     Year     Date     ID     Degree">
<cffile action="WRITE" file="#filename#" output="headers" addnewline="Yes">

then the content
<cfloop query="freshman">
   <cfset outputLine="#Title#     #Name#     #Middle#     #Last#     #Address#     #Address2#     #City#     #State#     #Zip#    #Country#     #Email#     #Interest#     #Year#     #Date#     #ID#     #Degree#"> <!--- where the whitespace is the single tab delimiter. --->
   <cffile action="APPEND" file="#filename#" output="outputLine" addnewline="Yes">
</cfloop>

Author

Commented:
pmascari That worked. Wonderful!

8riaN I had either a choice of two carriage returns or none. Either one continuous record or a blank between each record so couldnt count on one being added
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.