Solved

writing a file

Posted on 2000-03-31
4
139 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
Comment Utility
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
Comment Utility
And of course, that's my final answer, Regis...
0
 
LVL 19

Expert Comment

by:cheekycj
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now