Visual Studio - Run Batch File

后端 未结 5 2252
别跟我提以往
别跟我提以往 2021-02-07 08:04

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 solutio

相关标签:
5条回答
  • 2021-02-07 08:17

    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).

    Add program dialog

    0 讨论(0)
  • 2021-02-07 08:18

    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...

    0 讨论(0)
  • 2021-02-07 08:36

    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.

    0 讨论(0)
  • 2021-02-07 08:39

    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.

    0 讨论(0)
  • 2021-02-07 08:43

    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.

    enter image description here

    enter image description here

    0 讨论(0)
提交回复
热议问题