Search code examples
c#arrayslistfile-iofilehelpers

How to pass in name of file when using List<> to store payment records from filename


I've wrote an app to iterate through a directory which contains x amount of files, and import each of them into objects within my program. I create an instance of a class for each of the files which are "payment", and the FileHelpers library reads each line of the file as a new record - into a List. If the files are "info", they are simply moved to preset directories. I am wanting to either append the name of the file to the end of the List or just include this as a variable?

I need to know the name of the file, as each of the "payments" within x amount of files are combined into one fixed width text file to load into our legacy housing management system.

MORE INFO: When I create the fixed width file, I need to output each of the payments within the List<Payment> PLUS the name of the file they came from. I am unsure of how to do this within FileHelpers/C# world :(

E.G. (headers for display purposes - not required in export file)

PAYMENTID PAYMENTAMOUNT REFERENCE DATE FILETYPE FILENAME
011102010 000000010000 20148366 26102011 PO SHGR1234.PO
011102011 000000020000 20148367 26102011 PP SHGF6585.PP
011102012 000000030000 20148368 26102011 DD SHGI9854.DD

Any ideas? Below are some code snippets...

UPDATE - FileHelpers lib = http://www.filehelpers.com/

UPDATE 2 - Code used to loop through text file and get payments

public List<SundryPayment> getOAPayments()
        {
            FileHelperEngine engine = new FileHelperEngine(typeof(SundryPayment));
            res = (SundryPayment[])engine.ReadFile(getFilePath());
            foreach (SundryPayment record in res)
            {
                OAPaymentsList.Add(record);
            }
            return OAPaymentsList;
        }

Update 2 - Code to load files

public List<object> getFiles()
        {
            List<object> obj = new List<object>();
            foreach (string file in files)
            {
                fileExt = Path.GetExtension(file).ToUpper();
                filePath = Path.GetFullPath(file).ToUpper();
                fileName = Path.GetFileNameWithoutExtension(file).ToUpper();
                fullFileName = Path.GetFileName(file).ToUpper();
                fileFund = fileName.Substring(0, 4).ToUpper();

                if (fileExt == ".DIR" || fileExt == ".ERR" || fileExt == ".CRF" || fileExt == ".STA")
                {
                    //Create Info File
                    InfoFile infofile = new InfoFile(filePath);
                    obj.Add(infofile);
                }
                else if (fileExt == ".PO" || fileExt == ".PP" || fileExt == ".TDC" || fileExt == ".TCC" || fileExt == ".DD" || fileExt == ".CSH" || fileExt == ".CQE"
                    || fileExt == ".PZ")
                {
                    if (fileFund == "SHGS" || fileFund == "GGEN")
                    {
                        //Create OA Payment File
                        OAPaymentFile oafile = new OAPaymentFile(filePath);
                        obj.Add(oafile);
                    }
                    else if (fileFund == "SHGF")
                    {
                        InfoFile infofile = new InfoFile(filePath);
                        obj.Add(infofile);
                    }
                    else
                    {
                        //Create AH Payment File
                        AHPaymentFile ahfile = new AHPaymentFile(filePath);
                        //Console.WriteLine("Object Created: {0}", filePath);
                        obj.Add(ahfile);
                    }
                }
            }

            return obj;


        }

Update 2 - (prototype) code used to create fixed width file. Need to put filename where paymetns came from into this file

public new void Create()
        {
            string fileToCreate = Path.Combine("\\\\san\\ict\\allpay\\test\\", "cash.txt");

            using (StreamWriter sw = new StreamWriter(fileToCreate))
            {
                foreach (Payment r in ArchousePayments)
                {
                    string archouseref = r.TenancyRef + r.SubAccount + r.CheckDigit;
                    string firstamount = r.AmountPaid.Replace(".", "");
                    string amount = firstamount.PadRight(10, 'x');
                    string transcode = "ALPY";
                    string date = r.PaymentDate.Substring(0, 2) + r.PaymentDate.Substring(3, 2) + r.PaymentDate.Substring(6, 4);
                    string siteref;
                    string comment;
                    sw.WriteLine(archouseref + amount + transcode + date + amount.Length);
                }

            }
        }

Solution

  • I figured this one one by adding another fixed width record to the end of my Import class, then used this to store the filename via Path.GetFileName(). I could then pass this into the class as required.