Barrassment
Barrassment

Reputation: 75

Splitting one large text file by custom delimiter into smaller files. Each new file name derived from header from smaller file

I've used the following code to split a large text file into many smaller ones using "/* " as the delimiter.

This works as expected, however I'm now looking to pull the first line of text from each of the new, smaller text files, and use this to name each individually, rather than "C:\output-files-{0}.txt", C:\output-files-{1}.txt... etc.

Is someone able to help on this please?

Much appreciated.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Running file splitter now...");
        Splitter splitter = new Splitter();
        splitter.Split("C:\\STA_PRD_allJobs.JIL", "C:\\output-files-{0}.txt");
        Console.WriteLine("JIL script splitting complete.");
    }
}

class Splitter
{
    public void Split(string inputfile, string outputfilesformat)
    {
        int i = 0;
        System.IO.StreamWriter outfile = null;
        string line;

        try
        {
            using (var infile = new System.IO.StreamReader(inputfile))
            {
                while (!infile.EndOfStream)
                {
                    line = infile.ReadLine();
                    if (line.Contains("/*"))  
                    {
                        if (outfile != null)
                        {
                            outfile.Dispose();
                            outfile = null;
                        }
                    }

                    if (outfile == null)
                    {
                        outfile = new System.IO.StreamWriter(
                            string.Format(outputfilesformat, i++),
                            false,
                            infile.CurrentEncoding);
                    }
                    outfile.WriteLine(line);
                }
            }
        }
        finally
        {
            if (outfile != null)
                outfile.Dispose();
        }
    }
}

Upvotes: 0

Views: 174

Answers (1)

Anu Viswan
Anu Viswan

Reputation: 18153

You could read the first line and store in a variable.

public void Split(string inputfile, string outputfilesformat)
{
    int i = 0;
    System.IO.StreamWriter outfile = null;
    string line;

    try
    {
      var firstLine = string.Empty;
      using (var infile = new System.IO.StreamReader(inputfile))
      {

            while (!infile.EndOfStream)
            {
                line = infile.ReadLine();
                if(string.IsNullOrEmpty(firstLine))
                firstLine = line;
                if (line.Contains("/*"))  
                {
                    if (outfile != null)
                    {
                        outfile.Dispose();
                        outfile = null;
                    }
                }

                if (outfile == null)
                {
                    outfile = new System.IO.StreamWriter(
                        $"{firstLine}.txt",
                        false,
                        infile.CurrentEncoding);
                   firstLine = string.Empty;
                }
                outfile.WriteLine(line);
            }
        }
    }
    finally
    {
        if (outfile != null)
            outfile.Dispose();
    }
}

Upvotes: 0

Related Questions