Run a bat file with argument via server method

Hello Everyone,

I would like to run a bat file with argument on server via server method. I saw the same topic on forum in several year ago but they also have no solution.

https://community.aras.com/f/archive/2847/developers-forum---trying-to-execute-custom-batch-file-through-aras

Is it possible to run a bat file (or an application) on server via server method? Pls help.

Parents
  • Hi DucTran

    You can use below server method to run the bat file 

    string inputPath = string.Format(@"C:\Backup\Test");   //Directory of the Bat file
    System.Diagnostics.Process command = new System.Diagnostics.Process();
    command.StartInfo.FileName = Path.Combine(inputPath, "Hello.bat"); // Bat file Name
    string outputPath = Path.Combine(inputPath, "output.txt"); //Output file
    command.StartInfo.Arguments = string.Format("\"{0}\" \"{1}\" \"{2}\"", inputPath, outputPath, inputPath);
    command.StartInfo.UseShellExecute = false;
    command.StartInfo.CreateNoWindow = true;
    command.StartInfo.WorkingDirectory = inputPath;
    command.Start();
    if (!command.WaitForExit(3000)) //Execution allowed time to prevent system hung
    {
    throw new Exception(string.Format("The process '{0}' is not responding", command.StartInfo.FileName));
    }

    if (command.ExitCode != 0)
    {
    throw new Exception(string.Format("The process '{0}' has finished with error (ErrorCode={1})", command.StartInfo.FileName, command.ExitCode));
    }
    return this;

    Note: If you get access denied error, provide access to that folder to everyone an try

    Thank You

    Gopikrishnan R

  • I have tried your code, it's go to the end of line (return this;) fine without exception. but bat file is not execute, the output.txt is not created.

    I am sure the folder of bat file on server is shared for everyone.

  • Hi DucTran

    Hope you have modified the directory, bat file name in above method. If it is executed, might be the bat file executed correctly. Can you confirm what is there in the bat file. If you want to test whether the method is working or not, you can keep below lines in bat file and run

    Open Notepad

    Copy below line and save as Hello.bat

    @echo off
    @echo Testing Bat File Operation from ARAS Innovator> output.txt

    Copy this bat file to the input directory as per the method

    Execute the method

    Verify the directory provided in the method for output

  • I Tried run server method with your Hello.bat, It's worked fine.

    The problem is the my bat file :) . Thanks for your support!

Reply Children
No Data