Tuesday, March 24, 2009

C# Splitting a Single Text File into Multiple Files

In a recent post I showed how to export data using sqlcmd within ssis. The entire purpose of this utility was to create a generic package that could take any sql query and output the data to any flat file, dynamically and in so doing get past the meta-data of the flat file embedded within the package. The first chance I had to run it, I was thrown a curve ball, in that the request was that the results be broken up into multiple files. Since I wanted to keep things as generic as possible, I decided to write an executable that took the results and split it into multiple files. The code for this utility is below. It’s a work in progress, with most of the code handling bad input, but the beef is in the loop.


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace slicer
{
class Program
{
static void Main(string[] args)
{

//Define Local Variables for Args
string argValue;
string path = "holder";
int breakSplit = 10; // Default of 10
string fileOutPre = "holder";
//If left out it defaults to a txt extention
string fileOutExt = "txt";

//Instantiate Helper Class To handle errors
HelperClass hc = new HelperClass();

//Pull Args and Assign
foreach (string s in args)
{
//Manipulate input for case statement
string argtype;
int arglegnth;
arglegnth = s.Length;
argtype = s.Substring(0, 2);

switch (argtype)
{
case "-p": //Source Path
argValue = s.Substring(2, (arglegnth - 2));
path = argValue;
break;

case "-n": //Number of Records per file
argValue = s.Substring(2, (arglegnth - 2));
breakSplit = Convert.ToInt32(argValue);
break;

case "-d": //Dest Path
argValue = s.Substring(2, (arglegnth - 2));
fileOutPre = argValue;
break;

case "-e": //Extention of File
argValue = s.Substring(2, (arglegnth - 2));
fileOutExt = argValue;
break;

case "NoParm": //Bad input
//show input
Console.WriteLine(hc.unknownInput(argtype));

//skip line for readability
Console.WriteLine(" ");

//show syntax
Console.WriteLine(hc.syntax());

//Get Out Now!!!
Environment.Exit(1);
break;
default: //Navigate to NoParm switch
goto case "NoParm";

}

}

//Check Mandatory Parameters
if (path == "holder" | fileOutPre == "holder")
{
//show input
Console.WriteLine("Error: Missing Required Input Value");
//skip line for readability
Console.WriteLine(" ");

//show syntax
Console.WriteLine(hc.syntax());

//Get Out Now!!!
Environment.Exit(1);
}






int fileNumber = 1;
string ln;
string fileName = fileOutPre + "_" + fileNumber.ToString() + "." + fileOutExt;
int lineCounter = 1;
int breakCounter = 0;



StreamReader sr = new StreamReader(path);
StreamWriter sw = new StreamWriter(fileName);

while ((ln = sr.ReadLine()) != null)
{

sw.WriteLine(ln);
lineCounter = lineCounter + 1;
breakCounter = breakCounter + 1;

if((breakCounter/breakSplit)== 1)
{breakCounter = 0;
sw.Close();
fileNumber = fileNumber + 1;
fileName = fileOutPre + "_" + fileNumber.ToString() + "." + fileOutExt;
sw = new StreamWriter(fileName);
//Console.WriteLine(ln);
}

}
sw.Close();
sr.Close();

}

}
class HelperClass
{
public string unknownInput(string input)
{
string outText =
"There is no parameter for " + "\"" + input + "\"";

return outText;
}

public string syntax()
{
string outText;
outText =
"The Splice utility is designed to take a standard ascii text file and split it into " + "\r\n" +
"multiple ascii text Files, use this code at your own caution" + "\r\n" +
"\r\n" +
"All parameters follow the template of a preceeding " + "\"-\"" + "before the input Parameter" + "\r\n" +
"while all parameter values with spaces should be enclosed in double quotes. " + "\r\n" +
"[-p] Path of Source File - Required No Default" + "\r\n" +
"[-d] Destination Path and prefix of Files - Required No Default" + "\r\n" +
"(-n) Number of Lines per file Destination Path. - Default of 10 Lines" + "\r\n" +
"[-e] Extention of File - Default of .txt " + "\r\n" +
"\r\n" +
"EXAMPLE:"+ "\r\n" +
"-p\"H:\\vssimages.txt\" -d\"H:\\VSSImage\" -n1000000 -e\"txt\""


;
return outText;
}


}
}

No comments: