Using <cfexecute/> with Windows Batch Files

ColdFusion , Gotchas , Windows Add comments

Recently I wrote some batch files to complement some scheduled tasks I was developing to help with automating some processes that were being carried out manually by system administrators on applications deployed in a Microsoft Windows environment.

In doing so I came across a number of minor issues that I had to figure out, so I thought I'd share them here for anyone else who's ever required to do anything similar.

I was calling the batch file using the <cfexecute /> tag and passing some arguments to keep the script cohesive enough that its functionality could be used on other servers.

The code final code I used is below but I have discussed the important parts that I had left out.

<cftry>
    <cfscript>
        args = [1];
        args[1] = "argument1Value";
        args[2] = "argument2Value";
        args[3] = "argument3Value";
    </cfscript>
    <cfexecute variable="batchScriptOutput" 
	              name="#APPLICATION.BATCH_SCRIPT_DIRECTORY#\#APPLICATION.NAME_OF_SCRIPT#" 
				  arguments="#args#" 
				  timeout="99999" 
				  errorVariable="batchScriptError" 
	/>
    <cfoutput>
         SCRIPT OUTPUT:<br />#batchScriptOutput#<br />
         ERRORS:<br />#batchScriptError#<br />
    </cfoutput>
	<cfcatch>                
	    <cfrethrow />        
	</cfcatch>
</cftry>

The issues I had were that:

  • I wanted to pass argument values that had spaces in them
  • I wanted to see the output generated by the batch file

Neither of this seemed possible without formatting the code as I have above.  If a "timeout" value wasn't set, I didn't get the output returned and it just seemed simpler to pass the arguments as an array.

If anyone has had any similar experiences or better methods of implementation, I would like to hear your thoughts.

Bookmark and Share

3 responses to “Using <cfexecute/> with Windows Batch Files”

  1. Eapen Says:
    Another approach is to call the batch file with a .vbs script. I was using an SFTP command line client some time back and it always seemed to hang when trying to call it using a batch file or directly (it was waiting for input on the command line). So, I ended up CFEXECUTE-ing a .vbs script on the command line and it worked great.

    <code>
       <cfset exec = 'C:\windows\system32\cmd.exe'>   
       <cfset args = '/c "#serverPath#ftp.vbs"; 2>&1'>

       <cfexecute timeout="15" name='#exec#'
          arguments='#args#'
          outputFile="#serverPath#\ftp.log"></cfexecute>;
    </code>
  2. spills Says:
    Yep passing args in win batch files. The way I handled similar issue can be seen here: http://cookbooks.adobe.com/post_How_to_execute_a_Windows__bat_file_-16396.html
  3. Niall Says:
    @Eapen - Hi yeah I used to write VBS files for parsing EMail Attachments from Exchange but I felt that the batch script was "fit-for-purpose" on this occasion - fingers crossed I don't experience any issues!

    @spills - Hi, interesting, I suppose it's a matter of preference whether you wish to pass your arguments "inline" or predefine them in an array.

    Thanks for sharing ;-)

Leave a Reply

Leave this field empty:

Powered by Mango Blog. Design and Icons by N.Design Studio