Run Batch Files (DOS Commands) From Visual Studio

106 7
I recently downloaded some code from a website. The author had thoughtfully included several batch files in Solution Explorer that were necessary for the system. When you get beyond simple Windows Forms projects, you discover that a lot still has to be done at a console command line. But for reasons I can not understand, Visual Studio doesn't include a way to execute them directly. Fortunately, it's not hard to add one.


The next few paragraphs are for those of you who may be asking, "What's a batch file?"

Those of us who were programming PC's as soon as IBM introduced them remember batch files - and the original BASICA programming language - as the few ways that you could actually write programs. Most of us became experts at programming DOS commands back then because we had to.

Batch files might be called scripts or macros in another context. They're just text files filled with what we used to call DOS commands. For our purposes here, we will be working with a really simple one:

@ECHO offECHO Hello About Visual Basic!@ECHO on
Because I like to explain everything ...
  • The "@" suppresses the display of the current statement to the console. (So, the command "ECHO off" is not diplayed.)
  • 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 following, ie, "Hello About Visual Basic!
  • And @ECHO on switches the ECHO function back on so anything following will be displayed again.



    So, all of this was just to ensure that the only thing you actually see in the console window is the message. Programming DOS batch files can be tedious!

    The key to executing a batch file directly in Visual Studio is to Add one using the External Tools selection of the Tools menu. We're going to ...
    1. Create a simple batch program that executes other batch programs.
    2. Reference that program using the External Tools selection in Visual Studio.

    And just to make things more complete, we will also add a reference to Notepad in the Tools menu. I'll explain why later.

    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 ...". By the way, you can get a fast explanation of any console command - that is, "DOS" - using this syntax in a command prompt window:

    <command> /?
    Save this file using any name qualified with the file type ".bat". I used the name "RunBat.bat". You can also save it in any location but the Visual Studio directory in Documents is a good place. That's what I used.

    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. In my case, it was:

    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 again, you can also add the RunBat.bat file to the External Tools a different way as shown below:

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


    Some pages suggest that you make this file the "default editor" in the External Tools. I don't. That will make Visual Studio use RunBat.bat for files that are not batch files. Instead, it's not too much trouble to execute the batch file by selecting "Open With..." from a context menu as shown below:

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


    There is one more interesting thing to be discovered. As I noted earlier, a batch file is just a text file that is qualified with the .bat type (.cmd works too). You might, then, think that you can use the "Text File" template in Visual Studio to add one to your project. Good guess, but wrong. As it turns out, a "Visual Studio Text File" is not a text file. To demonstrate this, right click the project and use the "Add > New Item ..." to add a Text File to your project. You have to change the name so it ends in .bat. Enter the very 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 an error:

    '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 some header information to the front of every file. You need an editor, like Notepad, that doesn't. The solution here is to simply add Notepad as an External Tools too. Use Notepad to create a batch file. But remember, once you save your batch file, you still have to add it to your project as an "existing item".
    Subscribe to our newsletter
    Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
    You can unsubscribe at any time

    Leave A Reply

    Your email address will not be published.