Question

I have some sql commands that are separated by an additional newline character:

ALTER TABLE XXX
ALTER COLUMN xxx real

ALTER TABLE YYY
ALTER COLUMN yyy real

ALTER TABLE ZZZ
ALTER COLUMN zzz real

I've tried reading the file by using an array of character separators such as the following,

new char[] { '\n', '\r'}

inside this method:

    private static List<string> ReadFile(string FileName, char[] seps)
    {
        if (!File.Exists(FileName))
        {
            Console.WriteLine("File not found");
            return null;
        }

        using (StreamReader sr = new StreamReader(FileName, Encoding.Default))
        {
            string content = sr.ReadToEnd();
            return content.Split(seps, StringSplitOptions.RemoveEmptyEntries).ToList();
        }
    }

However, this doesn't seem to be working. I would like to have each command represented by a separate string. How can I do this?

Was it helpful?

Solution 5

Thank you everyone for your answers. I ended up going with this helper method:

    private static List<string> GetCommands(string location)
    {
        List<string> ret = new List<string>();
        List<string> tmp = ReadFile(location, new string[] { "\r\n\r\n"});

        for (int i = 0; i < tmp.Count; i++)
        {
            string rem = tmp[i].Replace("\r", "");
            ret.Add(rem);
        }
        return ret;
    }

As an aside, the equivalent is so much easier in Python. For example, what I'm trying to do can be expressed in these three lines:

with open('commands.txt', 'r') as f:
    content  = f.read()
    commands = [ command for command in content.split('\n\n') ]

OTHER TIPS

Why not use File.ReadAllLines()?

private static List<string> ReadFile(string FileName)
{
    if (!File.Exists(FileName))
    {
        Console.WriteLine("File not found");
        return null;
    }

    var lines = File.ReadAllLines(FileName);
    return lines.ToList();
}

This will automatically read and split your file by newlines.

If you want to filter out empty lines, do this:

var nonEmpty = ReadFile(path).Where(x => !string.IsNullOrEmpty(x)).ToList();

Side note, I would change your if statement to throw an exception if the file cannot be found.

if (!File.Exists(FileName))
{
    throw new FileNotFoundException("Can't find file");
}

You can filter the examples. When I read them in, the empty lines had a length 1 and its char value said 131 for some reason. So I just filtered by length > 1

void Main()

{ var results = ReadFile(@"C:\temp\sql.txt", new char[]{'\n'}); Console.WriteLine(results.Count); foreach (var result in results) { Console.WriteLine(result); } }

private static List<string> ReadFile(string FileName, char[] seps)
{
    if (!File.Exists(FileName))
    {
        Console.WriteLine("File not found");
        return null;
    }

    using (StreamReader sr = new StreamReader(FileName, Encoding.Default))
    {
        string content = sr.ReadToEnd();
        return content.Split(seps, StringSplitOptions.RemoveEmptyEntries).Where (c => c.Length > 1).ToList();
    }
}

Try This:

private static List<string> ReadFile(string FileName)
  {
      List<string> commands = new List<string>();
      StringBuilder command = new StringBuilder();

      if (!File.Exists(FileName))
      {
          Console.WriteLine("File not found");
          return null;
      }

      foreach (var line in File.ReadLines(FileName))
      {
          if (!String.IsNullOrEmpty(line))
          {
              command.Append(line + "\n");
          }
          else
          {
              commands.Add(command.ToString());
              command.Clear();
          }
      }
      commands.Add(command.ToString());
      return commands;
  }

If you are sure you'll always have \r\n line endings, you can use:

var commands = content.Split(new []{"\r\n\r\n"}, StringSplitOptions.RemoveEmptyEntries);

Otherwise, try using regex:

var commands = Regex.Split(content, @"\r?\n\r?\n")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top