85

What I want to do is very simple, yet can't find the way to do it.
In my projects I have several .bat files (I know, I'm old) the perform some tasks like running SqlMetal, etc.

I just want to right-click the file and select "run" or something so the bat is executed. How do I do it?

Note: I know I can use Tools -> External Tools, but it's not what I'm looking for

5
  • Are you looking to do this during the build or just in general? Commented Apr 9, 2011 at 15:28
  • 1
    So what are you looking for? Tools | External Tools would be the obvious way to do this. What additional functionality are you looking for?
    – Ade Miller
    Commented Apr 9, 2011 at 15:28
  • @Ade: I'm looking at productivity :) Commented Apr 9, 2011 at 15:33
  • 1
    @Ade: if you have 20+ projects with 3+ bat files: How many menu entries do you end up with? Commented Apr 9, 2011 at 16:01
  • OK. Now I understand the problem. You can fix this, see my answer below.
    – Ade Miller
    Commented Apr 9, 2011 at 16:56

6 Answers 6

155

If each project has a few batch files associated with it then why not include them in the project and add a new external tool and link it with a custom menu item to run the tool? This will give you a list of batch files in each project and a context menu command to run them. The only downside is that it isn't file type specific (it will let you run any file as a batch file).

Here's how to do it...

Create an external tool called "Run batch file"

  1. Set the Command to: CMD.EXE
  2. Set the Arguments to: /c "$(ItemPath)"
  3. Set the Initial directory to: $(ItemDir)
  4. Check the "use output window" checkbox and then Apply to create the command
  5. Note where the new command appeared in the list of commands. The external commands are numbered from 1 starting below the divider bar. #1 is usually "Create GUID"

Now go to Tools -> Customize and select the commands tab.

  1. Select the Context menu radio button and select "Project and Solution Context menus | Item" from the dropdown.
  2. Now use "Add Command..." to add a new command
  3. In the Categories list select "Tools"
  4. From the commands select the "External Command #" that corresponds to the position of the "Run Batch file" custom command you noted the number of in step 5 above.
  5. Move it to the correct position in the list add keyboard shortcuts etc.
  6. Close the dialog.

Now right click on the batch file and you should see a "Run batch file" menu item. This will execute the batch file and show its output in the VS Output window.

4
  • 1
    I got some IO exceptions for the program I was running from a batch file. If anyone else runs into these, don't check the "use output window" checkbox to have the batch file run in it's own cmd window.
    – DLeh
    Commented Aug 4, 2014 at 13:00
  • Here's how to create an external tool.. see the Adding New Tools section. msdn.microsoft.com/en-us/library/76712d27.aspx Commented Mar 7, 2017 at 4:20
  • Is it possible that this context menu item only appears on .bat files? It's quite strange, if I click on it on a .cs file it opens the class, in another instance of Visual Studio.
    – Wolfsblvt
    Commented Nov 28, 2018 at 11:46
  • 1
    Used this way too often, would up it each time if I could!
    – KarmaEDV
    Commented Apr 25, 2019 at 12:19
38
  • Right click the batch file in the Solution Explorer
  • Select “Open With…” from the context menu
  • Click “Add…”
  • In the “Program name” textbox, enter powershell.exe
  • In the “Friendly name” textbox enter “PowerShell”
  • Select “Set As Default”
  • Click OK

via

1
  • In Visual Studio 2017, "Program name" is now referred to as "Program". Commented May 17, 2018 at 20:01
14

Check out the Open Command Line Visual Studio extension.

As per the description, it features:

  • Opening of a command line at the root of the project.
  • Support for all consoles such as CMD, PowerShell, Bash etc.
  • Syntax highlighting, Intellisense and execution of .cmd and .bat files.

For files in your solution, a context-menu button shows up.

Execute batch file

Alternatively, the keyboard shortcut Shift+Alt+5 can be used when editing a batch file. This makes it really easy and fast to execute any batch file - even ones that are not part of your project.

1
  • Handy plugin, and super easy to setup!
    – CrazyTim
    Commented May 3, 2019 at 0:12
6

Right-click a file in the Solution Explorer window, Open With, click the Add button to add your .bat files. Use %1 in your .bat to get the path to the selected file.

3
  • 2
    Can work if I make another bat or exe that execute the bat. Nice solution also, but the Ade solution is more straightforward to me. Commented Apr 9, 2011 at 17:21
  • 2
    Batch should contain: cmd.exe /c %1 Commented Oct 5, 2012 at 11:06
  • This does not work for me in VS2010. If I type anything other than a valid path, it throws an error. Commented Jul 28, 2015 at 20:05
5

Couldn't get any of the above to quite work, but this did (VS 2019/2022):

  1. Right click a .bat file in Solution Explorer and Open With...
  2. Add...
  3. Program: CMD.exe
  4. Arguments: /c %1
  5. Friendly name: Command
  6. OK and Set as Default for double clicking of batch files to run from Solution Explorer as the default (or right click Open With... and select Command if you don't want it the default).

Add Progream

1
  • Finally something that actually works, thanks !
    – Sadistik
    Commented Jun 9, 2022 at 22:19
0

Merely a suggestion: This is a scenario which can be done with the VsCommandBuddy extension. The following configuration is an example:

{
    "cmdname": "mk",
    "title": "Run MK.BAT",
    "description": "Run the MK bat file.",
    "cwd": "$(SolutionDir)",
    "filename": "cmd.exe",
    "arguments": "/C mk.bat",
    "async": false
}

You can assign shortkeys as well, so you can invoke your batch files with shortcut keys. Output comes in your output pane.

Not the answer you're looking for? Browse other questions tagged or ask your own question.