Forum Discussion

DucTran's avatar
DucTran
Ideator I
6 years ago
Solved

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.

  • 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

7 Replies

  • 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

    • DucTran's avatar
      DucTran
      Ideator I

      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.

      • Gopikrishnan's avatar
        Gopikrishnan
        Ideator I

        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

    • David_Brook's avatar
      David_Brook
      Ideator I

      Hi, I'm running the identical code with python executable and script and I can't get it to work. 

      It works in console.application.  I added permission for Everyone and Users.

    • David_Brook's avatar
      David_Brook
      Ideator I

      Hi I have the same issue with trying to run python executable and script.  It runs in the console fine.

      I tried adding permissions for Everyone and Users.