Execute CMD files directly from Visual Studio

This will be the first post to start a category about links to resources that have helped me with various development efforts.  My primary reason for starting this category is to have a sort of blogged favorites listing.  I will also discuss how I have applied this information in my own efforts and any extra twist or tweak that I may have added.  I hope you find the links and information helpful in your own endeavors! Now on to how to execute a .CMD file directly from Visual Studio along with why I wanted to.

What started the quest

We develop a large-scale web centric enterprise platform on which applications are rapidly built and applied to any vertical market.  We will call this platform the Core and the vertically built applications Extensions.  The Core and Extensions are all Web Applications.  Having several web application projects one quickly finds the need to share web pages, web services, user controls and other web site resources between multiple web applications.  Discussing this configuration is an article within itself that maybe I will write more about soon but for now I will keep it to the simple fact that there is a website project in which common resources are developed and compiled into a library component.  Extension web applications can then reference this compiled assembly to use the shared features.  The issue is that non-compiled resources such as JavaScript, CSS and image files do not become available in the Extension web application by simply referencing the common assembly.  These common resources are maintained in the common website in a folder aptly named Common.  The folder structure looks like this:

imageNow in a normal build process these resources are copied to the Web Application by using the Post Build Event of the Web Application that is consuming them.  However the type of resources stored in these folders do not need a build to be used.  Furthermore, during normal development these files could change often and having to execute a build to start using the changes is not desirable.  This enters my quest for how to efficiently copy the files outside of the build processing events.

My quest ended after finding Rick Glos’ article about how to run .CMD files from Visual Studio.  Here are the steps I took:

1 – First I added a .CMD file to my Extension web application.  I actually added a text file and renamed the file extension.

2 – Next I added the contents of the .CMD file


For more information on RoboCopy


3 –External Tools DialogNext I added an External Tool to Visual Studio to use CMD.exe and execute the selected .CMD file. Within Visual Studio, select the Tools menu and External Tools… menu item.  The External Tools dialog will open. image

    • Click ‘Add
    • Provide a Title
    • Command is the path to CMD.exe typically C:\Windows\System32\CMD.exe
    • Arguments are /C $(ItemPath) which will execute the command and then stop.
    • Set the Initial Directory to $(ItemDir)
    • Finally I chose to check ‘Use Output Window’


image4 – Optionally – Map a keyboard shortcut to the External Tool

In order to set a keyboard shortcut you will need to note the Menu contents position of your newly added External Tool in the Visual Studio’s External Tools dialog.  In our example image of that dialog you will note that the ‘Execute Common Copy’ external tool is position 4.  To set the keyboard shortcut for this or any other Visual Studio External Tool; from within Visual Studio select the Tools menu and Options… menu item.

This will display the Options Dialog.image Next, if it is not already expanded, expand the Environment node in the left pane.  Then select the Keyboard node.  Type Tools.Extern in the Show command containing textbox to help limit the commands.  Locate the command named Tools.ExternalCommand# where # is the previously noted position.  In our example the command is Tools.ExternalCommand4.  Place your cursor in the Press Shortcut keys textbox and press the desired key combination for executing your external command.  I chose Alt+F4.  Finish by clicking the OK button.

Now to execute the .CMD file command from within Visual Studio. Select the .CMD file in your project and press your shortcut key combination. eg. Alt F4.  Alternatively you can select the menu item in the Tools menu.  In the following linked article Rick Glos also shows how to execute the .CMD file from its context menu.

Rick Glos’ original article and my blogged favorites link:


You May Also Like

About the Author: Joseph Caudill

Leave a Reply

Fork me on GitHub