Rick Glos Life in Portland, Oregon.

How to run windows batch files from Visual Studio 2010 Solution Explorer

11. August 2010 11:35 by Rick Glos in

This is an update to running cmd files from Visual Studio 2005 and Visual Studio 2008 post I did a couple years ago.  You can use this technique to run windows command files with the .bat and .cmd extensions.

Just as previously, we need to create at least one, and optionally two, external tools.

One that terminates the window after executing.

Here’s the values for you to copy+paste and screenshot.

Field Value
TitleRun With Cmd
Command%ComSpec%
Arguments/C $(ItemPath)
Initial Directory$(ItemDir)

screenshot

One that leaves the command window open after executing.  Useful if you didn’t put a ‘pause’ in your command file or if you want to leave the cmd window open for additional commands.

Field Value
TitleRun With Cmd and Remain
Command%ComSpec%
Arguments/K $(ItemPath)
Initial Directory$(ItemDir)

screenshot

Now we should see our new external tools available on the Tools menu.

image

However, we’d like to right-click on the file and run the cmd file via Solution Explorer like so:

image

Customizing this context menu in Visual Studio 2010 is abit different from context menu customization in VS2005/2008.

Click Tools –> Customize… to launch the Customize Dialog.

From that dialog we want to select the ‘Context menu’ radio option and the ‘Project and Solution Context Menus | Item’ from the drop down.  I’m going to put the two commands right under ‘Open With…’.  You can certainly do whatever you wish.  Start this process by clicking ‘Add Command’ button on the same dialog.

So here’s a screenshot with that above paragraph in a picture instead of words:

image

When we click ‘Add Command’, the Add Command Dialog will open.  Select ‘Tools’ from the Catgories list box on the left and find your external command from the command list box on the right.  Your command will be named something like ‘External Command {Number}’ where {Number} is the number in your list from your External Tools dialog.

image

Clear as mud?  I’m going to use this to make sure I add External Command 3 and External Command 4 to the context menu.

image

Now the ‘Project and Solution Context Menus | Item’ context menu should look like this in the Customize Dialog.

image 

Close that dialog and check out your new menu items by right clicking on a file in Solution explorer.  You should see your new external commands.

image

For extra credit, you can go back into the Customize dialog and move your commands around into spot your like and alos create a Group around them.

image

 

The finished product looks like this.

image

blog comments powered by Disqus