Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All
I have local folder with sub-folder files read that Files and Export those files into CSV using Console Application

C#
static void Main(string[] args)
        {

            string[] Filepath = Directory.GetFiles(@"E:\1150\1150", "*.*", SearchOption.AllDirectories);
            // converting Stream string
               StreamReader streamReader = new StreamReader(filePath);
                string text = streamReader.ReadToEnd();
               streamReader.Close();


           }
        }
        static void GetFiles(string dir)
        {
            try
            {
                foreach (string f in Directory.GetFiles(dir))
                    Console.WriteLine(f);
                foreach (string d in Directory.GetDirectories(dir))
                {
                    Console.WriteLine(d);
                    GetFiles(d);
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
    }
}


Note : Here Directory.GetFiles class Reading All .txt files But Stream Reader Class Reading one File i need to Read all Files and Export files into csv

Please can u Help me
thankyou
Posted
Comments
F-ES Sitecore 4-Aug-15 5:36am    
You must have a "filePath" variable declared elsewhere, you're not using the filepath variable returned from GetFiles. Also the GetFiles you are calling is not your own GetFiles function but the GetFiles on the Directory object.

The code really doesn't make any sense and needs re-written. Go through a book on c# and get familiar with the fundamentals and problems like this will become simpler.

get the list of text files and process each file as below
C#
string[] files =
            Directory.GetFiles(@"E:\1150\1150\","*.txt", SearchOption.AllDirectories);
foreach(string file in files )
{
    using(StreamReader streamReader = new StreamReader(file))
    {
      string text = streamReader.ReadToEnd();
      // do something with file content
    }
}

Quote:
we have create one test.csv file in that test file we have 2 rows
1. folderName and
2. nameofFile

string[] files contains array of File Paths (both directory and filename is in the string), read Path.GetFileName Method[^] and Path.GetDirectoryName Method[^] which will help you to get filename and Directory form path string.

Now We came to Final Task;
writing csv is simple, for example
C#
using(var w = new StreamWriter(path))// path of csv file
{
    foreach(string filepath in files ) // loop each file path 
    {
        var directory= Path.GetDirectoryName(file);
        var fileName= Path.GetFileName(file);
        var line = string.Format("{0},{1}", directory, fileName);
        w.WriteLine(line);
        w.Flush();
    }
}

hope this helps you
 
Share this answer
 
v2
Comments
DamithSL 4-Aug-15 6:11am    
what is your requirement?
DamithSL 4-Aug-15 6:34am    
export file names? or content?
if it is content of the files, how you going to process content and build comma separated file?
Sergey Alexandrovich Kryukov 4-Aug-15 10:24am    
Read the answer carefully.
—SA
Sergey Alexandrovich Kryukov 4-Aug-15 10:24am    
Sure, a 5.
—SA
DamithSL 4-Aug-15 11:07am    
Thank You, SA

Solution 1 from DamithSL does what you ask - it reads filename and path and creates a csv file .
If you are looking for more than that to do with csv files ;one nice library is FileHelpers - do a search and you will find it.

 
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