What’s a Batch File?

Print anything with Printful



A batch file is a text file containing commands for a command interpreter to execute. It can automate long projects and run multiple programs. The .bat extension is most commonly used, but .cmd may also be accepted. Autoexec.bat runs every time a computer starts up. Batch files can be created in any text editor and customized with commands. Variables like %1 can be used to print user-entered variables.

A batch file is a file used by a Windows®, DOS, or OS/2 operating system. 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 execute a complex array of commands, often running multiple programs, to achieve a single cohesive goal. System administrators often use them to automate long projects, and some programs that haven’t been fully packaged into one program can use a batch file to run the 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. Most 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, i.e. when errors will be displayed. In 4DOS and other similar shells, they may also use the .btm extension, although this is not recognized by other operating systems.

On older Windows® computers and DOS computers, there is a main file known as autoexec.bat that runs every time the computer starts up. This includes many common files that need to be started and commands that need to be run to get the system up and running. Elements can be added to the autoexec.bat file to set specific startup behaviors, such as launching programs, cleaning up file spaces, or echoing reminders.

Since a batch file is basically just a text file, one can be created in any kind of simple text editor, and the suffix can later be changed to .bat or .cmd. Most people find it easier to edit one from the command line, either directly in DOS or in a shell window launched from Windows®. The edit command can be used to create a new file, by typing something like edit foo.bat. Within the file, you can use a number of basic commands.

A simple batch file might include something like this: pause; echo Now let’s open the documents folder.;dir c:documents. To run the file, a user would simply type the file name, without the .bat suffix, and it will run. This particular file would start with a pause, which would prompt the user to press a key. Once a key was pressed, it printed the line “Now let’s open the document folder.” Then it will list the contents of My Documents directory found within C drive.

There are actually a few commands that can be used within a batch file to further customize them. For example, the variable %1 can be used to print a variable that the user enters after typing the name of the file to run. So, for example, if someone wanted to remove a directory and create a new one with one command, they could create a file called rmmk.bat that includes the following lines: rmdir %1; mkdir %2. He could then run it by typing something like “rmmk foo bar”, which would remove the directory named foo and create a new directory named bar.




Protect your devices with Threat Protection by NordVPN


Skip to content