0

I am attempting to replace a value in a .csv file, with "existing value" + "a".In other words, I would like to append an "a" to all rows, one specific column, but omit the header row. My code below is successful in the bottom row of the file, but I believe the placement of my foreach loop relative to the while loop is causing an issue. Is there something glaring that I am missing? If so, I apologize as I am a beginner. Please note, I know there is some redundant code in there that I am cleaning up once working. Thanks.

public static void ReadReplace()
{
    using (StreamReader sr = new StreamReader(@"C:\TestDirectory\Test.csv"))
    {
        foreach(string test in File.ReadLines(@"C:\TestDirectory\Test.csv"))
        {
            String line;

            while ((line = sr.ReadLine()) != null)
            {
                string[] parts = line.Split(',');
                string serviceId = parts[34].Trim('"');
                string appendedServiceId = serviceId + "a";

                string text = File.ReadAllText(@"C:\TestDirectory\Test.csv");
                text = text.Replace(serviceId, appendedServiceId);
                File.WriteAllText(@"C:\TestDirectory\TestUpdated.csv", text);

            }
        }
    }

}
3
  • 1
    Did you have a question? Manually splitting a CSV can be problematic. Using a tool like CSVHelper you could read it into a typed collection, make the changes and write out the result very easily. Please read How to Ask and take the tour. Commented Jan 3, 2018 at 22:18
  • Why are you reading the same file 3 times??? You have much larger issues with that code... Commented Jan 3, 2018 at 22:18
  • The intention is to refactor and reduce, but wanted to get the problem solved first. Commented Jan 3, 2018 at 22:21

1 Answer 1

2

You are re-reading the source file and over-writing once per loop iteration.

Despite all the other issues here, move the final write call out of the loop. You are still reading this file twice for no reason but I'll leave that up to you.

I also removed your un-neeeded and un-used "test" loop.

Untested:

public static void ReadReplace()
{

    string text = File.ReadAllText(@"C:\TestDirectory\Test.csv");

    using (StreamReader sr = new StreamReader(@"C:\TestDirectory\Test.csv"))
    {
        String line;    
        while ((line = sr.ReadLine()) != null)
        {
            string[] parts = line.Split(',');
            string serviceId = parts[34].Trim('"');
            string appendedServiceId = serviceId + "a";

            text = text.Replace(serviceId, appendedServiceId);

        }
    }

    File.WriteAllText(@"C:\TestDirectory\TestUpdated.csv", text);

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.