http:// jon-walsh.com / journal / 2008 / 12 / file-associations-and-scripts /

File Type Associations and Scripts [cmd.exe]

If you’re like me, you’re editing scripts more often than you’re using them. So it only makes sense to make the default program for a file type an editor rather than an executer. But while doing this, you may run into a little problem if you’re running them from Window’s Command Prompt. Instead of typing a quick command like script do-stuff, you have to type executer *absolute file ref* do-stuff.

The Problem

Since you’ve changed the default file type association to an editor, you obviously have to tell cmd.exe what program you’d like to use instead to execute it. This isn’t so much of a problem if you simply had to type executer script do-stuff, but unfortunately you now have to give an absolute file location of the script even if it can be found in the Window’s Path system variable. This is because cmd.exe will only use the system path for the initial program and not the following file reference (which will instead be classed as a relative file reference to the current directory).

The Solution

Use intermediate batch files. I’ve found this method very effective and easy to do. Here’s the process:

  1. Have a folder that is referenced in the Path system variable (you may already have something like this if you’ve previously been running scripts from cmd.exe).
  2. Instead of storing scripts in this folder, keep your batch files in here (with your scripts wherever you like).
  3. Create a new batch file for each script.

Here’s an example batch file for an md5 script I made:
python F:\dev\scripts\cmd\md5\md5.py %*
Now you can simply call it by the batch file name, such as md5 do-stuff and the batch file takes care of the rest for you. Note that %* is needed to pass any arguments on to the script.

No more “right-click, open with”! ;)

Posted on the 6th December 2008 at 6:20pm
In the category: Quick Fixes
This post was written by a mere human so errors are definitely possible, please let me know if you find any! :)
Missing “Shared Folders” Menu Item [Ubuntu] “There’s Probably No God”

Respond