Visual Studio - 运行批处理文件

时间:2010-07-01 17:10:45

标签: visual-studio visual-studio-2010

我想要什么

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

到目前为止我所知道的

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

我的解决方案有各种脚本/批处理文件,我希望有一种方法可以在解决方案文件夹或其他特定解决方案的地方创建快捷方式。

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

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

5 个答案:

答案 0 :(得分:12)

最好的方法是编写外部工具,然后根据当前加载的解决方案传入参数。

您可以将许多项目和解决方案特定变量传递给外部工具。 打开“外部工具”对话框,然后在列表框中选择您的工具。 您将看到外部工具的标题,以及指向要调用的脚本或批处理脚本的命令。 参数编辑框旁边有一个按钮,旁边有一个箭头。单击箭头,您将看到一个可以传递给外部工具的变量或参数的大列表。

因此,例如,您可以使用以下内容:

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

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

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

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

等...

答案 1 :(得分:12)

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

Add program dialog

答案 2 :(得分:11)

以下是关于如何通过右键单击文件添加外部工具来运行批处理文件的完整步骤,并选择"运行批处理文件",无论何时需要编辑文件,只需打开并编辑它。

以下是如何做到的......

创建一个名为"运行批处理文件"

的外部工具
    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.

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

希望它有所帮助。

enter image description here

enter image description here

答案 3 :(得分:1)

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

答案 4 :(得分:1)

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

由于您打算按需运行此脚本(而不是每次构建时运行),因此您需要将其从Configuration Manager中的构建中删除。不要忘记禁用为所有平台和配置构建项目。

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