How to run cmd files from Visual Studio

by Rick Glos 2/27/2008 1:25:59 PM

This will take you a few minutes to setup but once your done, it will save you countless minutes.

It's not unusual to have scripts in your project or solution that automate tasks.  Everything from executing a powershell script, moving files around, or some other custom automation tool you may have written.

The default for double-clicking a cmd file in Visual Studio is for it to open the script for editing.

image

But what if you want to execute it?

One way is to right click the folder above the file, choose 'Open Folder in Windows Explorer', wait for that window to open, find the file with your eyes again, and double-click to execute it.

I tried using the 'Open With...' menu item and adding cmd but it doesn't allow you to pass in the file.

image

So what you end up with is an empty cmd prompt window that hasn't executed the script you thought it would.

Ok great so how did you do it?

On the tools menu you can add external tools and add arguments.  So Tools -> External Tools and a window will open that allows you to run with cmd and also pass in the initial directory as well as some additional arguments.

I created two:

One that terminates the window after executing

Title Run With Cmd
Command C:\Windows\System32\cmd.exe
Arguments /C $(ItemPath)
Initial directory $(ItemDir)

image

One that remains after executing

Title Run With Cmd and Remain
Command C:\Windows\System32\cmd.exe
Arguments /K $(ItemPath)
Initial directory $(ItemDir)

image

So now you can select the file in solution explorer, then select Tools -> Run With Cmd.

You could go one step further and add it to the context menu.

There's many ways to get into menu customize mode, one way is to choose View -> Toolbars -> Customize.  Make sure to select the 'Context Menus' toolbar and you'll notice a toolbar appear in your menu when you're in customize mode.

image

This next part is tricky.  Leave the customize dialog open - it's semi model, if you close it, you're out of edit mode.  Click Tools -> and you'll see you're new command listed something like 'External Command 3', you'll have to remember which ones you created (or yes you could go and customize that text as well).

Hold down control and left click (we want to copy this to the context menu not move it), slide your pointer over the 'Project and Solution Context Menus' menu item in the context menu toolbar, then down to 'Item', and drop it in there wherever you like.

image

Close the customize dialog.

Now you can right-click on a cmd file in Solution Explorer and select either 'Run With Cmd' or 'Run With Cmd and Remain'.

image

Later

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

programming

Related posts

Comments

3/1/2008 7:38:55 PM

Mandie

Echo and the Bunny Man.... WHAT THE HELL ARE YOU TALKING ABOUT?? IS THIS A MAZAK OR AN XPS OR SOMETHING????!!

Mandie us

3/3/2008 9:50:35 AM

Rick Glos

Just some work related stuff that I thought other people might find useful.

You and Ryan won't find those posts very enlightening since you don't use Visual Studio in your day jobs. You can ignore the posts tagged 'programming'.

Rick Glos us

4/10/2008 1:34:58 PM

Stan Blaauw

Glad I checked this out, it'll come in handy. I've got this legacy process of batch scripts that are pretty key for us and I was dreading moving it all over to vb. This'll take some of the pain away or at least delay it!

Stan Blaauw us

Add comment


(Will show your Gravatar icon)  

  Country flag





Live preview

7/23/2008 11:45:10 AM

Powered by BlogEngine.NET 1.2.0.0
Theme by Mads Kristensen

About the author

Rick Glos Rick Glos

Moved to the Portland, OR after living 35 years in Chicago, IL and Milwaukee, WI. Write software to pay the bills. Aquire bills by being interested in bicycles, computer gaming, movies, and books.

E-mail me Send mail

Calendar

<<  July 2008  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

Recent posts

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in