Skip to main content
January 23, 2019
Answered

CF11 cffile write failing on a few files with no errors

  • January 23, 2019
  • 3 replies
  • 955 views

I inherited some order entry websites that have worked seemingly flawlessly for the past 3 years. Today I learned that 3 files didn't get sent to a customer last week, and I can confirm these files they should have gotten were never created on the server. However, I can find no entry in any of the log files (C:\ColdFusion11\cfusion\logs) that would indicate what the issue was. I would have expected some kind of exception to occur if a file could not be created or is that not that case? This part of the code does not contain any cftry / cfcatch blocks. I'll include the relevant parts here:

<cfset OrderPath = #GetTempDirectory()#> 

<cfset DateTime = DateFormat(now(),"yyyymmdd") & "_" & TimeFormat(now(),"hhmmssllll")>

<cfset OrderFileName = ("WB" & "_" & #DateTime# & ".ORD")>

<!--- Check if order file exists - if so, delete it --->

<cfif FileExists(#GetTempDirectory()#&#OrderFileName#)>  

  <cffile action="delete" file="#GetTempDirectory()##OrderFileName#">

</cfif>

<cfset HeaderRec ="H#x2tab##x2Cust##x2tab##x2OrdDate##x2tab##x2ContactName##x2tab##x2PO##x2tab##x2SubTotal##x2tab##x2PST##x2tab##x2GSTHST##x2tab##x2Total##x2tab##x2Comments##x2tab##x2ShipToAddr1##x2tab##x2ShipToAddr2##x2tab##x2ShipToCity##x2tab##x2ShipToProvince##x2tab##x2ShipToCountry##x2tab##x2ShipToPostal##x2tab##x2ShipToDate#"> <!--- I didn't include all those cfsets but if nothing else the first H would always be written, right? --->

<!--- Write Header record to file --->

<cffile action="write"

file="#GetTempDirectory()##OrderFileName#"

output="#HeaderRec#">

<cfloop from="1" to="#ArrayLen(Session.Cart.ItemID)#" index="ThisItem">

     <cfset DetailRec ="D#x2tab##x2Item##x2tab##x2Qty##x2tab##x2Price##x2tab##x2Mask##x2tab##x2UOM##x2tab##x2PriceOver#"> <!--- similar to the HeaderRec --->

       <!--- Write Detail record to file --->

     <cffile action="append"

     file="#GetTempDirectory()##OrderFileName#"

     output="#DetailRec#">

</cfloop>

<cfset source = trim(#OrderPath#) & trim(#OrderFileName#)>

<cfset dest = trim(#Session.ftpdataoutdir#)>

<cffile action="copy" source="#source#" destination="#dest#">

<cflocation url="thankyou.htm" addtoken="no">

The thankyou.htm page includes an email order confirmation which they did receive so nothing prevented the page from processing all the way to the end. However, going to C:\ColdFusion11\cfusion\runtime\work\Catalina\localhost\tmp, which is where all of our files are written initially, these few files don't exist while hundreds of others that continue to be written there. How can I tell why these 3 files were not written so I can prevent it in the future?

    This topic has been closed for replies.
    Correct answer

    Turns out this was a false alarm. We have 2 identical servers and somehow they connected to the secondary one for no reason so the files were on it.

    Thanks for the tips though.

    3 replies

    Correct answer
    January 23, 2019

    Turns out this was a false alarm. We have 2 identical servers and somehow they connected to the secondary one for no reason so the files were on it.

    Thanks for the tips though.

    BKBK
    Community Expert
    Community Expert
    January 23, 2019

    Question: is it your intention not to use the loop index ThisItem within the loop?

    January 23, 2019

    ThisItem is used many times within the loop, but just like I had commented on HeaderRec (see below) I didn't include all the cfset statements used for DetailRec since they aren't relevant to the issue.

    <!--- I didn't include all those cfsets but if nothing else the first H would always be written, right? --->

    As for the # symbols, this is how it was already coded so I had no reason to change it since it works.

    WolfShade
    Legend
    January 23, 2019

    topshotrhit  wrote

    As for the # symbols, this is how it was already coded so I had no reason to change it since it works.

    Using hashtags where they are not needed can impact performance.  Also, as BKBK pointed out, not using hashtags in code where you don't need them makes code easier to read.

    Most CF sites/apps will have hundreds of .cfm/cfml documents, which makes it difficult to weed through and remove the unnecessary pound signs.  But it would generally be a good idea to at least start doing just that.  You won't get it all corrected in one day, it could take weeks, but it could speed things up.

    V/r,

    ^ _ ^

    BKBK
    Community Expert
    Community Expert
    January 23, 2019

    I find it hard to read the many # symbols. You could make your code more efficient, hence more maintainable, as follows:

    <cfset OrderPath = getTempDirectory()>

    <cfset DateTime = dateFormat(now(),"yyyymmdd") & "_" & TimeFormat(now(),"hhmmssllll")>

    <cfset OrderFileName = ("WB" & "_" & DateTime & ".ORD")>

    <cfset tempOrderFile = OrderPath & OrderFileName>

    <!--- Check if order file exists - if so, delete it --->

    <cfif fileExists(tempOrderFile)>

      <cffile action="delete" file="#tempOrderFile#">

    </cfif>

    <cfset HeaderRec ="H" & x2tab & x2Cust & x2tab & x2OrdDate & x2tab & x2ContactName & x2tab & x2PO & x2tab & x2SubTotal  & x2tab & x2PST & x2tab & x2GSTHST & x2tab & x2Total & x2tab & x2Comments & x2tab & x2ShipToAddr1 &  x2tab & x2ShipToAddr2 & x2tab & x2ShipToCity & x2tab & x2ShipToProvince & x2tab & x2ShipToCountry & x2tab & x2ShipToPostal & x2tab & x2ShipToDate>

    <!--- Write Header record to file --->

    <cffile action="write"

            file="#tempOrderFile#"

            output="#HeaderRec#">

    <cfloop from="1" to="#ArrayLen(Session.Cart.ItemID)#" index="ThisItem">

        <cfset DetailRec = "D" & x2tab & x2Item & x2tab & x2Qty & x2tab & x2Price & x2tab & x2Mask & x2tab & x2UOM & x2tab & x2PriceOver>

          <!--- Write Detail record to file --->

        <cffile action="append"

                file="#tempOrderFile#"

                output="#DetailRec#">

    </cfloop>

    <cfset source = tempOrderFile>

    <cfset dest = trim(Session.ftpdataoutdir)>

    <cffile action="copy" source="#source#" destination="#dest#">

    <!--- Check if order file exists - if so, then redirect to thankyou page --->

    <cfif fileExists(tempOrderFile)>

      <cflocation url="thankyou.htm" addtoken="no">

    <cfelse>

    <!--- You might want to include code here to inform the user something went wrong --->

    </cfif>