Batch Files - Overview
Last Updated: 06/16/13
A batch file is a type of script that is interpreted through the command interpreter. Specifically, a batch file will be interpreted by cmd.exe. For those of you unfamiliar, this is the shell program for command prompt. Command prompt can be used to perform a various number of tasks, some of which can manipulate the internal workings of the operating system. Most batch files are relatively simple, and the type of operations you can use are limited. Microsoft came out with a more powerful shell called PowerShell.
Any tutorials written in this section will be specifically compatible with cmd.exe on Windows 7. A number of the topics shown here will be compatible with other versions of Windows; however, some of the calls will be specifically limited to Windows 7.
For a list of all of the native commands view this article from Microsoft. More commands can be added; however, the ones in that list will be supported on any of the systems explicitly stated in the article. This is also a good reference to get more details about a specific command.
My tutorials will assume that you know how to launch command prompt and that you can create a batch file. To launch command prompt, simply click on the start orb and type “cmd”. Then click on “cmd.exe”. The command prompt window will now appear. You can use this window to execute commands directly. For a basic overview of some commands check out this article.
Creating a batch file is quite simple; all you need to do is change the extension of the file to “.bat”. You can create the batch file in any text editor. By default, Windows hides the extensions for known file types. This can be quite irritating, especially if you’ve created a batch file, but saved it as a text file and only need to change the extension from “.txt” to “.bat”. This Microsoft article explains how to show extensions for known file types.
While it’s not required, it is a good idea to launch batch files directly from command prompt. This way you will be able to read whatever output the batch file displays, assuming that the script does not wait on user input to close the window.
To demonstrate how to make an extremely basic batch file, we will create a script that will print “Hello World” to command prompt. Start by opening up your favorite text editor, preferably not Microsoft Word. If you are looking for something that does syntax highlighting, Notepad++ is a good option. For now, I will simply assume that you only have notepad. You can launch notepad by click on the start orb, typing “notepad” and pressing the “Enter” key.
Copy and paste the below code into notepad and save the file to your desktop as “HelloWorld.bat”. The first line of the code will turn off printing the command it is executing to the shell. The second command simply prints the text after the word “echo ” to the shell.
@echo off echo Hello World
If you simply launched the batch file by double clicking on it, you would have only seen a quick flash on your screen. Instead, navigate to where you saved the batch file (assuming you are following this exactly, then it will be on your desktop) and call the batch file by name. Assuming you have a fairly default setup, you can change your path to your desktop by issuing the below command.
To run the program all you have to do is call it by name. Simply run the below command and you should see the correct output displayed. Note that the path to my desktop is most likely very different that yours will be, this is due to a custom setup I have. If you are unsure whether you are in the correct directory or not, simply execute the command “dir” to see if your batch file is there.