Here is a quick-n-dirty version:
using System;
using System.IO;
namespace DW_390448
{
class Program
{
private static void Usage()
{
Console.Write("DW_390448 infile outfile");
}
static void Main(string[] args)
{
if (!args.Length.Equals(2))
{
Usage();
return;
}
try
{
using (StreamReader fileIn = new StreamReader(args[0]))
{
using(StreamWriter fileOut = new StreamWriter(args[1]))
{
string[] arr_strData = {};
while(!fileIn.EndOfStream)
{
arr_strData = fileIn.ReadLine().Split(',');
foreach(string s in arr_strData)
{
fileOut.WriteLine(s.Trim());
}
}
//
fileOut.Close();
}
//
fileIn.Close();
}
}
catch (Exception exc)
{
Console.WriteLine("Exception: " + exc.Message);
return;
}
Console.WriteLine("Finished");
}
}
}
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
Just thinking: If you REALLY meant you wanted each line in a new separate file, there is another method you would need. Here is a program that will do both. If you give it only the input file, it will create multiple output files starting with File000.txt
using System;
using System.IO;
namespace DW_390448
{
class Program
{
private static void Usage()
{
Console.WriteLine(
"DW_390448 infile outfile /*creates outfile*/\n" +
"DW_390448 infile /*creates multiple outfiles*/\n"
);
}
private static bool SliceFile(StreamReader fileIn, StreamWriter fileOut, ref string strError)
{
bool blnRetVal = true;
try
{
using (fileIn)
{
using (fileOut)
{
foreach (string s in fileIn.ReadToEnd().Split(",\r\n".ToCharArray()))
{
fileOut.WriteLine(s.Trim());
}
//
fileOut.Close();
}
//
fileIn.Close();
}
}
catch (Exception exc)
{
blnRetVal = false;
strError = exc.Message;
}
return blnRetVal;
}
private static string GetFileName(int intNameNum)
{
return "File" + intNameNum.ToString().PadLeft(3, '0') + ".txt";
}
private static bool SeparateFile(StreamReader fileIn, ref string strError)
{
bool blnRetVal = true;
try
{
using (fileIn)
{
int intFileCount = 0;
foreach (string s in fileIn.ReadToEnd().Split(",\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
{
using (StreamWriter fileOut = new StreamWriter(GetFileName(intFileCount++)))
{
fileOut.WriteLine(s.Trim());
fileOut.Close();
}
}
//
fileIn.Close();
}
}
catch (Exception exc)
{
blnRetVal = false;
strError = exc.Message;
}
return blnRetVal;
}
static void Main(string[] args)
{
string strError = "";
if (!args.Length.Equals(1) && !args.Length.Equals(2))
{
Usage();
return;
}
// Split single files into another file
if (args.Length.Equals(2))
{
if (!SliceFile(new StreamReader(args[0]), new StreamWriter(args[1]), ref strError))
{
Console.WriteLine("Error: " + strError);
return;
}
}
// Split single files into multiple files
if (args.Length.Equals(1))
{
if (!SeparateFile(new StreamReader(args[0]), ref strError))
{
Console.WriteLine("Error: " + strError);
return;
}
}
Console.WriteLine("Finished");
}
}
}
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402