BCS Execute Batch Files Using BCSWinExecNWait

There are those time when it is necessary to execute a batch file from within Delphi. Also in many cases before further processes continue we need to make sure the batch file is finish.
The key to success in this endeavor is to execute a batch file you want to use cmd.exe as the program to execute with the /c Drive:\batfilepath\batfilename.bat parameter options.
sr in the example below contains the value of the ComSpec variable.  We use this approach to ensure success execution regardless of system type and system configuration.
[codesyntax lang=”delphi”]

    sr := GetEnvironmentVariable('ComSpec');

Once the sr variable is populated you can continue to execute the batch file by using the following commands.  Of course RExecute and RCommand are both properties with the type of string.
[codesyntax lang=”delphi”]

        BCSWinExecNWaitdp1.RExecute := sr;
    BCSWinExecNWaitdp1.RCommand := '/c ' + batpath + '\' + 'batfile.bat';

This combination of commands should produce the results you desire.
Mr. Arch Brooks, Software Engineer, Brooks Computing Systems authored this article.

1 comment
  1. slposey
    June 19, 2011 at 7:28 am

    Instead of using GetEnvironmentVariable() to retrieve “SystemRoot” (the path to the Windows folder) and appending ‘CMD.EXE’, I’d suggest retrieving the “ComSpec” variable instead, which will include the path and will account for systems where someone may be using an alternate command shell.

Leave a Reply

Your email address will not be published. Required fields are marked *