[Solved] Split text file every 120,000 Lines?


Just another way using Enumerable.GroupBy and “integer division groups”:

int batchSize = 120000;
var fileGroups = File.ReadLines(path)
    .Select((line, index) => new { line, index })
    .GroupBy(x => x.index / batchSize)
    .Select((group, index) => new {
        Path = Path.Combine(dir, string.Format("FileName_{0}.txt", index + 1)),
        Lines = group.Select(x => x.line)
    });
foreach (var file in fileGroups)
    File.WriteAllLines(file.Path, file.Lines);

3

solved Split text file every 120,000 Lines?