Skip to content

Instantly share code, notes, and snippets.

@CVertex
Created December 8, 2014 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CVertex/6c83770043e26e94af04 to your computer and use it in GitHub Desktop.
Save CVertex/6c83770043e26e94af04 to your computer and use it in GitHub Desktop.
Splitting files by line using C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using C = System.Console;
using System.IO;
using System.Net;
namespace Commands
{
public class SplitFile
{
public void Run()
{
C.WriteLine("Splitting a file..");
var lines = File.ReadAllLines(@"insert-song-genres.sql");
long totalLines = lines.LongCount();
var outputFileFormat = @"insert-song-genres_part_{0}.sql";
int skip = 0;
int count = 20000;
while (skip < totalLines) {
string outFile = String.Format(outputFileFormat,skip);
File.WriteAllLines(outFile,lines.Skip(skip).Take(count));
skip += count;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment