Execution of batch jobs and other programs from Navision

Automations 4 Comments

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.cmd

Please be aware that you may be running an executable that could potentially
harm your computer

Do 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.

4 Responses to “Execution of batch jobs and other programs from Navision”

  1. Host Says:
    June 7th, 2008 at 8:41

    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.

  2. mark Says:
    April 16th, 2009 at 3:14

    I rarely comment on blogs but yours I had to stop and say Great Blog!!

  3. Debty Says:
    July 26th, 2009 at 22:05

    Ahaan… I will follow.

  4. Ignas Says:
    December 7th, 2010 at 13:21

    This is exactly what I was looking for :) Thanks.

Leave a Reply

You must be logged in to post a comment.

Icons by N.Design Studio. Designed By Ben Swift and Customized by Ingrid Byllemos. Powered by WordPress
Entries RSS Comments RSS Log in