This is a very old piece of .net code that became very popular for some reason, so I'm posting it here in the legacy section.
A delimited file is a text file used to store un-typed data in a tabular form. It consists of a Header line and one or more LineEntries of data. Each line consists of a Value that is encased within what is known as a String Qualifier. Each encased value is then separated by a delimiter or Separator. At the end of every line is an EndLine string. Common examples are the CSV (Comma Separated Value) format and the Tab Delimited format.
So… I finally ended up writing a library that I’ve found to be immensely useful to me over the years. You can download it here! Its completely .Net, hasn’t failed me yet and can chop, slice, dice and grate delimited file formats. Common funcationality includes:
- Reading/ Writing Delimited Files
- Providing a library of strogly typed objects that define elements of a delimited text file
- Converting from one delimited format to another
- Defining your own custom file formats
- Re-ordering columns of a delimited file
- Re-naming columns of a delimited file
- Adding columns
- Removing columns
- Inserting columns
- Sorting columns
- Reversing columns
And more… Before you start, Remember to add a reference to TextFileUtils.dll and add the line “using Vishal.TextFileUtils;” above your class. Here are some C# examples of how easy it is to work with delimited files:
Reading/ Writing Delimited Files:
DelimitedFile df = new DelimitedFile(new FileFormat(PredefinedFormats.CSV), @”C:\SomeFile.abc”);
foreach (LineEntry le in df)
{
Console.WriteLine(le.FieldValues[“MyColumnName”]);
}
for (int i = 0; df.Count; i++)
{
le.FieldValues[“MyColumnName”]= “MyColumnData”;
}
df.Save(@”C:\Ouput.csv”);
Converting from one format to another:
// One line of code!
FileUtil.ConvertFile(@”C:\somefile.txt”, new FileFormat(PredefinedFormats.CSV), @”C:\Output.txt”, new FileFormat(PredefinedFormats.TAB));
// One line of code with custom formats!
FileUtil.ConvertFile(@”C:\somefile.txt”, new FileFormat(“\”", “,”, “\r\n” ), @”C:\Output.txt”, new FileFormat(“”, “\t”, “\r\n”);
Removing, Renaming, Inserting, Reordering, Sorting, Adding Columns
// Read File
DelimitedFile df = new DelimitedFile(new FileFormat(PredefinedFormats.CSV), @”C:\SomeFile.abc”);
// Remove a column called “MyColumn”
df.RemoveColumn(“MyColumn”);
// move a column called “MyOtherColumn” to the 5th position
df.MoveColumn(“MyOtherColumn”, 5);
// sort the columns alphabetically
df.SortColumns();
// Insert a column called “MyFirstColumn” at position 0
df.InsertColumn(0, “MyFirstColumn”);
// Rename a column called “OldColumnName” to “NewColumnName”
df.RenameColumn(“OldColumnName”, “NewColumnName”); // Reverse the columns in the file df.ReverseColumns();
// Re-order the columns to your liking
List<string> OldColumnsList = df.GetColumnNamesList();
// {Code to Re-Order OldColumnList here}
df.ReOrderColumns(OldColumnsList);
// Save the file!
df.Save(@”C:\Ouput.csv”);
The library has much more. For the complete specifications refer to the TextFileUtils.chm help file. If you have any comments, questions or improvements, you can post them here or email me.
Download TextFileUtils version 1.0.0.0