CompTIA A_ Certification All-In-One Exam Guide, Seventh Edition - Michael Meyers [256]
Working with Batch Files
Batch files are nothing more than text files that store a series of commands, one command per line. The only thing that differentiates a batch file from any other text file is the .BAT extension. Take a look at Figure 15-18, and note the unique icon used for a batch file compared to the icon for a regular text file.
Figure 15-18 Text and batch file icons
You can create and edit batch files by using any text editor program—good old Notepad is often the tool of choice. This is the command-line chapter, though, so let’s dust off the ancient but still important Edit program—it comes with every version of Windows—and use it to create and edit batch files.
Get to a command prompt on any Windows system and use the CD\ command to get to the root directory (use C: to get to the C: drive if you’re not on the C: drive by default). From there, type EDIT at the command prompt to see the Edit program’s interface (Figure 15-19).
Now that you’ve started Edit, type in the two commands as shown in Figure 15-20. Make sure they look exactly the same as the lines in Figure 15-20.
Great! You have just made your first batch file. All you need to do now is save it with some name—the name doesn’t matter, but this example uses FIRST as the filename. It is imperative, however, that you use the extension .BAT. Even though you could probably figure this out on your own later, do it now. Hold down the ALT key to activate the menu. Press the F (File) key. Then press S (Save). Type in the name first.bat as shown in Figure 15-21. Press ENTER and the file is now saved.
Figure 15-19 Edit interface
Figure 15-20 Edit with two commands
Now that you’ve saved the file, exit the Edit program by pressing ALT-F and then pressing X (Exit). You’re back at the command prompt. Go ahead and run the program by typing FIRST and pressing ENTER. Your results should look something like Figure 15-22.
Super! The batch file created a folder and moved the prompt to focus on that folder. Don’t run the First batch file again or you’ll create another folder inside the first one.
Figure 15-21 Saving the batch file
Figure 15-22 Running the batch file
Let’s now get back to the root directory of C: and edit the FIRST.BAT file again. This time type EDIT FIRST.BAT and press ENTER. The batch file will come up, ready to edit. Now change the batch file to look like Figure 15-23. Use the ARROW keys to move your cursor and the DELETE key to delete.
* * *
NOTE Most of the keyboard shortcuts used in WordPad, Word, and so on, were first used in the Edit program. If you know keyboard shortcuts for WordPad or Word, many will work in Edit.
The VER command shows the current version of Windows. The ECHO command tells the batch file to put text on the screen. Run the batch file, and it should look like Figure 15-24.
Figure 15-23 New version of FIRST.BAT
Figure 15-24 Running FIRST.BAT
Gee, that’s kind of ugly. Try editing the FIRST.BAT file one more time and add the following line as the first line of the batch file:
@echo off
Run FIRST.BAT again. It should look quite a bit nicer. The @echo off command tells the system not to show the command, just the result.
Sometimes you just want to look at a batch file. The TYPE command displays the contents of a text file on the screen, as shown in Figure 15-25.
* * *
CAUTION Don’t try using the TYPE command on anything other than a text file—the results will be unpredictable.
Figure 15-25 Using the TYPE command to see file contents
One of the more irritating aspects to batch files is that sometimes they don’t work unless you run them in the folder in which they are stored. This is because of the path setting. Every time you open a command prompt, Windows loads a number of settings by default. You can see all of these settings by running the SET command. Figure 15-26 shows the results of running the SET command.
Figure 15-26 Using the SET command to see settings