Click here to Skip to main content
15,914,924 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to find the NON ASCII characters from the files and replace them by similar looking Alphabets in text file. I have 2 text files which are looking like LS_PRSADT_*.txt and I have deployed a table with NON Ascii codes and similar looking characters . Now I am calling the SP and take the input(NON-ASCII) from file and replace them by similar looking alphabets. I have the following code . Where I am reading 2 text files from one one directory and I also need to save those two text files in the same location and also with the same names. Somehow It is not working properly. Please help

C#
static string CS = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString.ToString();
        static void Main(string[] args)
        {


            string date = DateTime.Today.ToString("yyyyMMdd");
            string date2=DateTime.Today.ToString("YYYYMMMDD");
            string dirPath =
            string quarintine =
            string filepattern1 = "LS_PRSADT_*.txt";
            string backupfile = Path.Combine(quarintine, date);
            string movefiles = Path.Combine(quarintine, date2);
            string[] filesDT = Directory.GetFiles(dirPath, filepattern1);
            string s1 = null;
            string s2 = null;

            Directory.CreateDirectory(backupfile); 
            filesDT.ToList().ForEach(k => File.Copy(k, Path.Combine(backupfile, Path.GetFileName(k))));

            
                for (int i = 0; i < filesDT.Length; i++)
                {


                    string file = filesDT[i];

                    FileInfo fileinfo = new FileInfo(file);
                    string content = File.ReadAllText(fileinfo.FullName);


                    foreach (char c in content)
                    {
                        if (c > 127)
                        {
                            using (SqlConnection con = new SqlConnection(CS))
                            {
                                con.Open();
                                SqlCommand sqlcom = new SqlCommand("sp_ReplaceNonAsciiCharset", con);
                                sqlcom.CommandType = CommandType.StoredProcedure;
                                SqlParameter sqlparam = new SqlParameter();
                                sqlparam.ParameterName = "@non_ascii_char";
                                sqlparam.Direction = ParameterDirection.Input;
                                sqlparam.SqlDbType = SqlDbType.NChar;
                                sqlparam.Size = 2;
                                sqlparam.Value = c;
                                sqlcom.Parameters.Add(sqlparam);
                                object o = sqlcom.ExecuteScalar();
                                int j = content.IndexOf(c);
                                s1 = content.Remove(j, 1);
                                s2 = s1.Insert(i, o.ToString());
                                content = s2;

                            }
                        }
                    }

                    filesDT[i] = content;
Posted
Updated 19-Jan-14 6:23am
v2
Comments
Bernhard Hiller 20-Jan-14 9:12am    
Oh, I like it really when I see a B in place of a German ß, oh yes, great!

1 solution

If you try to remove accents and umlauts, etc., then the problem is related to Diacritics[^].
Have a look at e.g. How do I remove diacritics (accents) from a string in .NET? [^].

Cheers
Andi
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900