A First Look at Batch Files

Before there was Windows, there were consoles. Of course, even with Windows, there is still a console mode. One thing that many newer developers might not realize is that there is a whole world of commands that can be used within the console world. Back in the old days, these commands often were used to create batch files that could manipulate the operating system and its programs.

Note: The phrase console mode is often referred to as command line mode or DOS mode.

Many people still use batch files today. In fact, many still write programs using the command line utility programs. This article is a brief introduction to just a couple of the commands available. You will, however, learn where to get more information.

The Command Line

Getting to the command line can be accomplished in a number of ways. The most consistent way is to go the Windows Start menu and select Run… From the Run dialog, you can enter cmd and click OK. Alternatively, you can enter Command. Either way, a console window will be opened.

Figure 1: The command window

If you look at Figure 1, you’ll see console windows have been opened using both cmd and command. One of the benefit of cmd over command is that it recognizes the longer file names and directory structures in the more recent versions of Windows. Command is an older, DOS console that is restricted to the standard 8.3 file name scheme. As such, it is recommended that you use cmd.

Creating a Command Line Program

Command line programs created using operating system commands are often called batch files. These files have extensions of BAT. A simple batch program to display information to the screen and wait for the user to hit a key is shown in Listing 1.

Listing 1: Hello Batch World

ECHO off
ECHO Hello Batch World!
PAUSE

If you enter this program into your text editor and save it as Hello.bat, you will be able to see what it does. If you double-click on it within Windows Explorer, it will open a command window, display the text, and then pause. Once you hit a key, the command window will close. Alternatively, you can run the program from an opened command window. First, open a command window. Next, navigate to where you saved the program. You then can simply type the name of the program. You don’t need the extension. Running Hello will display what is in Figure 2.

Figure 2: Running Hello.bat

As you can see, it is extremely simple. The ECHO command is used to echo (display) information to the console window. You might wonder what the Echo Off command is in the first line of the program. If you remove that line and re-run the program, you’ll see a slightly different result, as shown in Figure 3.

Figure 3: Running Hello.bat with Echo on

As you can see in Figure 3, with Echo left on, each command in the file is actually displayed prior to it being executed. In other words, each command is ‘echoed.’ For this reason, you’ll most likely want to turn Echo Off.

Although I’ve suggested using your own text editor, you also can use a command line text editor. This editor is called EDIT. At the command line, if you type EDIT and press enter, the EDIT editor will be opened. If you want to edit a file, you can type its name after Edit, so to edit the Hello.Bat file, you’d enter:

Edit Hello.bat

The result of this would be the Hello.bat file being opened in the EDIT editor as shown in Figure 4. If Hello.bat is not in the current directory, it is assumed you are creating a new file.

Figure 4: The EDIT Editor with Hello.bat

The Edit editor is not overly complex; however, it does provide a number of simple options. As you can see from Figure 4, unlike Notepad in Windows, Edit is completely character based. You can use the menus and Help to learn more about the editor.

The Command Commands

Any of the commands within the command window can be put into a batch file. The list of commands available are:

  • ASSOC: Displays or modifies file extension associations.
  • AT: Schedules commands and programs to run on a computer.
  • ATTRIB: Displays or changes file attributes.
  • BREAK: Sets or clears extended CTRL+C checking.
  • CACLS: Displays or modifies access control lists (ACLs) of files.
  • CALL: Calls one batch program from another.
  • CD: Displays the name of or changes the current directory.
  • CHCP: Displays or sets the active code page number.
  • CHDIR: Displays the name of or changes the current directory.
  • CHKDSK: Checks a disk and displays a status report.
  • CHKNTFS: Displays or modifies the checking of disk at boot time.
  • CLS: Clears the screen.
  • CMD: Starts a new instance of the Windows command interpreter.
  • COLOR: Sets the default console foreground and background colors.
  • COMP: Compares the contents of two files or sets of files.
  • COMPACT: Displays or alters the compression of files on NTFS partitions.
  • CONVERT: Converts FAT volumes to NTFS. You cannot convert the current drive.
  • COPY: Copies one or more files to another location.
  • DATE: Displays or sets the date.
  • DEL: Deletes one or more files.
  • DIR: Displays a list of files and subdirectories in a directory.
  • DISKCOMP: Compares the contents of two floppy disks.
  • DISKCOPY: Copies the contents of one floppy disk to another.
  • DOSKEY: Edits command lines, recalls Windows commands, and creates macros.
  • ECHO: Displays messages, or turns command echoing on or off.
  • ENDLOCAL: Ends localization of environment changes in a batch file.
  • ERASE: Deletes one or more files.
  • EXIT: Quits the CMD.EXE program (command interpreter).
  • FC: Compares two files or sets of files, and displays the differences between them.
  • FIND: Searches for a text string in a file or files.
  • FINDSTR: Searches for strings in files.
  • FOR: Runs a specified command for each file in a set of files.
  • FORMAT: Formats a disk for use with Windows.
  • FTYPE: Displays or modifies file types used in file extension associations.
  • GOTO: Directs the Windows command interpreter to a labeled line in a batch program.
  • GRAFTABL: Enables Windows to display an extended character set in graphics mode.
  • HELP: Provides Help information for Windows commands.
  • IF: Performs conditional processing in batch programs.
  • LABEL: Creates, changes, or deletes the volume label of a disk.
  • MD: Creates a directory.
  • MKDIR: Creates a directory.
  • MODE: Configures a system device.
  • MORE: Displays output one screen at a time.
  • MOVE: Moves one or more files from one directory to another directory.
  • PATH: Displays or sets a search path for executable files.
  • PAUSE: Suspends processing of a batch file and displays a message.
  • POPD: Restores the previous value of the current directory saved by PUSHD.
  • PRINT: Prints a text file.
  • PROMPT: Changes the Windows command prompt.
  • PUSHD: Saves the current directory then changes it.
  • RD: Removes a directory.
  • RECOVER: Recovers readable information from a bad or defective disk.
  • REM: Records comments (remarks) in batch files or CONFIG.SYS.
  • REN: Renames a file or files.
  • RENAME: Renames a file or files.
  • REPLACE: Replaces files.
  • RMDIR: Removes a directory.
  • SET: Displays, sets, or removes Windows environment variables.
  • SETLOCAL: Begins localization of environment changes in a batch file.
  • SHIFT: Shifts the position of replaceable parameters in batch files.
  • SORT: Sorts input.
  • START: Starts a separate window to run a specified program or command.
  • SUBST: Associates a path with a drive letter.
  • TIME: Displays or sets the system time.
  • TITLE: Sets the window title for a CMD.EXE session.
  • TREE: Graphically displays the directory structure of a drive or path.
  • TYPE: Displays the contents of a text file.
  • VER: Displays the Windows version.
  • VERIFY: Tells Windows whether to verify that your files are written correctly to a disk.
  • VOL: Displays a disk volume label and serial number.
  • XCOPY: Copies files and directory trees.

This list and set of definitions can be found by simply typing HELP and pressing Enter while in the command window. To get more details on any of the commands, you can type HELP followed by the command.

I recommend caution if you are going to use these commands. If you do something such as deleting a file, the file is gone. The Windows Recycle Bin isn’t here to help you if you make a mistake.

Having said that, you can do a lot with the above commands and batch files. If you want more on what you can do and how to do it, leave a comment on this article. If enough people ask, I’ll write a series of articles on doing different ‘batch’ things!

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read