web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics NAV (Archived)

Launch Bat in Nav 2013

(0) ShareShare
ReportReport
Posted on by 147

Hi all,

I'm trying to execute bat in Nav 2013 in a page action.

I follow the article of @Saurav Dhyani (https://community.dynamics.com/nav/b/sauravdhyanimicrosoftdynamicsnav/archive/2015/08/30/nav-2013-amp-later-how-to-run-a-batch-file-from-nav).

This link explains perfect step by step how to do it. But when i launch the action page the client freeze and not responding.

In my case for do test i do it a more simple bat file, only set a echo instruccion:

@echo off
echo Bat Test.
pause

And the code in C/AL:

Command := 'C:\CAC\test.bat'
ExecuteBat := ExecuteBat.ProcessStartInfo('cmd','c/ "' + Command + '"');

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;

I'm trying to change some option but always frezee the client freeze.

Any idea ??

Thanks!

Regards

*This post is locked for comments

I have the same question (0)
  • Zaid Tariq Profile Picture
    2,274 on at

    Is there any particular situations when this happens or every time you run this?

  • CristianAguerre Profile Picture
    147 on at

    Hi Zaid,

    Always,

    Thx!

    Regards

  • Suggested answer
    Zaid Tariq Profile Picture
    2,274 on at

    Well this happens normally due to following conditions:

    - The command is invalid

    - If you are doing any I/O operation then the file is not closed or already in use by another process.

    - Process is not disposed and you are are calling the same process (Use Process.dispose at the end)

    My Suggestions:

    - Use ExecuteBat.CreateNoWindow := FALSE; because you are using Pause in your CMD command.

    Please check event viewer, might be you will be able to see any error. Thanks

  • CristianAguerre Profile Picture
    147 on at

    Hi Zaid Tariq,

    First thank's of the possible solution.

    -The command i'm using for the test only have a echo instruction.

    -I'm not doing any operation of files

    -The process isn't in any bucle only is called once in the action.

    -I'm change the value of the property ExecuteBat.CreateNoWindow to false and not work's. When i push the action button the client freeze.

    Adding information of error:

    Its strange but I'm create a MESSAGE() before call the function that call's the .bat and the client freeze before show the message.... The code:

    bat-errror.PNG

    Tranks!

    Regards.

  • Verified answer
    Zaid Tariq Profile Picture
    2,274 on at

    I guess you would understand better if i try to explain technically.

    What happening here is when you run this code it does not actually run a new thread to run this bat file. Your command is saying that display Bat Test.  and the Pause command means that Wait until the user presses and key to continue. Now here is the problem, while you cmd/shell/bat file  is waiting for your input to continue you click on the RTC and then it freezes because that process is not yet closed and still waiting for your input. 
    Now the question could be, ah what if I would give the input of any random key then will it close and  message Bat end will be displayed? Then my answer would be YES.
    But how would you enter the input as you are not able to see the black CMD window? There are couple of reasons as 
    - First this line of code: ExecuteBat.CreateNoWindow := TRUE; // In case we want to see the window set it to False.
    - The Property RunAsClient of your DotNet variable is set as No. Due to this your service tier executes your CMD/Shell command and if a program/process is run through service output is not displayed and run as background.

    Suggestions:
    ExecuteBat.CreateNoWindow := FALSE;
    RunAsClient : =Yes
    - If dont want to do any of then Change your command and try something like copying file from one location to another. 

    Hope you got my point but can ask if it still making trouble. Thanks

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi Cristian,

    I'm not sure if you know the C# language or not but C/AL is compiled into C# so your command C:\CAC\test.bat will break it unless you use an escape character.  I suspect this is your case, try C:\\CAC\\test.bat

  • CristianAguerre Profile Picture
    147 on at

    Hi Zaid Tariq ,

    Thanks, the problem was the property RunOnClient, i forget set to True....

    Thanks a lot!

    Regards!

     

  • CristianAguerre Profile Picture
    147 on at

    Hi Tony Phan,

    Thaks for help but this is not the problem.

    Regards

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics NAV (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans