none
How to read multiple file from multiple folder and write it to a out folder RRS feed

  • Question

  • Hi

    I am trying to read a text file from a location and put it in a out folder using entities

    it works for one folder but how to read multiple files from multiple folder using configuration manager

    My program is below and how I can modify my app config

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    using MRSINotificationLibrary;
    using System.IO;
    using MayoFL;

    namespace LoadMayoEdLog
    {
        class Program
        {
            static void Main(string[] args)
            {
                string strFilePath = ConfigurationManager.AppSettings["InputPath"];
                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;

                                if (context.AllClientEDLogs.Any(o => o.Account_No == allClEdLog.Account_No))
                                {
                                    AllClientEDLog s = context.AllClientEDLogs.First(o => o.Account_No == allClEdLog.Account_No);
                                    context.AllClientEDLogs.DeleteObject(s);
                                    context.SaveChanges();
                                }
                                context.AddToAllClientEDLogs(allClEdLog);
                                context.SaveChanges();

                                allClEdLog = new AllClientEDLog();

                            } //while
                            file.Close();
                            file.Dispose();
                            if (context.AllClientEDLogs.Any(o => o.Account_No == strfileAccount))
                            {
                                AllClientEDLog s = context.AllClientEDLogs.First(o => o.Account_No == o.Account_No);
                                context.AllClientEDLogs.DeleteObject(s);
                                context.SaveChanges();
                            }
                            if (allClEdLog.Account_No != null)
                            {
                                context.AddToAllClientEDLogs(allClEdLog);
                                context.SaveChanges();
                            }
                        } //using
                        File.Move(FileToGo, Path.Combine(ConfigurationManager.AppSettings["ArchivePath"].ToString(), Path.GetFileName(FileToGo) + "_" + DateTime.Now.ToString("yyyy MM dd").Replace(" ", "") + ".txt"));
                    } //for
                }
                            }
            }
        }
    }

    My config filg has below.
    <add key="InputPath" value="\\mABC \NC\Temp\in\" />
    <add key="ArchivePath" value="\\mABC \NC\Temp\out\" />

    I have one more folder which has text files how can I add to the above.

    Thanks,

    Mary Abraham


    Mary Sunish

    Monday, December 30, 2013 9:21 PM

Answers

  • If you want to have multiple input files then you'll likely want to create your own custom configuration section so you can do something like this:

    <mySettings>
       <inputs>
          <add value="path1" />
          <add value="path2" />
       </inputs>
    </mySettings>

    Creating a custom configuration section isn't hard but does require intermediate C# skills. 

    If you will only have 2 paths and you want a simple solution then you can stuff both paths into your single InputPath separated by a delimiter (vertical bar or something).  Then in your code you can use Split('|') to get an array of input paths that you can loop through.

    var setting = ConfigurationManager.AppSettings["InputPath"];
    var inputs = setting.Split('|');
    foreach (var input in inputs)
       //Do work

    Michael Taylor
    http://msmvps.com/blogs/p3net
      

    Tuesday, December 31, 2013 3:21 PM
    Moderator