In DOS, OS/2, and Microsoft Windows, a batch file is a text file containing a series of commands intended to be executed by the command interpreter. When a batch file is run, the shell program (usually
COMMAND.COMorcmd.exe, orWscript.exe or cscript.exe) reads the file and executes its commands, normally line-by-line. Batch files are useful for running a sequence of executables automatically and are often used by system administrators to automate tedious processes. Unix-like operating systems (such as Linux) have a similar type of file called a shell script.DOS batch files have the filename extension .bat . Batch files for other environments may have different extensions, e.g. .cmd or .bat in the Microsoft Windows NT-family of operating systems and OS/2, or .btm in 4DOS and 4NT related shells. The now-obsolete Windows 9x family of operating systems only recognize the .bat and .btm extension.
History
Originally command interpreters' primary responsibility was to execute out commands entered manually. Such commands might involve starting programs, carrying out file operations, executing functions concerned with controlling the system, setting preferences or administrative tasks. Sequences of such commands were also sometimes stored in files, which could be later passed to the command interpreter to be read and executed, so such stored sequences could be termed sets of 'batch commands'.
Over time, command interpreters or 'shells' grew additional features, as such stored sequences of such 'batch commands' became more complex, and command interpreters evolved into something more akin to interpreters for a kind of limited programming languages or 'script'. Additional commands, advanced syntactic features and computation abilities were added which allowed sophisticated programs to be written so that batch files or scripts could contain a mixture of commands of the traditional.
Early influences
Microsoft DOS batch language was influenced by various Unix shells, as well as other text-based command line interfaces from the early 1980s such as CP/M which in turn took much of their inspiration from TOPS-10 and TOPS-20 from Digital Equipment Corporation. Although a DOS batch file is analogous to a shell script in Unix-like operating systems, the syntax and range of commands available is less sophisticated.
DOS
The first example of Microsoft's batch files were the .bat files used by DOS. The operating system used
COMMAND.COMto process commands and batch files. Commands were either internal (part ofCOMMAND.COM), or if they were too large to keep in the main file; external, whereCOMMAND.COMwould look for the command each time it is requested at the prompt (or display an error message if it didn't exist). This meant that if one wanted, they could add commands to DOS, and in turn allow more functionality to batch files when using the new commands.An example of an important batch file was
AUTOEXEC.BATwhich automatically runs after DOS loads during booting. It typically had commands to load drivers.Enhancements and alternatives
The limitations of the DOS command intrepreter led to various non-Microsoft interpreters to provide enhanced syntax by providing "enhancement" commands such as those in the Norton Utilities (like the BE or Batch Enhancer), in 1989 the replacement shell 4DOS.
Early Windows
Microsoft Windows was introduced in 1985 as a GUI Operating System alternative to text-based operating and was designed to run on MS-DOS. In order to start it the
WINcommand was used and could be added to the end of theAUTOEXEC.BATfile to allow automatic loading of Windows. In the earlier versions one could run a .bat type file from Windows in the MS-DOS Prompt.Windows was based on MS-DOS and used
COMMAND.COMto run .bat files on the following operating systems:
- Windows 1, 2 and 3.
- Windows 95 and 98.
- Windows ME (access to real mode MS-DOS was restricted).
OS/2
The IBM OS/2 operating system supported DOS-style batch files. It also included a version of REXX, which was a more advanced scripting language. IBM and Microsoft started developing this system but during the construction of it broke up after a dispute, as a result of this IBM referred to their MS-DOS like console shell without mention of Microsoft; naming it just DOS, although this seemingly had no impact on the way batch files worked.
Windows NT
The Microsoft Windows NT-family of operating systems featured a second, new command interpreter
cmd.exe. CMD was far more sophisticated than its DOS counterpart, the oldCOMMAND.COMinterpreter which was also retained but launched inside an MS-DOS virtual machine emulator facility. CMD batch files may have extensions either .cmd or .bat.Filename extensions
- .bat : The first extension used by Microsoft for batch files. This extension can be run in most Microsoft Operating Systems, including MS-DOS and most versions of Microsoft Windows.
- .cmd : Designates a Windows NT Command Script , which is written for the Cmd.exe shell, and is not backward-compatible with COMMAND.COM.
- .btm : The .btm file extension is used by 4DOS and 4NT rather than Command Prompt or
COMMAND.COM. This non-Microsoft Filename extension when used with larger scripts is faster than .cmd or .bat scripts because it is all loaded at once into one command, rather than line-by-line (as it is done with .bat and .cmd extensions).Differences
The only known difference between .cmd and .bat file processing is that in a .cmd file the
ERRORLEVELvariable changes even on a successful command that is affected by Command Extensions (when Command Extensions are enabled), whereas in .bat files theERRORLEVELvariable changes only upon errors.Example
@ ECHO OFF ECHO Hello World! ECHO . PAUSETo execute the file it must be saved with a .bat or .cmd extension in plain text format (with a program like Notepad).
Result
When executed (either from Windows Explorer or Command Prompt) this is displayed:
Hello World! Press any key to continue . . .Explanation
Batch files are executed by every line being executed in order until the end is reached or something else stops it (such as the key shortcut for terminating batch processing; 'Ctrl' + 'C'). This batch file first turns off the 'echo' with
ECHO OFF. This stops the display of input from the batch file and limits the display to output from commands only. Since this command is executed before the input is silenced, the @ symbol is used at the start of the command which stops that command line showing input. Then theECHOcommand is used again in the formECHO.Hello World!which outputs the lineHello World!. The.is important as it stops theECHOcommand confusing an attempt to output a line with 'ON' or 'OFF' at the start with an attempt to change the state of showing input, and it is easier to tell which lines are outputs with the.there as a visual aid. Then the commandECHO.is used which adds the empty line belowHello World!, again using the.so that the command doesn't output the input display's state (ECHO is on.orECHO is off.) and just outputs an empty line. Then thePAUSEcommand is used which pauses execution until the user presses a key. ThePress any key to continue . . .prompt is output by the command. Lastly, after the user presses a key the commandECHO ONis used which turns the prompt and input on again, so that if the file is executed from the Command Prompt, rather than Windows Explorer then when the execution ends the user can see the prompt again to use normally. After the last line is reached the batch file ends execution automatically. If it was started from the command prompt (by entering the name of the file when in its directory) then the window remains when finished, but when started from Windows Explorer the window automatically closes upon the end of execution.Future
Microsoft hasn't officially released information pertaining to the future of Command Prompt (host for .bat and .cmd files) yet, but the company is now starting to include Windows PowerShell in releases for newer Operating Systems, which has all the core functions of Command Prompt and more (and instead of .bat and .cmd files, it runs .ps1 files). Yet it is important to remember that it is not certain this will replace Command Prompt, and that Microsoft is still making important tools for Command Prompt specifically, instead of for PowerShell (such as
servermanagercmd.exe, which incorporates the entire set of Server Manager functions for Windows Server 2008).Other Windows scripting languages
In addition to traditional batch files, the need for more powerful capabilities has led to the
batch.c File Reference
00064 { 00065 int i; 00066 int bytes_to_write; 00067 00068 /* Write flist info to batch file */ 00069 00070 bytes_to_write = 00071 sizeof ...
How to write a batch file | eHow.com
How to write a batch file. Batch files have been part of Microsoft Operating Systems since the early MS-DOS days. The text file can be used on the command line to execute an ...
How to write DOS batch files
How to write DOS batch files (MS-DOS Featuring DOS 5.0). From Compute! Issue 137 / January 1992
forum.laptop.org • View topic - How do I write 'batch' files (dos ...
I have installed winzig, and treeline. Both are written in python. I had to install PyQt to get treeline to work. I want to write simple executable scripts that will launch these ...
Read and Write in Batch file
Hi I am rookie ot batch. I have a requirememnt where, I have to read from a text file which looks like this a,test.txt s,Files.txt a,ForCommand.txt a is for application file a...
How to write a DOS batch file to loop through files « Jim 2.0’s ...
I used to write DOS batch files like this all the time, but after a while you start to forget the DOS syntax and the tricks. So this post is almost for as much for me as it is for ...
Help to write a batch file to convert recorded TV to DVR-MS ...
As Windows 7 build 7048 and above now include a built in convertor to convert .wtv to .dvr-ms I want to run a batch file that scans my recorded tv folder and automatically
Batch File Basics
As a general rule you cannot write batch files and so forth with a conventional word processing package. There is one exception, that is PC-Write.
Citations: Optimal Write Batch Size in Log-structured File Systems ...
Carson, S., Setia, S., Optimal Write Batch Size in Log-structured File Systems , Proceedings of 1992 Usenix Workshop on File Systems, Ann Arbor, MI, May 21-22 1992, 7991.
New To Java Technology Archive - Re: How to write batch files??
Hi, suppost that you have a jar, that represents an application, how can I run the Main class (wit the main method) from a batch file, in other words how to write that batch (.bat ...