Link to home
Create AccountLog in
Avatar of rragledavis
rragledavis

asked on

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

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>
Avatar of pmascari
pmascari
Flag of United States of America image

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">
Avatar of rragledavis
rragledavis

ASKER

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. . .
ASKER CERTIFIED SOLUTION
Avatar of pmascari
pmascari
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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>
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