Skip to main content
Participant
January 29, 2007
Question

<cffile action="write" to a txt file

  • January 29, 2007
  • 10 replies
  • 775 views
Hi

I'm using cffile to write to a text file, I'm looping over varibles adding addtional text. I what to have each line of the loop on seprate lines within the text file. What am I missing?

<cfloop from="1" to="10" index="count">
<cfset content = content & "blah blah blah" & count>
</cfloop>

<cffile action = "write" file = "thisfile.txt" addnewline="yes" output = "#content#">
    This topic has been closed for replies.

    10 replies

    Inspiring
    January 29, 2007
    Is that something new? I originally worked on this under CF 6 (or maybe
    even 4.5!)

    IIRC I have been able to do this since 4.5. It has always been a matter
    of changing the 'USER LOG ON AS' property of the ColdFusion Windows
    Service, this service used to be something like "Cold Fusion Application
    Service" I believe, now it usually is something like "Macromedia CFMX AS
    ????". At least on my current "Multi-home" configuration.

    When you change the NT user that ColdFusion uses from the default
    "localSystem" to some specific user, you can then provide that user with
    normal NT read/write/update/ect. permissions to various NT domain
    resources. Then ColdFusion will have access to those permissions.

    I don't know how this works with non-windows networks, but I presume
    there is some mechanism for it.
    tclaremont
    Inspiring
    January 29, 2007
    Is that something new? I originally worked on this under CF 6 (or maybe even 4.5!)
    Inspiring
    January 29, 2007
    Note that you cannot use the CFFILE function to write to a location that
    is NOT on the server. You cannot write to an end user's machine for
    example, or even to another server. The path and file are relative to
    the server. So, you could use something like
    c:\directory\subdirectory\filename.txt," which would relate to the C:\
    drive on the server.


    Actually you can use CFFILE to write to other resources over the
    network, if it has been configured to do this. By DEFAULT it is
    configure to only access the local box, but if the CF service is
    assigned a NT user account that has permissions to read and\
    or write to other network resources, then CF can use CFFILE to do what
    it has been given permission to do.

    We do this all the time with code like this:

    <CFFILE .... file="\\NASSERVER\...\...\aFile.xsl">
    tclaremont
    Inspiring
    January 29, 2007
    Note that you cannot use the CFFILE function to write to a location that is NOT on the server. You cannot write to an end user's machine for example, or even to another server. The path and file are relative to the server. So, you could use something like "c:\directory\subdirectory\filename.txt," which would relate to the C:\ drive on the server.
    January 29, 2007
    Notice tclaremont's use of APPEND. That keeps you from overwriting.
    tclaremont
    Inspiring
    January 29, 2007
    I agree with the idea of combining the chr(13) &
    chr(10), I was merely trying to document what I was doing so it made more sense.

    We have to do things like this for health information we submit to the state, which uses an old mainframe application to read strictly formatted submission files. What fun...
    Inspiring
    January 29, 2007
    <CFSET CurrentRowToWrite = "whatever"

    <CFSET CurrentRowToWrite = '#CurrentRowToWrite#' & chr(13)><!--- Add
    carriage
    return ---->
    <CFSET CurrentRowToWrite = '#CurrentRowToWrite#' & chr(10)><!--- Add
    line
    feed ---->

    Personally I would write this as <cfset CurrentRowToWrite & chr(13) &
    chr(10)>

    PS. Note, if one views this file in a browser the browser will NOT SHOW
    the linefeeds and carriage returns. They are there, but the HTML
    standard says to ignore them.
    tclaremont
    Inspiring
    January 29, 2007
    Here is what works for me:


    <CFSET CurrentRowToWrite = "whatever">

    This adds a carriage reutrn:
    <CFSET CurrentRowToWrite = '#CurrentRowToWrite#' & chr(13)>

    This adds a line feed:
    <CFSET CurrentRowToWrite = '#CurrentRowToWrite#' & chr(10)>

    This appends the variable to the end of the text file:
    <pre><cffile action="APPEND" charset="US-ASCII" file="#FileLocation#" output="#CurrentRowToWrite#" addnewline="No"></pre>


    The above adds a carriage return and then a line feed to a variable, and then appends that variable to the end of an existing file.
    Coogie84Author
    Participant
    January 29, 2007
    <cfloop from="1" to="10" index="count">
    <cfset content = content & "blah blah blah" & count & "blah" & chr(10)>
    </cfloop>


    Doesn't seem to work.
    Inspiring
    January 29, 2007
    chr(10)