Click to Download FileUtils
INTRODUCTION:
Thanks for using Vishal.FileUtils.
This library was written to aid developers with the common task of handling and
working with delimited text format files. The library makes working with delimited
text files quick and painless by providing common routine functionality. Some of
the common functionality includes:
and more…
Definition of a Delimited File:
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 C# examples:
Here you can see how easy it is to
work with delimited files. Before you start, don’t forget to add a reference to
TextFileUtils.dll in your project and add the line “using Vishal.TextFileUtils;” above your code.
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");
// 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");
// 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
df.ReOrderColumns(OldColumnsList);
// Save the file!
df.Save(@"C:\Ouput.csv");
Thanks for using Vishal.TextFileUtils!
Regards,
Vishal Seth