Skip to main content

NAV 2013 & Later - How to Run a Batch File From NAV.

Hi All,

In Back Old Days we used to Run a Batch File From Navision Using Shell Command. The Shell Command is now discontinued with Release of Role Tailored Client.


Today we will discuss the possibility of same using Dotnet Command.

For the Demo i have create a .bat file named as autonav.bat. The bat file used in demo just rename a text file placed in C:\NAVDemo\.

The File Placed at above location is named as "agic ile". On Right Hand Side you can see  image shows the code in bat file and the files.


Now for Demo I am creating a Codeunit which will execute the batch file. Below are the variables that i require

Name
DataType
Subtype
ExecuteBat
DotNet
System.Diagnostics.ProcessStartInfo.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
Process
DotNet
System.Diagnostics.Process.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
Command
Text

Result
Text

ErrorMsg
Text













Where
ExecuteBat & Process are DotNet Variables.
Command Variable stores the Path of the Batch File.
Result and Error Message are the vairables to store output of the execution of command.

Let's see how we use all these.



As you can see in Code Above certain steps are taken.
Initialized the Command with the path of batch file.
Set Parameters for the ExecuteBatch Dotnet Variable, details in image.
Display the Error message if any or the complete command in Batch File.

CODE
Command := 'C:\NAVDemo\autonav.bat';
ExecuteBat := ExecuteBat.ProcessStartInfo('cmd', '/c "' + Command + '"');     //Provide Details of the Process that need to be Executed.

ExecuteBat.RedirectStandardError := TRUE;      // In Case of Error the Error Should be Redirected.

ExecuteBat.RedirectStandardOutput := TRUE;     // In Case of Sucess the Error Should be Redirected.

ExecuteBat.UseShellExecute := FALSE;

ExecuteBat.CreateNoWindow := TRUE;             // In case we want to see the window set it to False.

Process := Process.Process;                    // Constructor

Process.StartInfo(ExecuteBat);

Process.Start;

ErrorMsg := Process.StandardError.ReadToEnd(); // Check Error Exist or Not

IF ErrorMsg <> '' THEN

  ERROR('%1',ErrorMsg)

ELSE BEGIN

  Result := Process.StandardOutput.ReadToEnd();// Display the Query in the Batch File.

  MESSAGE('%1',Result);

END;
Now Let's try to execute the Codeunit and see the results.



** This image is for the Demo purpose you can remove the Else part of the ErrorMsg if you don't want the popup.

Let's check the folder to see our file is renamed or not.



Hope post help you during your projects. Your Comments are much awaited.

If required you can download the sample Codeunit and files used from my  SkyDrive.

File Name - NAV 2013 & Later Execute Batch File.rar

**  The Objects are developed on NAV 2013 R2 Version.
**  Do Keep the Extracted Folder in C Drive, or change the path in Codeunit and Batch File.

Thanks & Regards,
Saurav Dhyani
www.sauravdhyani.com

Comments

  1. PLS tell me what is code inside in magic file..
    thanks you

    ReplyDelete
    Replies
    1. Really it have nothing.
      The blog explains how you run Batch files or exe from NAV after RTC Client is introduced. Same code can be use to run a exe from NAV.
      Hope it make sense.

      Delete
  2. access is denied error is coming

    ReplyDelete
    Replies
    1. The Dynamics NAV Service account should have permission to run that file.

      Delete

Post a Comment

Popular posts from this blog

VIEW SERVER STATE permission on SQL Server?

Hi all, Sometime While trying to Login into a database we face an error message as shown below. --------------------------- Microsoft Dynamics NAV Classic --------------------------- You cannot start Microsoft Dynamics NAV Classic because you do not have the VIEW SERVER STATE permission on SQL Server. Contact your system administrator. --------------------------- OK    ---------------------------

RTC Report It is not possible to instantiate the Visual Studio bridge.

Hi all, As a Navision developers I have Multiple Versions of Navision running in single Machine. As discussed Earlier in the post how to run multiple Version of RTC in single machine. So my machine have following details for RTC Versions - NAV 2009 R2    - is installed. NAV 2009 SP1  - is copied at C:\Program Files (x86)\Microsoft Dynamics NAV\60\NAV 6.0 SP1 IN\ NAV 2009         - is copied at C:\Program Files (x86)\Microsoft Dynamics NAV\60\NAV 6.0 IN\ This approach has been working great for execution of Classic and RTC Clients. However, after installing Dynamics NAV 2009 R2, if i tried to view the Layout for an NAV 2009 SP1 Report i was getting the following error: ---------------------------  Microsoft Dynamics NAV Classic  ---------------------------  It is not possible to instantiate  the Visual Studio bridge.  ---------------------------  OK    --------------------------- After searchi...

NAV 2013 R2 - Cumulative update 12 Released.

Hi all, Please find below the details of  Cumulative Update 12 released for Microsoft Dynamics NAV 2013 R2. Title - Cumulative Update 12 for Microsoft Dynamics NAV 2013 R2 Build No. - 38053 Release Date - October, 2014 Local Version Included - AU, AT, BE, CH, DE, DK, ES, FI, FR, IS, IT, NA, NL, NO, NZ, SE, UK, RU Download Link Note: Implementing this cumulative update will require a database conversion unless you have already implemented update rollup 5.