Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
C#
static void Main(string[] args)
        {
            string readFile = @"C:\Log\*.txt";
            string writeFile = @"D:\DataLocate_PR_id.csv";
            Creating_CSV_File(readFile, writeFile);
        }

        private static void Creating_CSV_File(string readFrom, string writeTo)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                using (StreamReader sr = new StreamReader(readFrom))
                {
                    string delimiter = ";";
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] columns = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        sb.AppendLine(string.Join(delimiter, columns));
                    }
                }
                File.WriteAllText(writeTo, sb.ToString());
            }
            catch(Exception ex)
                {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
                }
            }


What I have tried:

Current code getting error(Illegal characters in path), this is because I give * to read all .txt, but its need specific .txt file only, but I want it to read all .txt files.
Posted
Updated 26-May-16 20:24pm
Comments
PIEBALDconsult 26-May-16 23:15pm    
Have a look at System.IO.Directory.EnumerateFiles
https://msdn.microsoft.com/en-us/library/dd413233(v=vs.110).aspx

if you want to read all .txt file, then read it separately using Directory class object and then read file one by one
see trailing code
C#
string[] array2 = Directory.GetFiles(@"C:\", "*.txt");

	// get all files and read them one by one.
	foreach (string name in array1)
	{
	    //your 'Creating_CSV_File' function goes here
	}
 
Share this answer
 
v2
 
Share this answer
 

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