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

BC 21 and Higher - PowerShell Cmdlet (Replacement of Business Central Administration).

Hi Readers, As discussed in last article about deprecating of Business Central Administration, there are few common actions that we use in administration till Business Central 20. For our on-prem customers, we will still require doing activities. As Microsoft suggest we need to start using PowerShell cmdlet.    Let's see how to do those via PowerShell, or Administration Shell. I will be keep adding commands as you comment to this article.

Send Mail with Attachment From Navision.

Hi all, We have seen how to save a report into PDF and how to send mail to a customer. Let's link these two post in one i.e. Mailing statement to a customer into PDF Format. This article is part of the Series. Please Refer  Table of Content here . If you have the old objects set let me brief you what I will be changing - 

MSDYN365BC - Data Upgrade To Microsoft Dynamics 365 Business Central on premises.

Hi Readers, We have already talked about the number of steps for upgrading to Business Central on Premises from different NAV versions. After that article, I received multiple requests for an article which list down steps for Data Migration. In this article, we will discuss steps of data migration to MSDYN365BC (on-Prem) from NAV 2017. For this article, I am considering a Cronus Demo Database without any customization. For an actual upgrade project, we will have to complete object merge using compare and Merge process. After the Merge Process, the next step is data migration. Let's discuss those steps. Direct Upgrade to Microsoft Dynamics 365 Business Central (on-Prem) is from following versions - 1. NAV 2015. 2. NAV 2016. 3. NAV 2017. 4. NAV 2018.