ToClip: Send Text to the Windows Clipboard

By Mitch Stuart
Copyright © 2003-2004 FullSpan Software  -  Usage subject to license
Software Version: 1.4  -  Document Version: $Revision: 1.2 $, $Date: 2004/01/05 03:30:55 $

Contents

1. Introduction

2. Using ToClip

2.1. Send To Clipboard (as name)

2.2. Command Line

3. Installing ToClip

4. Programming and Testing

1. Introduction

ToClip is a small Windows utility program to send text to the Windows clipboard. Its primary purpose is to replace the Send File To Clipboard As Name feature that is missing from the Microsoft Windows XP PowerToys collection. It also has two additional features: it can expand mapped drive letters to their UNC path names, and it can copy text directly to the clipboard.

Beginning with Windows 95, Microsoft has made available an unsupported set of utilities called the PowerToys. There are several versions of the PowerToys available (this may not be a complete list):

One of the most useful of the PowerToys is Send To Clipboard (as name). It allows you to right-click on a filename in Windows Explorer, and send the fully-qualified name of that file to the clipboard. For example, this is useful when you are composing an email and you want to send a colleague a link to a file on the network.

However, for some reason, the Send To Clipboard (as name) PowerToy is not included in the most recent versions of the PowerToys, such as the Windows XP version. So I wrote ToClip to replace it.

Getting ToClip

2. Using ToClip

ToClip has a command-line syntax that supports operation in two modes:

2.1. Send To Clipboard (as name)

Right-click on a file or directory name in Windows Explorer, select Send To, then select Clipboard (as name). You can then paste the fully-qualified filename wherever you need it: an email, a document, a program command line or dialog box, etc. The following screenshot shows ToClip being invoked from the Send To menu:

ToClip example screenshot

From the Send To menu, you can also select Clipboard (as UNC name) to expand mapped drive letters. Universal Naming Convention (UNC) names are a standardized way to refer to a resource located on a Windows network. For example, let's say you have the network share \\accounting\reports mapped to your local computer as K:, and you are browsing the K:\current directory in Windows Explorer.

If you select Send To Clipboard (as name) for the file summary.xls, the text K:\current\summary.xls will be placed on the clipboard. But if you select Send To Clipboard (as UNC name), the text \\accounting\reports\current\summary.xls will be placed on the clipboard. This is convenient when documenting or emailing network links, because other users may have the network share mapped with a different drive letter (or not mapped at all). With a UNC name, the location of the file will be clear regardless of their local mappings.

You can select multiple files or directories (using the standard Ctrl-click or Shift-click mouse actions), and send all of the names to the clipboard. When you paste the result, each name will be on a separate line.

2.2. Command Line

You can send text from the standard input to the clipboard. For example:
  dir | toclip
will put the directory listing on the clipboard. And:
  toclip <somefile.txt
will copy the contents of somefile.txt onto the clipboard.

ToClip only handles text data - it does not handle binary data (such as images).

Finally, you can tell ToClip to directly send the command-line arguments to the clipboard. This is how the Send To Clipboard (as name) feature works, except in this case you are directly typing the arguments instead of right-clicking the filename(s). For example:

  toclip -a Hello World
will put this text on the clipboard:
  Hello
  World
And:
  toclip -a "Hello World"
will put this text on the clipboard:
  Hello World

3. Installing ToClip

  1. Copy toclip.exe to a directory that is on your system path, for example, c:\bin\toclip.exe.

  2. Create a new Send To item (search in Windows Help for "send to" if you need help with this). For the Target, enter a command like the following (adjust the paths for your environment):
      C:\bin\toclip.exe -a
    and for the name, enter:
      Clipboard (as name)
    or another name of your choice. (This is the name that will be displayed in the Send To menu.)

  3. Create a new Send To item for the UNC feature. For the Target, enter a command like the following (adjust the paths for your environment):
      C:\bin\toclip.exe -a -u
    and for the name, enter:
      Clipboard (as UNC name)
    or another name of your choice. (This is the name that will be displayed in the Send To menu.)

  4. Test the installation by right-clicking on a filename in Windows Explorer. Select Send To, then select Clipboard (as name). Open an empty file in Notepad or another text editor, press Ctrl-V (or select Edit/Paste from the editor's menu) and you should see the fully-qualified filename that you right-clicked.

4. Programming and Testing

ToClip has been tested on Windows 2000 and Windows XP.

ToClip is written in C. I built toclip.exe in the Cygwin development environment, using the gcc compiler, and the MinGW windows libraries, all of which are easily downloaded and installed by the Cygwin installer. However, ToClip is a generic program; you should be able to compile it with any Windows C compiler/environment (although I haven't tested any others). To build ToClip in the Cygwin environment, use the build.sh file included with the distribution.

ToClip limits the size of the data placed on the clipboard to no more than 64,000 characters.

ToClip uses the Windows API function WNetGetUniversalName to expand mapped drive letters to their UNC paths.