Skip to content

Instantly share code, notes, and snippets.

@DeepSky8
Created March 30, 2016 17:09
Show Gist options
  • Save DeepSky8/79cdeee3b823a091308bacee1b4eadb3 to your computer and use it in GitHub Desktop.
Save DeepSky8/79cdeee3b823a091308bacee1b4eadb3 to your computer and use it in GitHub Desktop.
Supposedly inserts a tab after each csv item in the source document, or a new line every _Columns_ number of items. The text documents currently appear to have new lines after each item. The relevant code starts on line 138.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSV_Split
{
class Program
{
static void Main(string[] args)
{
var s = new Splitter();
s.Controller();
}
}
public class Splitter
{
int Columns { get; set; }
string sourcePath = @"C:\Users\DeepSky8\Downloads";
string targetPath = @"C:\Users\DeepSky8\Downloads\SubDir";
StringBuilder wholeFile = new StringBuilder();
string newFileName;
/// <summary>
/// Coordinates user input, file reading, string creation, and new file creation.
/// </summary>
public void Controller()
{
while (NeedColumnNumber()) { }
while (NeedFile()) { }
NeedNewFileName();
FinishUp();
}
/// <summary>
/// Gets user input and parses & saves to class-level var. Use as while statement arg to ensure column number is accepted
/// </summary>
/// <returns></returns>
bool NeedColumnNumber()
{
bool needNumber = true;
Console.WriteLine("How many columns should the resulting text contain?");
var entry = Console.ReadLine();
var result = 0;
if (Int32.TryParse(entry, out result))
{
Columns = result;
needNumber = false;
}
else
{
Console.WriteLine($"{entry} didn't parse as an integer. Please try again.");
}
return needNumber;
}
/// <summary>
/// Gets name of file to parse, including extension. If this file exists, reads the file into memory, stored in wholeFile.
/// </summary>
/// <returns></returns>
bool NeedFile()
{
bool needFile = true;
Console.WriteLine("What is the file name, including extension.");
var fileName = Console.ReadLine();
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
if (System.IO.File.Exists(sourceFile))
{
var file = System.IO.File.Open(sourceFile, System.IO.FileMode.Open);
var isDone = false;
var data = new byte[1024];
var totalBytesRead = 0;
while (!isDone)
{
var bytesRead = file.Read(data, 0, 1024);
totalBytesRead += bytesRead;
isDone = bytesRead < 1024;
wholeFile.Append(System.Text.Encoding.ASCII.GetString(data, 0, 1024));
}
needFile = false;
}
else
{
Console.WriteLine("Source does not exist!");
}
return needFile;
}
/// <summary>
/// Gets user input to name the new file, and stores it in a class-level var.
/// </summary>
void NeedNewFileName()
{
Console.WriteLine("What should the new document be called?");
newFileName = Console.ReadLine() + ".txt";
}
/// <summary>
/// Splits the file in memory on commas, and inserts a tab after each item. Instead inserts a new line after the number of items indicated by Columns. Appends all this to a document, created if it didn't exist
/// </summary>
void FinishUp()
{
var isDone = false;
var pointer = 0;
var splitFileContents = wholeFile.ToString().Split(',');
string[] eachLine = new string[Columns];
string[] amendedLines = new string[Columns];
if (!System.IO.Directory.Exists(targetPath)) { System.IO.Directory.CreateDirectory(targetPath); }
string destFile = System.IO.Path.Combine(targetPath, newFileName);
int remainderArray = (splitFileContents.Length % Columns);
while (!isDone)
{
try { Array.Copy(splitFileContents, pointer, eachLine, 0, Columns); }
catch (System.ArgumentException)
{
if (!(remainderArray == 0))
{
Array.Copy(splitFileContents, pointer, eachLine, 0, remainderArray);
}
}
for (int i = 0; i < Columns; i++)
{
if (i == Columns)
{
amendedLines[i] = String.Concat(eachLine[i], "\n");
}
else
{
amendedLines[i] = String.Concat(eachLine[i], "\t");
}
}
pointer += Columns;
System.IO.File.AppendAllLines(destFile, amendedLines);
if (pointer >= splitFileContents.Length)
{
isDone = true;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment