Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV (Archived)

Launch Bat in Nav 2013

Posted on by 145

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

  • CristianAguerre Profile Picture
    CristianAguerre 145 on at
    RE: Launch Bat in Nav 2013

    Hi Tony Phan,

    Thaks for help but this is not the problem.

    Regards

  • CristianAguerre Profile Picture
    CristianAguerre 145 on at
    RE: Launch Bat in Nav 2013

    Hi Zaid Tariq ,

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

    Thanks a lot!

    Regards!

     

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Launch Bat in Nav 2013

    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

  • Verified answer
    Zaid Tariq Profile Picture
    Zaid Tariq 2,266 on at
    RE: Launch Bat in Nav 2013

    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

  • CristianAguerre Profile Picture
    CristianAguerre 145 on at
    RE: Launch Bat in Nav 2013

    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.

  • Suggested answer
    Zaid Tariq Profile Picture
    Zaid Tariq 2,266 on at
    RE: Launch Bat in Nav 2013

    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
    CristianAguerre 145 on at
    RE: Launch Bat in Nav 2013

    Hi Zaid,

    Always,

    Thx!

    Regards

  • Zaid Tariq Profile Picture
    Zaid Tariq 2,266 on at
    RE: Launch Bat in Nav 2013

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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans