欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

从Visual Studio运行批处理文件(DOS命令)

程序员文章站 2023-12-30 14:14:04
...

Microsoft Visual Studio integrated development environment doesn't run DOS commands, but you can change that fact with a batch file. When IBM introduced PCs, batch files and the original BASIC programming language were among the few ways to write programs. Users became experts at programming DOS commands.

Microsoft Visual Studio集成开发环境不运行DOS命令,但是您可以使用批处理文件更改该事实。 当IBM引入PC时,批处理文件和原始的BASIC编程语言是编写程序的几种方法。 用户成为编程DOS命令的专家。

关于批处理文件 ( About Batch Files )

Batch files might be called scripts or macros in another context. They're just text files filled with DOS commands. For example:

批处理文件在另一个上下文中可能称为脚本或宏。 它们只是充满DOS命令的文本文件。 例如:

@ECHO off
ECHO Hello About Visual Basic!
@ECHO on
  • The "@" suppresses the display of the current statement to the console. So, the command "ECHO off" is not displayed.

    “ @”禁止向控制台显示当前语句。 因此,不会显示命令“ ECHO off”。
  • "ECHO off" and "ECHO on" toggles whether statements are displayed. So, after "ECHO off," statements are not displayed.

    “关闭ECHO”和“打开ECHO”可切换是否显示语句。 因此,在“ ECHO off”之后,不会显示语句。
  • "ECHO Hello About Visual Basic!" displays the text "Hello About Visual Basic!"

    “关于Visual Basic,您好! 显示文本“ Hello About Visual Basic!”。
  • "@ECHO on" switches the ECHO function back on so anything following is displayed.

    “ @ECHO on”将重新打开ECHO功能,以便显示以下内容。

All of this was just to ensure that the only thing you actually see in the console window is the message.

所有这些只是为了确保您在控制台窗口中实际看到的唯一内容是消息。

如何在Visual Studio中执行批处理文件 ( How to Execute a Batch File in Visual Studio )

The key to executing a batch file directly in Visual Studio is to Add one using the External Tools selection of the Tools menu. To do this, you:

直接在Visual Studio中执行批处理文件的关键是使用“工具”菜单的“外部工具”选择添加一个。 为此,您:

  1. Create a simple batch program that executes other batch programs.

    创建一个执行其他批处理程序的简单批处理程序。
  2. Reference that program using the External Tools selection in Visual Studio.

    使用Visual Studio中的“外部工具”选择引用该程序。

To be complete, add a reference to Notepad in the Tools menu.

要完成操作,请在“工具”菜单中添加对记事本的引用。

执行其他批处理程序的批处理程序 ( A Batch Program That Executes Other Batch Programs )

Here's the batch program that will execute other batch programs:

这是将执行其他批处理程序的批处理程序:

@cmd /c %1
@pause

The /c parameter carries out the command specified by string and then terminates. The %1 accepts a string that the cmd.exe program will try to execute. If the pause command wasn't there, the command prompt window would close before you could see the result. The pause command issues the string, "press any key to continue."

/ c参数执行字符串指定的命令,然后终止。 %1接受cmd.exe程序将尝试执行的字符串。 如果没有暂停命令,则命令提示符窗口将关闭,然后才能看到结果。 暂停命令发出字符串“按任意键继续”。

Tip: You can get a fast explanation of any console command—DOS—using this syntax in a command prompt window:

提示:您可以在命令提示符窗口中使用以下语法来快速解释任何控制台命令DOS:

/?

Save this file using any name with the file type ".bat."  You can save it in any location, but the Visual Studio directory in Documents is a good place. 

使用文件类型为“ .bat”的任何名称保存该文件。 您可以将其保存在任何位置,但是“文档”中的Visual Studio目录是一个不错的地方。

将项目添加到外部工具 ( Add an Item to External Tools )

The final step is to add an item to the External Tools in Visual Studio.

最后一步是将一个项目添加到Visual Studio中的外部工具。

--------Click Here to display the illustration--------

--------点击此处显示插图--------

If you simply click the Add button, then you get a complete dialog that allows you to specify every detail possible for an external tool in Visual Studio.

如果仅单击“ 添加”按钮,则将显示一个完整的对话框,可让您为Visual Studio中的外部工具指定所有可能的详细信息。

--------Click Here to display the illustration--------

--------点击此处显示插图--------

In this case, enter the complete path, including the name you used when you saved your batch file earlier, in the Command textbox. For example:

在这种情况下,请在“命令”文本框中输入完整的路径,包括您先前保存批处理文件时使用的名称。 例如:

C:\Users\Milovan\Documents\Visual Studio 2010\RunBat.bat

You can enter any name you like in the Title textbox. At this point, your new batch file executing command is ready. Just to be complete, you can also add the RunBat.bat file to the External Tools a different way as shown below:

您可以在“标题”文本框中输入您喜欢的任何名称。 至此,新的批处理文件执行命令已准备就绪。 为了完整起见,您还可以按照以下所示的不同方式将RunBat.bat文件添加到外部工具中:

--------Click Here to display the illustration--------

--------点击此处显示插图--------

Rather than make this file the default editor in External Tools, which will cause Visual Studio to use RunBat.bat for files that are not batch files, execute the batch file by selecting "Open With..." from a context menu.

而不是使该文件成为外部工具中的默认编辑器,这将导致Visual Studio对不是批处理文件的文件使用RunBat.bat,而是通过从上下文菜单中选择“打开方式...”来执行批处理文件。

--------Click Here to display the illustration--------

--------点击此处显示插图--------

Because a batch file is just a text file that is qualified with the .bat type (.cmd works too), you might think that you can use the Text File template in Visual Studio to add one to your project. You can't. As it turns out, a Visual Studio Text File is not a text file. To demonstrate this, right-click the project and use "Add > New Item ... to add a text file to your project. You have to change the extension so it ends in .bat. Enter the simple DOS command, Dir (display a directory contents) and click OK to add it to your project. If you then try to execute this batch command, you get this error:

因为批处理文件只是一个用.bat类型限定的文本文件(.cmd也可以),所以您可能认为可以使用Visual Studio中的“文本文件”模板将一个文件添加到项目中。 你不能 事实证明,Visual Studio文本文件不是文本文件。 为了演示这一点,请右键单击该项目,然后使用“ 添加 > 新建项...”将一个文本文件添加到您的项目中。您必须更改扩展名,使其以.bat结尾。输入简单的DOS命令Dir (显示目录内容),然后单击确定以将其添加到您的项目中。如果随后尝试执行此批处理命令,则会出现以下错误:

'n++Dir' is not recognized as an internal or external command,
operable program or batch file.

That happens because the default source code editor in Visual Studio adds header information to the front of every file. You need an editor, like Notepad, that doesn't. The solution here is to add Notepad to External Tools. Use Notepad to create a batch file. After you save the batch file, you still have to add it to your project as an existing item.

发生这种情况是因为Visual Studio中的默认源代码编辑器将标头信息添加到每个文件的开头。 您不需要像记事本这样的编辑器。 这里的解决方案是将记事本添加到外部工具。 使用记事本创建批处理文件。 保存批处理文件后,您仍然必须将其作为现有项添加到项目中。

翻译自: https://www.thoughtco.com/run-batch-files-from-visual-studio-3424204

上一篇:

下一篇: