visual-studio Visual Studio - 运行批处理文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3160063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:11:19  来源:igfitidea点击:

Visual Studio - Run Batch File

visual-studiovisual-studio-2010

提问by quip

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.

我想在不离开 Visual Studio 的情况下轻松运行批处理文件。批处理文件并不总是相同的,它们会根据我正在使用的解决方案而变化。

What I know so far

到目前为止我所知道的

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

我知道您可以在 Visual Studio 的工具部分(在外部工具下)创建自定义快捷方式。

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.

我可以创建脚本,但似乎没有办法运行脚本。我不喜欢每次要运行脚本时都必须打开 Windows 资源管理器。

Perhaps an add-in that would start a process with the selected file in the solution explorer? Or something similar?

也许一个加载项会使用解决方案资源管理器中的选定文件启动进程?或者类似的东西?

采纳答案by C Johnson

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"

$(ProjectDir) - 您正在处理的项目的完整路径目录。例如“C:\builds\myproject”

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

$(ProjectPath) - 您正在处理的 vcproj 的完整路径名。例如“C:\builds\myproject\foo.vcproj”

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

$(ProjectName) - 项目的名称。例如“foo”

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

$(SolutionDir) - 当前加载的解决方案的完整路径目录。例如“C:\builds\mysolution”

etc...

等等...

回答by Shaun Rowan

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

我通过右键单击批处理文件并选择“打开方式”来完成此操作,然后我添加了一个新编辑器并使用了 explorer.exe(然后将其设置为默认编辑器)。

Add program dialog

添加程序对话框

回答by InkHeart

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.

现在右键单击批处理文件,您应该会看到“运行批处理文件”菜单项。这将执行批处理文件并在 VS 输出窗口中显示它的输出。

Hope it helps.

希望能帮助到你。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by PacerX

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.

您可以将新的 Makefile 项目添加到您的解决方案中。Makefile 项目是一个简单的 Visual Studio 项目,其构建操作是您想要的任何命令行。它列在新项目对话框中的 Visual C++ - 常规类别下。在您的情况下,只需设置构建命令来调用您的批处理脚本。然后,要在解决方案中工作时执行脚本,只需右键单击解决方案资源管理器中的 Makefile 项目并选择构建上下文菜单项。

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.

大多数宏可用于 Makefile 项目的构建命令行,尽管与您的主项目关联的一些宏可能没有正确的值。

回答by yasouser

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.

构建事件挂钩(预构建、预链接、后构建)对您有用吗?您也可以查看自定义构建设置。这些是解决方案的一部分。