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

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 - 

Error After Restore SQL Backup of NAV 2013 Database

Hi all, we are facing a conman issue during restoring a SQL Database backup restored for NAV 2013. While Trying to run object from Object Designer we get below listed error (even the service is configured properly) - --------------------------- Microsoft Dynamics NAV Development Environment --------------------------- There are no NAV Server instances available for this database. You must ensure that a NAV Server instance is running and is configured to use the database before you perform this activity. --------------------------- OK    ---------------------------

Future - What Next After NAV 2016. Code Name - Madeira...

Hi All, While Browsing web I came across some article which says about next release of Microsoft Dynamics NAV. Let's see the History of Code Names Given to Previous Version  - NAV 2013 - Code Name 'Sicily' NAV 2015 - Code Name 'CRETE' NAV 2016 - Code Name 'CORFU' And as we all know that all these are the Islands.