Tuesday, May 5, 2015

c# - Call exe with incremental filenames as command line arguements

I work on projects involving data processing and more often than not, we end up processing files created over months.This requires that the data processing program is called over number of times.Instead of doing it manually I created this small utility program to automate the processing of these files.

Below I have listed the code for sequential/batch processing of files using a program as well as parallel processing of files using the same program.

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IncrementCmdLineArgs
{
    class Program
    {
        static void Main(string[] args)
        {
            int iStartCounter = int.Parse(ConfigurationManager.AppSettings["START_COUNTER"]);
            int iEndCounter = int.Parse(ConfigurationManager.AppSettings["END_COUNTER"]);
            string sInputFile = ConfigurationManager.AppSettings["INPUT_FILENAME"];
            string sOutputFile = ConfigurationManager.AppSettings["OUTPUT_FILENAME"];
            string sExeName = ConfigurationManager.AppSettings["EXE_NAME"];
            string sWorkingDir = ConfigurationManager.AppSettings["WORKING_DIR"];
            string sInputParam = ConfigurationManager.AppSettings["INPUT_PARAM_NAME"];
            string sOutputParam = ConfigurationManager.AppSettings["OUTPUT_PARAM_NAME"];

            try
            {
                for (int iCounter = iStartCounter; iCounter <= iEndCounter; iCounter++)
                {
                    Process oProc = new Process();
                    oProc.StartInfo.FileName = sExeName;
                    oProc.StartInfo.WorkingDirectory = sWorkingDir;
                    oProc.StartInfo.CreateNoWindow = true;
                    oProc.StartInfo.Arguments = string.Format(" {0}={1} {2}={3}", sInputParam, sInputFile + iCounter + ".txt", sOutputParam, sOutputFile + iCounter + ".txt");
                    Console.WriteLine(string.Format(" {0}={1} {2}={3}", sInputParam, sInputFile + iCounter + ".txt", sOutputParam, sOutputFile + iCounter + ".txt"));
                    oProc.Start();
                    oProc.WaitForExit();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            Console.Write("Done.Press any key to exit !");
            Console.Read();
        }
    }
}


The parallel version is below

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IncrementCmdLineArgs
{
    class Program
    {
        static void Main(string[] args)
        {
            int iStartCounter = int.Parse(ConfigurationManager.AppSettings["START_COUNTER"]);
            int iEndCounter = int.Parse(ConfigurationManager.AppSettings["END_COUNTER"]);
            string sInputFile = ConfigurationManager.AppSettings["INPUT_FILENAME"];
            string sOutputFile = ConfigurationManager.AppSettings["OUTPUT_FILENAME"];
            string sExeName = ConfigurationManager.AppSettings["EXE_NAME"];
            string sWorkingDir = ConfigurationManager.AppSettings["WORKING_DIR"];
            string sInputParam = ConfigurationManager.AppSettings["INPUT_PARAM_NAME"];
            string sOutputParam = ConfigurationManager.AppSettings["OUTPUT_PARAM_NAME"];

            try
            {
                 Parallel.For(iStartCounter, iEndCounter, iCounter =>
                {
                    Process oProc = new Process();
                    oProc.StartInfo.FileName = sExeName;
                    oProc.StartInfo.WorkingDirectory = sWorkingDir;
                    oProc.StartInfo.CreateNoWindow = true;
                    oProc.StartInfo.Arguments = string.Format(" {0}={1} {2}={3}", sInputParam, sInputFile + iCounter + ".txt", sOutputParam, sOutputFile + iCounter + ".txt");
                    Console.WriteLine(string.Format(" {0}={1} {2}={3}", sInputParam, sInputFile + iCounter + ".txt", sOutputParam, sOutputFile + iCounter + ".txt"));
                    oProc.Start();
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            Console.Write("Done.Press any key to exit !");
            Console.Read();
        }
    }
}

No comments: