Tuesday, December 4, 2018

CSV file replace with a datatble functionality


        private int CSVReplace(DataTable dtDictionary, string path, bool chkCase)
        {
            int totalcount = 0;
            try
            {
                string[] input = File.ReadAllLines(path);
                string inputLine = "";
                for (int i = 0; i < dtDictionary.Rows.Count; i++)
                {
                    if (dtDictionary.Rows[i].ItemArray[0].ToString().Trim() != "")
                    {
                        for (int k = 0; k < input.Count(); k++)
                        {
                            StringReader reader = new StringReader(input[k]);
                            List<List<string>> data = new List<List<string>>();
                            string[] inputArray = { };
                            while ((inputLine = reader.ReadLine()) != null)
                            {
                                if (inputLine.Trim().Length > 0)
                                {
                                    inputArray = inputLine.Split(new char[] { '|' });
                                    for (int j = 0; j < inputArray.Count(); j++)
                                    {
                                        int count = 0;
                                        if (chkCase)
                                        {
                                            if (inputArray[j] == dtDictionary.Rows[i].ItemArray[0].ToString().Trim())
                                            {
                                                count = count + 1;
                                                totalcount = totalcount + count;
                                                if (input[k].Contains(dtDictionary.Rows[i].ItemArray[0].ToString().Trim()))
                                                {
                                                    input[k] = input[k].Replace(dtDictionary.Rows[i].ItemArray[0].ToString().Trim(), dtDictionary.Rows[i].ItemArray[1].ToString().Trim() + ".");
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if (inputArray[j].ToLower() == dtDictionary.Rows[i].ItemArray[0].ToString().Trim().ToLower())
                                            {
                                                count = count + 1;
                                                totalcount = totalcount + count;
                                                if (input[k].Contains(dtDictionary.Rows[i].ItemArray[0].ToString().Trim()))
                                                {
                                                    input[k] = Regex.Replace(input[k], dtDictionary.Rows[i].ItemArray[0].ToString().Trim(), dtDictionary.Rows[i].ItemArray[1].ToString().Trim() + ".", RegexOptions.IgnoreCase);
                                                }
                                            }
                                        }
                                        if (count > 0)
                                        {
                                            File.WriteAllLines(path, input);
                                            Log.WriteLog(Path.GetFileName(txtTarget.Text) + " target file replaced with " + Path.GetFileName(txtDictionary.Text) + " : Replaced " + count + " entries from " + dtDictionary.Rows[i].ItemArray[0].ToString() + " to " + dtDictionary.Rows[i].ItemArray[1].ToString());
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog("CSVReplace at Replace" + ex.Message);
            }
            return totalcount;
        }

No comments:

Post a Comment