Example #1
0
        public static void ReplaceOccurances(string path, string extension,
                                             string s, string r, ParallelLog log = null, int maxDegreeOfParallelism = 2)
        {
            List <string>   fileNames = GetListOfFiles(path, extension);
            ParallelOptions options   = new ParallelOptions();

            if (maxDegreeOfParallelism < 1)
            {
                throw new ArgumentException("Number of threads can't be " + maxDegreeOfParallelism);
            }
            Console.WriteLine("Invoking parallel");
            try
            {
                options.MaxDegreeOfParallelism = maxDegreeOfParallelism;
                Parallel.ForEach(fileNames, options, name => Replace(name, s, r, log));
                Console.WriteLine("Replaced");
            }
            catch (AggregateException exceptions)
            {
                Console.WriteLine(exceptions.Message);
                foreach (var exception in exceptions.Flatten().InnerExceptions)
                {
                    Console.WriteLine("\t{0}", exception.Message);
                }
            }
        }
Example #2
0
        public static void Replace(string fileName, string s, string r,
                                   ParallelLog log = null)
        {
            if ((fileName == null) || (fileName == ""))
            {
                throw new ArgumentException("Empty filename is not allowed");
            }
            FileInfo fileInfo = new FileInfo(fileName);

            if (!fileInfo.Exists)
            {
                throw new ArgumentException("File doesn't exist");
            }

            string tempFileName = "./" + fileInfo.Name + ".tmp";

            using (var input = File.OpenText(fileName))
            {
                using (var output = new StreamWriter(tempFileName))
                {
                    string line;
                    int    strNum = 0;
                    while (null != (line = input.ReadLine()))
                    {
                        string newLine = line.Replace(s, r);
                        if ((log != null) && (line != newLine))
                        {
                            log.Log(fileName, strNum, line, newLine);
                        }
                        output.WriteLine(newLine);
                        strNum++;
                    }
                }
            }
            File.Delete(fileInfo.FullName);
            File.Move(tempFileName, fileName);
        }