How to run a .bat from inside the IDE

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

54763 次浏览

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.

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.

  • 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

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.

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.

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