Solved

writing a file

Posted on 2000-03-31
4
148 Views
Last Modified: 2013-12-24
I need to write some code which will allow a client to query a database and then write the results of the query into a text file (csv) to be placed in the same folder as the database on the server.

The query is easy, but how do I write the text file.

Thanks for the help.
0
Comment
Question by:RICHARDH
  • 2
4 Comments
 

Expert Comment

by:bdemarzo
ID: 2674444
Use CFFILE to write to the text file.

<CFFILE ACTION=WRITE FILE="filename" OUTPUT="output text">

You first need to create the output text, best done in a variable. Let's say you query a database as follows:

<cfquery name="myquery" ...>
SELECT field1, field2
FROM mydbf
</cfquery>

Then create the variable with your CSV text:

<cfoutput query="myquery">
<cfset myoutput=myquery.field1 & "," & myquery.field2>
</cfoutput>

Finally, add the CSV file to your text file:

<cffile action="write" file="c:\myfile.csv" output=#myoutput#>

That should do it! Note that if you want to APPEND to a text file rather than overwriting it, use action="append" in the CFFILE tag.
0
 

Accepted Solution

by:
bdemarzo earned 50 total points
ID: 2674446
And of course, that's my final answer, Regis...
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 2674473
basically do this:

<cfset filename = "/pathtodb/text.csv">
<cfloop query="queryname">
<cfset Result= "#queryname.field1#" & "," "#queryname.field2#" & "," "#queryname.field3#">
<cffile action="APPEND" file="#filename#" output="#Result#" addnewline="Yes">
</cfloop>

If you don't know the column names or field names then do this:
<cfloop index="i" list="queryname.columnlist" delimiters=",">
<cfset Result = "#Result#" & "#queryname[index]#" & ","
</cfloop>
chop of the last ","

I am not sure if the queryname[index]
is right but I can find out the exact syntax if you need it.

CJ
0
 

Author Comment

by:RICHARDH
ID: 2674565
Thanks to both people for the answers. they seem quite different, will they both do the same thing.

thanks to both of you.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question