[Update: I devised an improved method to create command prompts for Visual Studio 2008, 2010 and 2012 all-in-one.]

I open a command prompt so often that it has become a burden to have to use a stock cmd shortcut and cd to the right directory. It is easy to add an option to the context menu that comes up when you right click a directory on Windows Explorer. I have been experimenting with various methods to easily do this. Here is two of them.

First Method

This uses environment variables from your system to find the necessary VC files necessary. I prefer this method, because the context menu option does not require that you know where Visual Studio 2010 and VS are installed. It is also trivial in that you just execute it from a cmd window and you’re ready to go. I provide here two batch files that you can execute on your system (of course at your own risk!!) to set this up right away:

Below is a brief explanation of what the individual steps in the batch files do. (Do not run these from a command prompt, use a batch file, like those provided above. You are not supposed to run the commands below on a command prompt as variable expansion will mess things up!!)

For 32-bit, we first create the context menu entry, which is what you see on the menu, by executing:

reg add HKEY_CLASSES_ROOT\Directory\shell\VS2010cmd-x86 /ve /f /d "VS2010 x86 Prompt"

Then for the command to be executed when you click on that menu entry, we execute (in one line):

reg add HKEY_CLASSES_ROOT\Directory\shell\VS2010cmd-x86\command 
        /ve /t REG_EXPAND_SZ /f /d "%%comspec%% /V:ON /K 
        """"""%%VS100COMNTOOLS%%VCVarsQueryRegistry.bat"""""" 32bit ^&^& 
        ^!VCINSTALLDIR^!vcvarsall.bat x86 ^&^& cd /d %%1"

Respectively for the 64-bit VS2010 cross compiler tools prompt, first create the context menu entry by executing:

reg add HKEY_CLASSES_ROOT\Directory\shell\VS2010cmd-x64 /ve /f /d "VS2010 x64 Prompt"

Then for the command to be executed when you right-click on that entry, execute (in one line):

reg add HKEY_CLASSES_ROOT\Directory\shell\VS2010cmd-x64\command
        /ve /t REG_EXPAND_SZ /f /d "%%comspec%% /V:ON /K
        """"""%%VS100COMNTOOLS%%VCVarsQueryRegistry.bat"""""" 64bit ^&^&
        ^!VCINSTALLDIR^!vcvarsall.bat x86_amd64 ^&^& cd /d %%1"

One can easily set this up for other versions of Microsoft Visual Studio. For example, you can exchange the VS100COMNTOOLS variable with VS90COMNTOOLS for a prompt to work with Visual Studio 2008.


Second Method

Another, less versatile method is to do the following. This has the limitation that it uses fixed paths, while the first one will work on any system. However with this method you have more control of what happens and you are not bound to use a batch script.

Open the Registry Editor (regedit.exe) add a registry key under:

HKEY_CLASSES_ROOT\Directory\shell

Name it anything (I named mine CommandPrompt) and give it a data value, for example VS2010 Command Prompt, this is the text that will appear in the context menu. Create a new key under this newly create CommandPrompt key and name it command. The value of this key should use vcvarsall.bat of your Visual Studio installation in order for the environment to be set properly when you open the cmd window. The following value for that key does it for my installation:

cmd /k ""D:\dev\msvc2010\VC\vcvarsall.bat"" x86 && cd /d %1

replace D:\dev\msvc2010 with the installation path of your visual studio and you’re done. To find out the right path to the vcvarsall.bat file you can look at the shortcut created by your VS installer. You can also replace x86 (for instance with ia64, amd64, x86_amd64, x86_ia64) to request that another environment is setup if you are cross-compiling for instance.

0