The shortest, most useful program that I have ever written


When celebrities receive awards, they often express appreciation for the contributions of all the “little people” who made their success possible. While this may seem like a back-handed acknowledgement, it is probably the most  inclusive way to recognize people in the entertainment industry in which small stature and large headedness has reached pandemic proportions.

Today I am going to follow that lead, sans golden naked man statue, and give a shout out to one of my most successful “little programs.”

Introducing cmd2clip.exe

Although the details are a little fuzzy, I wrote cmd2clip and began using it around 1996 and have used it on a daily basis for over a decade. It does one very simple, but incredibly useful thing. It copies whatever is passed to it on the command line to the Windows clipboard.

Here is the original (VB) source code in all its diminutive glory:

Public Sub Main()
Clipboard.Clear
Clipboard.SetText Command$
End Sub

What it does

While it may not immediately seem all that useful, context is everything. This program is intended to live in the  C:\Documents and Settings\yourpofile\SendTo\ directory which adds it to the “Send To” shortcut menu in explorer that is displayed when you right-click on an object in explorer.

Cmd2Clp SendTo Menu

This provides a quick way to copy the full path to a file or directory onto the clipboard making it easy to e-mail a path to someone or insert it into a configuration file, even if it is at the end of a long path.

 The Update

In the years since I originally wrote this in VB4, I’ve only had to update it once.  In exchange for around 30 extra lines including white-space and comments, I modified the utility to provide better formatted output when selecting multiple objects or those with spaces in the path.

Public Sub Main()
'The SendTo command will pass in the following on the command line:
' -If multiple files are selected, they are space delimited
' -If there is a space in the path of a file it is enclosed in quotation marks.

Const DelimiterBetweenFilenames As String = vbCrLf

Dim FileNames As String
Dim EvalCharOffset As Long Dim EvalChar As String
Dim AreQuotesOpen As Boolean: AreQuotesOpen = False

For EvalCharOffset = 1 To Len(Command$)
EvalChar = Mid(Command$, EvalCharOffset, 1)
If EvalChar = """" Then
'Don't append quotes to output, just toggle the AreQuotesOpen status
AreQuotesOpen = Not AreQuotesOpen
Else
If (EvalChar = " ") And (Not AreQuotesOpen) Then
'A space outside of quotes indicates the start of a new path, add a delimiter
FileNames = FileNames & DelimiterBetweenFilenames
Else
FileNames = FileNames & EvalChar
End If
End If
Next EvalCharOffset

Clipboard.Clear
Clipboard.SetText FileNames
End Sub

What’s Next

Although I’d love to get the line-count back down using the pre-tokenized args parameter provided in the Main() method for both C# and VB.NET, I doubt I’ll bother updating unless I dream up some new functionality that I need that won’t drive up the complexity considerably.

The most likely candidate is to detect when the path contains a locally mapped path and convert it to its canonical format to make it more appropriate for e-mailing to someone else. For example, changing X:\myfile.xml to \\myserver\fileshare\myfile.xml.

Why I am proud of this little utility

Admittedly, this hardly qualifies as code golf, and there is absolutely no major technical accomplishment demonstrated by this widget. There is just something about the combination of brevity and utility of this app that makes me feel good about it.

I suppose it appeals to my notion that less is more, at least to the point where you start obfuscating more than simplifying. I believe that SLOC metrics should work like golf scores, the lower the better.

How about you?

I’d love to hear about your mighty-mouse apps.  Feel free to brag about them in the comments.

Download Link

By request, here is a link to download the compiled version.

About these ads

7 Responses

  1. Nice and simple.

    I use something similar to add a the company letterhead as a background to PDFs: I use a simple word processor template that contains just the text on a blank page, no headers, no background then save it as a PDF.

    I just need to right-clik on the PDF file and then ‘Send To PDF Watermark’ to add the company letterhead, itself a PDF, and combine the two.

    Saves me a lot of space and it’s easy to modify the company letterhead or make multiple versions for different circumstances without having to change the wordprocessor templates.

    I used pdftk and simple batch files to provide the functionality.
    The free PDF manipulation utility can be found at http://www.accesspdf.com/pdftk/

  2. Any chance that there’s a download link that I’m just missing?

    • But, the entire source code is posted in the article!

      J/k: I’ll throw a link up on the article on sometime on Monday.

    • I’ve updated the article to include a download link to the compiled version.

  3. cool! I agree on the LOC stats, the lower the better :-)

    I feel the same way about a tool I recently made: http://vsclean.codeplex.com/

  4. Thanks. I’ve translated the updated version to c#, and it’s back to one line (2 if you still want to clear the clipboard first):

    Clipboard.SetText(String.Join(“\r\n”, args));

    (need [STAThread] on the main method and System.Windows.Forms referenced in the project.

    • Heh, thanks. This is perhaps the only program that you could open source using Twitter.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 128 other followers

%d bloggers like this: