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.

Reply Children