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:

  1. Readingdelimited text files,
  2. Providing a library of strongly typed objects to deal with elements of a delimited file,
  3. Writing delimited files,
  4. Changing formats of text files by using common delimited formats or custom ones,
  5. Defining your own custom formats to work with delimited files,
  6. Re-ordering columns of a delimited file,
  7. Re-Naming Columns of a delimited file,
  8. Adding, Deleting, Inserting Columns in a delimited file,
  9. Sorting Columns of a delimited file,

     

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.

   

  1. 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");

   

  1. 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");

   

   

  1. Inserting/ Reordering/ Renaming/ Moving/ Deleting/ Reversing 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

df.ReOrderColumns(OldColumnsList);

   

// Save the file!

df.Save(@"C:\Ouput.csv");

   

  1. More: 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 reach the author at projects@vishalseth.com.

   

   

Thanks for using Vishal.TextFileUtils!

   

Regards,

Vishal Seth

http://www.vishalseth.com

http://www.vishalseth.net

projects@vishalseth.com

 

Click to Download FileUtils