Internet
Fact-checked

At EasyTechJunkie, we're committed to delivering accurate, trustworthy information. Our expert-authored content is rigorously fact-checked and sourced from credible authorities. Discover how we uphold the highest standards in providing you with reliable knowledge.

Learn more...

What is a Batch File?

Brendan McGuigan
Brendan McGuigan

A batch file is a file used by either a Windows® operating system, DOS, or OS/2. It is a text file that contains a string of commands to be interpreted in order by a command interpreter. This means that a single file can run a complex arrangement of commands, often running multiple programs, to achieve a single cohesive end. System administrators often use them to automate long projects, and some programs that haven’t been fully collected into one program may use a batch file to run necessary components and then launch the final program.

The .bat extension is the most commonly-used extension for a batch file, and will be recognized by virtually all shells. More modern shells may accept the .cmd extension as well, and usually the distinction between the two has to do with what the error levels are, which is to say when errors will be displayed. In 4DOS and other similar shells, they may also use the .btm extension, though this isn’t recognized by other operating systems.

Man holding computer
Man holding computer

On older Windows® computers and DOS computers, there is a core file known as autoexec.bat that is run every time the computer starts up. This includes many common files that need to be launched and commands that need to be run to prepare the system to operate. Things can be added to the autoexec.bat file to set specific behaviors on start-up, such as launching programs, cleaning up file spaces, or echoing reminders.

Since a batch file is fundamentally just a text file, one can be created in any sort of simple text editor, and the suffix can later be changed to .bat or .cmd. Most people find it easiest to simply edit one from the command line, either straight in DOS or in a shell window launched from within Windows®. The edit command can be used to create a new file, by typing something like edit foo.bat. Within the file, a slew of basic commands can be used.

A simple batch file might include something the following: pause; echo Now we open the documents folder.;dir c:\documents\. To execute the file, a user would simply type the name of the file, without the .bat suffix, and it would run. This particular file would begin with a pause, which would prompt the user to hit a key. Once a key had been hit, it would then print the line, “Now we open the documents folder.” Then it would list the contents of the Documents directory found within the C drive.

There are actually quite a few commands that can be used within a batch file to further customize them. For example, the %1 variable can be used to print a variable that the user inputs after typing the name of the file to execute. So, for example, if someone wanted to remove a directory and make a new directory in one command, he could make a file named rmmk.bat that included the following lines: rmdir %1; mkdir %2. He could then execute it by typing something like "rmmk foo bar," which would remove the directory named foo, and make a new directory named bar.

Discussion Comments

anon130322

A good second generation batch framework is Visual Batch which can be found at SourceForge.

This allows for GUI or Windows Form Style batch files called .vbat files.

Post your comments
Login:
Forgot password?
Register:
    • Man holding computer
      Man holding computer