代码之家  ›  专栏  ›  技术社区  ›  quip

Visual Studio-运行批处理文件

  •  13
  • quip  · 技术社区  · 16 年前

    我想要什么

    我想在不离开visualstudio的情况下轻松运行批处理文件。批处理文件并不总是相同的,它们的变化取决于我使用的解决方案。

    到目前为止我所知道的

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

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

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

    是否有一个外接程序可以用解决方案资源管理器中选定的文件启动进程?或者类似的?

    5 回复  |  直到 16 年前
        1
  •  12
  •   C.J.    16 年前

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

    有许多特定于项目和解决方案的变量可以传递给外部工具。 打开“外部工具”对话框,在列表框中选择您的工具。 “参数”编辑框旁边有一个按钮,旁边有一个箭头。单击箭头,您将看到一个大的变量或参数列表,您可以将其传递给外部工具。

    例如,您可以使用以下方法:

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

    $(ProjectName)-项目的名称。

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

        2
  •  13
  •   Shaun Rowan    13 年前

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

    Add program dialog

        3
  •  13
  •   InkHeart    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 output窗口中显示其输出。

    希望有帮助。

    enter image description here

    enter image description here

        4
  •  1
  •   yasouser    16 年前

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

        5
  •  1
  •   PacerX    12 年前

    可以将新的Makefile项目添加到解决方案中。Makefile项目是一个简单的visualstudio项目,它的生成操作是您想要的任何命令行。它在新项目对话框中被列在Visual C++一般类别中。在您的例子中,只需设置build命令来调用批处理脚本。然后,要在解决方案中执行脚本,只需在解决方案资源管理器中右键单击Makefile项目并选择Build上下文菜单项。

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