Monday, August 31, 2015

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.
Intalized 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
saurav-nav.blogspot.com

No comments:

Post a Comment