3

I have a little piece of code in which I can read a CSV file and then my code converts the accounts in the CSV file to IBAN accounts.

Somehow this gives me an error:

using (var client = new WebService.BANBICSoapClient("IBANBICSoap"))
{
    StreamReader sr = new StreamReader(@"C:\CSV\test.csv");
    StreamWriter sw = new StreamWriter(@"C:\CSV\testOut.csv");
    while (sr.Peek() >= 0)
    {
        string line = sr.ReadLine();
        try
        {
            string[] rowsArray = line.Split(';');
            line = string.Empty;
            string account = rowsArray[0];
            string rel = rowsArray[1];
            string res = acc;
            string rest = acct;
            if (resultIBAN != string.Empty && resultBIC != string.Empty)
            {
                line = account + ";" + res +";" + rest + ";" + rel;
            }

            else
            {
                line = account + ";" + "0" + ";" + "0" + ";" + rel;
            }
        }
        catch (Exception msg)
        {
            Console.WriteLine(msg);
        }
        sw.WriteLine(line) ;
    }
    sr.Close();
    sw.Close();
}
}
Matheno
  • 4,112
  • 6
  • 36
  • 53
  • Finding all the files in a folder and applying the changes to them is a topic for a high school student. If you can'd grasp the logic of this, how come you're dealing with ANYTHING related to banking and money? I'd be scared as hell if I were you. – walther Nov 28 '13 at 08:19
  • @Marijke Is there a reason you can't use a loop ? – bumble_bee_tuna Nov 28 '13 at 08:21
  • FYI, there's an extra closing brace in this code. – Adi Inbar Mar 06 '14 at 05:15

3 Answers3

4

For you streamreader and writer

StreamReader sr = new StreamReader(@"C:\CSV\test.csv");
StreamWriter sw = new StreamWriter(@"C:\CSV\testOut.csv");

You can place this within a method:

private void ConvertToIBANAccount(string fileName,string outFileName)
{
    using (var client = new WebService.BANBICSoapClient("IBANBICSoap"))
        {
            StreamReader sr = new StreamReader(fileName);
            StreamWriter sw = new StreamWriter(outFileName);
            Console.WriteLine("Bezig met converteren....");
            while (sr.Peek() >= 0) 
            {
            string line = sr.ReadLine(); 
                try
                {
                    string[] rowsArray = line.Split(';');
                    line = string.Empty;
                    string account = rowsArray[0];
                    string relationID = rowsArray[1];
                    string resultIBAN = client.BBANtoIBAN(account);
                    string resultBIC = client.BBANtoBIC(account);
                    if (resultIBAN != string.Empty && resultBIC != string.Empty)
                    {
                        line = account + ";" + resultIBAN +";" + resultBIC + ";" + relationID;
                    }

                    else
                    {
                        line = account + ";" + "0" + ";" + "0" + ";" + relationID;
                    }
                }
                catch (Exception msg)
                {
                    Console.WriteLine(msg);
                }
            sw.WriteLine(line) ;

            }
            sr.Close();
            sw.Close();
        }
        Console.WriteLine("Conversie succesvol");
        Console.ReadLine();
    }
}

Then find each file of extension .csv

string[] files = Directory.GetFiles(yourDirectoryName, "*.csv");
files.ToList().ForEach(f => ConvertToIBANAccount(f,"temp" + f));
liquidsnake786
  • 449
  • 2
  • 8
  • The file path gets passed into the method as seen in the last line of my answer – liquidsnake786 Nov 28 '13 at 08:26
  • files.ToList().ForEach(f => ConvertToIBANAccount(f,f.Replace(".csv","temp.csv")); and to remove the slashes you can do a similar .Replace if you cannot come across a nice alternative. – liquidsnake786 Nov 28 '13 at 09:24
3

you can read all csv file like this

var filePaths = Directory.GetFiles(@"\\Pontos\completed\", "*_*.csv")
foreach(string s in filePaths)
{
    using (StreamReader sr = new StreamReader(s))
    {
           //perform task related file 
    }
}
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
1

Do something like this...

List<string> filesList = Directory.GetAllFiles("*.csv", FolderLocationToSearch).ToList();
foreach(var fileName in fileList)
{
    ConvertToIBANfromCSV(fileName);
}


public static void ConvertToIBANfromCSV(string fileName)
using (var client = new WebService.BANBICSoapClient("IBANBICSoap"))
        {
            StreamReader sr = new StreamReader(fileName);
            StreamWriter sw = new StreamWriter(@"C:\CSV\testOut.csv");
            Console.WriteLine("Bezig met converteren....");
            while (sr.Peek() >= 0) 
            {
            string line = sr.ReadLine(); 
                try
                {
                    string[] rowsArray = line.Split(';');
                    line = string.Empty;
                    string account = rowsArray[0];
                    string relationID = rowsArray[1];
                    string resultIBAN = client.BBANtoIBAN(account);
                    string resultBIC = client.BBANtoBIC(account);
                    if (resultIBAN != string.Empty && resultBIC != string.Empty)
                    {
                        line = account + ";" + resultIBAN +";" + resultBIC + ";" + relationID;
                    }

                    else
                    {
                        line = account + ";" + "0" + ";" + "0" + ";" + relationID;
                    }
                }
                catch (Exception msg)
                {
                    Console.WriteLine(msg);
                }
            sw.WriteLine(line) ;

            }
            sr.Close();
            sw.Close();
        }
        Console.WriteLine("Conversie succesvol");
        Console.ReadLine();
    }

Please forgive me if there are syntax mistakes as i have written code manually.

Nisarg Shah
  • 354
  • 1
  • 14