开发者

Visual Studio - Run Batch File

What I would like

I would like to easily run a batch file without leaving Visual Studio. The batch files are not always the same, they change depending on the solution I'm working with.

What I know so far

I know you can create custom shortcuts in the Tools section of Visual Studio (Under External Tools).

My solutions have various scripts/batch files and I wish there was a way to create shortcuts under a solution folder or some other place that is solution specific.

I can create the scripts but there doesn't seem to be a way to run the script. I don't like having to open Windows Explorer each time I want to run a script.

Perhaps an add-in that would start a process开发者_如何学JAVA with the selected file in the solution explorer? Or something similar?


Here is a full steps on how to add the external tool to run the batch files by right click on the file and select "Run the batch file", also whenever you need to edit the file, just open and edit it.

Here's how to do it...

Create an external tool called "Run batch file"

    1) From Tools-> External Tools, create a new and put the below parameters:
    2) Set the Command to: CMD.EXE
    3) Set the Arguments to: /c "$(ItemPath)"
    4) Set the Initial directory to: $(ItemDir)
    5) ![DO NOT Check the "use output window" check box and then Apply to create the command
    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"
To make it easy to remember you can move the new  command to the top, to be the number one command in the list.][1]

    6) Now go to Tools -> Customize and select the commands tab.
    7) Select the Context menu radio button and select "Project and Solution Context menus | Item" from the drop down.
    8) Now use "Add Command..." to add a new command
    9) In the Categories list select "Tools"
    10) 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.
    11) Move it to the right position in the list add keyboard short cuts etc.
    12) Close the dialogue.

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

Hope it helps.

Visual Studio - Run Batch File

Visual Studio - Run Batch File


I did this by right clicking the batch file and choosing "Open With", then I added a new editor and used explorer.exe (and then set that to be the default editor).

Visual Studio - Run Batch File


The best way to proceed is to write an external tool, and then you can pass in parameters based on your current solution that is loaded.

There are many project and solution specific variables you can pass to your external tool. Open up the 'External Tools' dialog, and select your tool in the list box. You will see the title of your external tool, as well as the command that points to the script or batch script you want to call. The arguments edit box has a button next to it with an arrow next to it. Click the arrow and you will see a big list of variables, or parameters, you can pass to your external tool.

So, for instance you can use the following:

$(ProjectDir) - The full path directory of the project you are working on. for instance "C:\builds\myproject"

$(ProjectPath) - The full path name of the vcproj you are working on. for instance "C:\builds\myproject\foo.vcproj"

$(ProjectName) - The name of the project. for instance "foo"

$(SolutionDir) - The full path directory of the solution that is currently loaded. for instance "C:\builds\mysolution"

etc...


Will the Build Event hooks (pre-build, pre-link, post-build) be of any use to you? Also you can check out the Custom build setup too. Those are part of the solution.


You can add a new Makefile project to your solution. A Makefile project is a simple Visual Studio project whose build action is any command line you want. It is listed under the Visual C++ - General category in the new project dialog. In your case, just set the build command to invoke your batch script. Then, to execute your script while working in your solution, just right click on the Makefile project in the Solution Explorer and choose the Build context menu item.

Since you intend to run this script run on-demand (as opposed to running each time you build) you will want to remove it from the build in the Configuration Manager. Don't forget to disable building the project for all platforms and configurations.

Most of the macros are available to the Makefile project's build command line, although perhaps some that are associated with your main project will not have the correct value.


Running Visual Studio 2022 on Windows 11 I use this custom "Open With..." command:

Visual Studio - Run Batch File

Note that the additional parameter $(ItemPath) is not necessary and leads to an error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜