Search code examples
batch-filecoldfusioncoldfusion-10

CFFILE not reading a file that's been manipulated by server's batch file?


the server is windows.

what I'm trying to do is create a batch that will run a program that outputs to a temp file which then I will read. I am able to create this batch and as far as I know everything works until I get back to using cffile to read the 'changed' file... in which case it seems like nothing as changed. However, if I were to actually open the file via windows or ftp explorer I can see it has been changed.

please see example code below:

<cfset path = "D:/tests/" />
<cfset tempfile_  = GetTempFile(path, "test_")>
<cfset tempfile_batch = getTempFile(path, "testBatch_")>

<cffile action="rename"
        destination="#replaceNocase(tempfile_batch, '.tmp', '.bat')#"
        source="#tempfile_batch#" />
<cfset tempfile_batch = replaceNocase(tempfile_batch, '.tmp', '.bat') />



<cfset batchContents = '' />
<cfsavecontent variable="batchContents">
    @echo off
    <cfoutput>
        echo "this is a test" > "#tempfile_#"
    </cfoutput>
    exit
</cfsaveContent>        



<cffile
    action="write"
    file="#tempfile_#"
    output='init text' />

<cffile
    action="write"
    file="#tempfile_batch#"
    output='#batchContents#' />

<cfexecute name="#tempfile_batch#" />


<cffile action="read" file="#tempfile_#" variable="foo">

<cfoutput>
fileName: #tempfile_# <br />

result is... 
#foo#
</cfoutput>

when I actually open up the temp file the contents are "this is a test" while #foo# is "init text"

any idea why?


Solution

  • By default, cfexecute fires off the process, but does not wait for it to finish. So you may be trying to read the file before it is updated. Add a timeout to your cfexecute call and it should fix the problem.

    timeout - Length of time, in seconds, that ColdFusion waits for output from the spawned program.

    • 0 (default): equivalent to nonblocking mode.
    • A very high value: equivalent to blocking mode.