Saturday, November 23, 2019

Run Batch Files (DOS Commands) From Visual Studio

Run Batch Files (DOS Commands) From Visual Studio Microsoft Visual Studio integrated development environment doesnt 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. About Batch Files Batch files might be called scripts or macros in another context. Theyre just text files filled with DOS commands. For example: 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 and ECHO on toggles whether statements are displayed. So, after ECHO off, statements are not displayed.ECHO Hello About Visual Basic! displays the text Hello About Visual Basic!ECHO on switches the ECHO function back on so anything following is displayed. All of this was just to ensure that the only thing you actually see in the console window is the message. 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: Create a simple batch program that executes other batch programs.Reference that program using the External Tools selection in Visual Studio. To be complete, add a reference to Notepad in the Tools menu. A Batch Program That Executes Other Batch Programs Heres 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 wasnt there, the command prompt window would close before you could see the result. The pause command issues the string, press any key to continue. Tip: You can get a fast explanation of any console command- DOS- using this syntax in a command prompt window: /? 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.   Add an Item to External Tools The final step is to add an item to the External Tools in 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. 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: 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. 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 cant. 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: nDir 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 doesnt. 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.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.