Click here to Skip to main content
13,194,103 members (50,652 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 7 Oct 2007

Text File Combiner

, 2 Nov 2007
Rate this:
Please Sign up or sign in to vote.
A utility to get combined text of multiple text files
Screenshot - TextFileCombiner.jpg


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.


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(\"\")");
sw.WriteLine("strSendTo = WshShell.SpecialFolders(\"SendTo\")");
sw.WriteLine("set oMyShortcut = WshShell.CreateShortcut(strSendTo + 
    \"\\Text File Combiner.lnk\")"); 
sw.WriteLine("oMyShortcut.TargetPath = \"" + Application.ExecutablePath + "\"");

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

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
    else // file is not a text file
        strErrorFiles += sArgs[i] + "\r\n";
    i = i + 1;

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


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


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


About the Author

Pakistan Pakistan
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralGreat idea Pin
bigRahn9-Oct-07 3:51
memberbigRahn9-Oct-07 3:51 
GeneralRe: Great idea Pin
nwaheed9-Oct-07 20:14
membernwaheed9-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171018.2 | Last Updated 2 Nov 2007
Article Copyright 2007 by nwaheed
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid