There are at least 2 ways of execution of external batch jobs and programs.
One way is to use the SHELL command another way is to use WshShell.
Lets start with looking at SHELL.
SHELL is a command that you can use directly in the C/AL code. It returns a “returncode” and takes name and “parameters” as parameter.
ReturnCode := SHELL(‘cmd.exe’,’/c’,’c:\mybatch.cmd’);
Where /c means execute my command and the close window (terminate).
You can also use /k which will leave the execution window open.
Be aware of, if you are using SHELL in Navision 4 (or higher) you will get a security message from Navision.
You are about to run the following executable for the first time
Executable: cmd.exe
Parameter: /c mybatch.cmdPlease be aware that you may be running an executable that could potentially
harm your computerDo you trust this executable and its parameter?
This message is ok in the situations where a user manually can accept the execution – but if you are running batchs jobs from a scheduler, this is not the ideal situation. Therefor in these cases I would recommend the use of WshShell – or you could always use WshShell, in that way you would never be shown the security warning 😉
So how do we use WshShell? WshShell is an automation, which I have earlier mentioned in the articles about SendKeys. You can execute a batch job by using the command Run.
IF ISCLEAR(WshShell) THEN
…. CREATE(WshShell);WshMode := 1;
WaitForEndOfCommand := TRUE;ReturnCode := WshShell.Run(‘cmd.exe /c c:\mybatch.cmd’,
……………………………………. WshMode,
……………………………………. WaitForEndOfCommand);WshShell is defined as ‘Windows Script Host Object Model’.WshShell
WshMode is an integer and is used to handle the Window Style (minimized, maximize etc.). Valid values can be found here.
WshShell requires a few more lines of codes than SHELL – but I think its worth the effort.
I searched for ‘Windows Script Host’ in google and found this your post (‘Execution of batch jobs and other programs from Navision’) in search results. Not very relevant result, but still interesting to read.
I rarely comment on blogs but yours I had to stop and say Great Blog!!
Ahaan… I will follow.
This is exactly what I was looking for 🙂 Thanks.