Running a batch file or command prompt from ATEasy

Knowledge Base Article # Q200173

Read Prior Article Read Next Article
Summary This article shows some example of how to use the WinExec procedure to run a command prompt commands or batch files.
  
Login to rate article
The procedure we will be using for calling our batch files is the ATEasy internal function WinExec.  Using WinExec causes ATEasy to run a batch file or an executable as an independant child process.

To run a batch file, simply reference it in the WinExec procedure call.  The batch file is looked for in several places including the directory in which the project resides.  The list of locations and their priorities is located in the ATEasy help files.

! default directory is the directory of execution
WinExec("test.bat")


To clear up any filename ambiguity, you can reference a specific location for the batch file.  You can enter the full directory location, but since you are writing a string, you must use escape characters where appropriate (e.g C:\test.bat becomes "C:\\test.bat")

! run batch file in non-default directory
WinExec("C:\\test.bat")


When specifying a location, directories with spaces require quotes around them (e.g. C:\My Documents\test.bat becomes "\"C:\\My Documents\\test.bat\"")

! run batch file in non-default directory (spaces in folder name)
WinExec("\"C:\\My Documents\\test.bat\"")


In these last examples your batch files appeared on the screen while they ran.  To get the files to execute in the background, you must pass an optional parameter into your procedure call.

! batch file runs in background
WinExec("test.bat", aformShowHide)


So far we have run the batch files directly.  For your purposes, you may want to run your executables through the command prompt.  Running the following command opens the command prompt.

! call cmd prompt
WinExec("cmd")


Or, to add some more functionality.  We will open the command prompt and run our batch file.  Using the /k modifier with cmd.exe causes the window to stay open after completing it's task.

! call cmd prompt to run a batch file and remain open
WinExec("cmd /k test.bat")


Alternately, we can use the cmd.exe modifier /c to have the command prompt close after execution and the ATEasy parameter aFormShowHide to have it run in the background.

! call cmd prompt to run a batch file silently and close itself.
WinExec("cmd /c test.bat", aformShowHide)


Finally, to allow a bit of end-user interaction, we could create a form with a textbox and allow the user to enter the name of the file that needs to be run and any additional parameters.  In this example, the name of the textbox would be changed to tbUserInput.  On a button click, the user input would be inserted into the WinExec command call as follows.

! call a cmd prompt to run a file based on user input
WinExec("cmd.exe /k " + tbUserInput.Text)


The WinExec function launches the command console as a child process and continues the execution of the ATEasy program in parallel with the command console.  If you wish for your program to pause and wait until the command console is close, the following example can be used:

Procedure CommandTest() : Void
h : AHandle
{
     ! assign the window to the AHandle variable
     h=WinExec("cmd")  

     ! wait for the window to close
     WaitForSingleObject(h)
}

Article Date 12/23/2009
Keywords WinExec, DOS, batch, command, prompt


Login to rate article

Read Prior Article Read Next Article
>