65.9K
CodeProject is changing. Read more.
Home

Text File Combiner

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.87/5 (7 votes)

Oct 8, 2007

CPOL

3 min read

viewsIcon

25305

downloadIcon

612

A utility to get combined text of multiple text files

Screenshot - TextFileCombiner.jpg

Introduction

This article describes a utility I wrote to get combined text of multiple text files. From a quick initial review of the requirement, one would have thought about making a form that takes a directory and filter conditions to get the collection of required files. But I thought it would be easier to use the 'Send To' folder of Windows. (Remember it's better to work smart than working harder.) When files are right clicked in Windows Explorer and any of the options in Send To folder is selected, filenames are automatically passed to that application as command line arguments.

Using the Send To folder, we can send the files to be combined as arguments to our program. Almost all the things you can do with custom form to get filtered files are possible with send to feature. For selecting multiple files in a hierarchy, we can do Windows search on that folder using the search/filter conditions, then select multiple files from the resulting set of files and send to our program.

Implementation

When the application starts, it checks whether the shortcut for the application exists in the current user 'Send To' folder. If it does not exist, then it creates one. There is no function in the .Net Framework library to create a shortcut. One possibility is that we create a shortcut using Windows script host. WSH includes several COM automation methods that allow you to do several tasks easily through the Windows Script Host Object Model. However using COM objects from .NET requires a runtime callable wrapper - RCW (interop DLL) to be created before using the COM object. As I wanted my application to be a single EXE solution, I opted to create a VB script file in a temporary folder and run that file using the System.Diagnostics.Process class to create the shortcut.

Here is the code to create the shortcut in Send To folder:

string tmpFileName = Environment.GetEnvironmentVariable("TEMP")+ 
    "\\" + Guid.NewGuid().ToString()+ ".vbs";
StreamWriter sw = new StreamWriter(tmpFileName);
sw.WriteLine("set WshShell = CreateObject(\"Wscript.shell\")");
sw.WriteLine("strSendTo = WshShell.SpecialFolders(\"SendTo\")");
sw.WriteLine("set oMyShortcut = WshShell.CreateShortcut(strSendTo + 
    \"\\Text File Combiner.lnk\")"); 
sw.WriteLine("oMyShortcut.TargetPath = \"" + Application.ExecutablePath + "\"");
sw.WriteLine("oMyShortCut.Save");
sw.Flush();
sw.Close();

Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo(tmpFileName);
p.StartInfo=psi;
p.Start();
p.WaitForExit(); //wait for script to exit
File.Delete(tmpFileName);

The application then checks whether filenames are passed as arguments. If no filename is passed, it shows a message box. Next the application loops through the filenames passed as arguments, reads the content of file if it's a text file and adds it to the textbox. I am using the technique used by VSS to check whether a file is text or binary. The program checks the file for NULL characters (bytes with value 0). If it finds such a character, it identifies the file as binary. Following is the main loop that combines the text of files passed as arguments.

int i = 0;
StringBuilder sb = new StringBuilder();
String strFile = "";
string strErrorFiles = ""; 

while (i < sArgs.Length)
{
    TextReader tr = new StreamReader(sArgs[i]);
    strFile = tr.ReadToEnd();
    if (VerifyText(strFile)) // file is a text file
    {
        sb.Append(strFile);
        sb.Append("\r\n");
    }
    else // file is not a text file
    {
        strErrorFiles += sArgs[i] + "\r\n";
    }
    i = i + 1;
    tr.Close();
}

Points of Interest

Although it is a small utility, it demonstrates following techniques:

  • Using the built-in Send To feature to get command line arguments
  • Using Windows Script Host (WSH) to create shortcuts to applications
  • Running other applications, dynamic script or batch files using Process class
  • Waiting for an application to exit before control is passed to calling program

History

  • Initial version: 8th October, 2007
  • Updated: 2nd November, 2007 (Modified function to check whether file is binary)