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
Now 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
robocopy "..\BehindTheCode.ExtensionCommonWeb\Common" "Common" /COPY:DAT /R:1000000 /W:30 /S /FP /E /XD bin App_Data ConfigSections My*Project obj /XF *.config *.vb* *.xml *.vs* *.sc* *.cmd
For more information on RoboCopy
3 –Next 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.
- 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’
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. 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: