Click here to Skip to main content
15,881,139 members
Articles / Programming Languages / C#
Article

Text File Combiner

Rate me:
Please Sign up or sign in to vote.
3.87/5 (7 votes)
2 Nov 2007CPOL3 min read 24.9K   607   20   2
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:

C#
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.

C#
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)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGreat idea Pin
bigRahn9-Oct-07 3:51
bigRahn9-Oct-07 3:51 
GeneralRe: Great idea Pin
nwaheed9-Oct-07 20:14
nwaheed9-Oct-07 20:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.