Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
1.11/5 (2 votes)
See more:
Hi,

Can anybody help me to read multiple text file from multiple folder
I am using entity frame work to save data to the table my code is

I am using foreach loop for reading each lines in a file.

my code is
C#
string strFilePath =  ConfigurationManager.AppSettings["InputPath"];

       string[] strfilesToProcess = Directory.GetFiles(strFilePath,"*.txt");
         // string strArchivePath = clientname + ConfigurationManager.AppSettings["key"];
     // string[] strfilesToProcess = Directory.GetFiles(strFilePath, "*.txt");
          string strline = "";
          string strfileAccount = "";
          try
          {
              foreach (string FileToGo in strfilesToProcess)
              {
                  using (IPDEntities context = new IPDEntities())
                  {
                      AllClientEDLog allClEdLog = new AllClientEDLog();
                      System.IO.StreamReader file = new System.IO.StreamReader(FileToGo);
                      while ((strline = file.ReadLine()) != null)
                      {
                          if ((strline.Contains("Account Number|")) || (strline.Trim() == ""))
                          {
                              continue;
                          }

                          string[] lineData = strline.Split('|');
                          strfileAccount = lineData[0].Trim();
                          allClEdLog.Account_No = lineData[0].Trim();
                          allClEdLog.MRN = lineData[1].Trim();
                          if (lineData[3].Trim() == "")
                          {
                              allClEdLog.Pat_Name = lineData[2].Trim() + ", " + lineData[4].Trim();
                          }
                          else
                          {
                                  allClEdLog.Pat_Name = lineData[2].Trim() + ", " + lineData[4].Trim() + " " + lineData[3].Trim();
                          }
                          allClEdLog.Carrier = lineData[5].Trim();
                          allClEdLog.Department_Location = lineData[10].Trim();
                          allClEdLog.DOS = Convert.ToDateTime(lineData[6]);
                          allClEdLog.Registration_Time = Convert.ToDateTime(lineData[6] + " " + lineData[7].Substring(0, 2) + ":" + lineData[7].Substring(2, 2));
                          if (lineData[8].Trim() != "")
                          {
                              allClEdLog.Discharge_Time = Convert.ToDateTime(lineData[8] + " " + lineData[9].Substring(0, 2) + ":" + lineData[9].Substring(2, 2));
                          }
                          allClEdLog.Disposition = lineData[11].Trim();
                          allClEdLog.DOB = Convert.ToDateTime(lineData[12]);
                          allClEdLog.Gender = lineData[13].Trim();
                          allClEdLog.TemplateId = 580;

My app config is
HTML
<appsettings>
    <add key="InputPath" value="\\mrsi-data2\ED_Logs\TEST\IN\" />
    <add key="ArchivePath" value="\\mrsi-data2\ED_Logs\TEST\OUT\" />
    <add key="RecepientAddress" value="mabraham@logixhealth.com" />
    <add key="EmailErrorsTo" value="mabraham@logixhealth.com" />
    <add key="EmailErrorsFrom" value="mabraham@logixhealth.com" />
  </appsettings>
Posted
Updated 3-Jan-14 5:38am
v2
Comments
Richard MacCutchan 3-Jan-14 11:54am    
And your problem is ?
Mary Abraham 3-Jan-14 15:44pm    
I want to read from another folder also how to read using add key . Can we use the same entity and read from another folder.

Thanks

Mary
Richard MacCutchan 4-Jan-14 4:33am    
To read from another folder you just need to provide its path. I have no idea what "read using add key" means.
db7uk 3-Jan-14 16:07pm    
Does this work using the InputPath? Also to clean it up a bit I would not concatenate your strings like you have done. If you were getting hundreds of files you might overtime see some memory problems. use string.format, string.concat or stringbuilder.

Can you explain a little more? What is wrong with moving what you have into a method like "void ReadFiles(string filePath)" and looping through other folders passing the directory path into method?

Lastly, are you referring to looping through sub folders? this is simple to do "Directory.GetFiles(strFilePath,"*.txt", SearchOption.AllDirectories)"????

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