Batch file
This article needs additional citations for verification. Please help improve this article by adding reliable references. Unsourced material may be challenged and removed. (May 2009) |
Filename extension | .bat .cmd .btm |
---|---|
Type of format | Scripting |
Container for | Shell scripts |
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.COM
or cmd.exe
, or Wscript
.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.[1] Unix-like operating systems (such as Linux) have a similar type of file called a shell script.[2]
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.COM
to process commands and batch files. Commands were either internal (part of COMMAND.COM
), or if they were too large to keep in the main file; external, where COMMAND.COM
would 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.BAT
which automatically runs after DOS loads during booting. It typically had commands to load drivers.[2]
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 WIN
command was used and could be added to the end of the AUTOEXEC.BAT
file 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.COM
to 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 old COMMAND.COM
interpreter 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).[3]
Differences
The only known difference between .cmd and .bat file processing is that in a .cmd file the ERRORLEVEL
variable changes even on a successful command that is affected by Command Extensions (when Command Extensions are enabled), whereas in .bat files the ERRORLEVEL
variable changes only upon errors.[4]
Example
ex.1
@echo off
title Hello World
echo Hello World
echo.
pause
To execute the file it must be saved with a .bat or .cmd extension in plain text format (with a program like Notepad).
ex.2
@echo off
color A
title Conditional Shutdown
:start
cls
set /p name=enter a name:
cls
echo Hi, %name%
echo.
echo 1.Shutdown
echo 2.Quit
set /p choice=enter your choice 1,2:
if %choice%==1 goto shutdown
if %choice%==2 exit
:shutdown
cls
set /p sec=enter the amount of seconds that you wish the computer to shutdown in:
set /p msg=enter the shutdown message you wish to display:
shutdown -s -f -t %sec% -c %msg%
echo shutdown initiated at %time%
set /p cancel=type cancel to stop shutdown
if %cancel%==cancel shutdown -a
if %cancel%==cancel goto start
Result
When executed (either from Windows Explorer or Command Prompt) this is displayed:
ex.1
Hello World!
Press any key to continue . . .
ex.2
enter a name:
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 the ECHO
command is used again in the form ECHO.Hello World!
which outputs the line Hello World!
. The .
is important as it stops the ECHO
command 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 command ECHO.
is used which adds the empty line below Hello World!
, again using the .
so that the command doesn't output the input display's state (ECHO is on.
or ECHO is off.
) and just outputs an empty line. Then the PAUSE
command is used which pauses execution until the user presses a key. The Press any key to continue . . .
prompt is output by the command. Lastly, after the user presses a key the command ECHO ON
is 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).[5]
Other Windows scripting languages
In addition to traditional batch files, the need for more powerful capabilities has led to the development of other Windows-specific scripting languages:
- .kix: KiXtart was developed by a Microsoft employee in 1991, specifically to met the need for commands useful in a network logon script while retaining the simple 'feel' of the traditional batch file.
- .vbs and .js: Released in 1998, Windows Script Host (comprised of cscript.exe and wscript.exe) allows the running of scripts written in VBScript or JScript. They can be run in windowed mode (with the wscript.exe host) and console-based mode (with the cscript.exe host). They were included as a part of Windows since Windows 98.
- .ps1: In 2006, Microsoft released a further raw-text script processor, Windows PowerShell, which can be used with Windows XP (SP2/SP3) and above. This is designed for both interactive use from a command line interface, and also for writing scripts, and has a strong resemblance to Unix shells.[6]
In addition to these, powerful cross platform scripting tools such as Perl, Python, and Rexx are now available for Windows.
See also
Search Wikibooks | Wikibooks has a book on the topic of |
- VBScript
- Windows PowerShell, extensible command-line shell released in 2006
- List of DOS commands
References
- ↑ http://technet.microsoft.com/en-us/library/cc758944(WS.10).aspx
- ↑ 2.0 2.1 http://www.file-extensions.org/bat-file-extension
- ↑ http://www.cryer.co.uk/filetypes/b/btm.htm
- ↑ http://groups.google.com/group/microsoft.public.win2000.cmdprompt.admin/msg/ad9066638815812c
- ↑ http://www.winsupersite.com/showcase/win2008_cli.asp
- ↑ http://geekswithblogs.net/sdorman/archive/2006/06/18/82258.aspx
External links
- Microsoft Windows XP Batch file reference
- How Windows batch files work
- Windows batch file command overview
- FreeDOS' FreeCOM : complete feature list
- MS-DOS+Win../95/98/Me batch programming links
- Windows Command Line Interface script programming links
- scripting related information (also command line)
ca:Batch cs:Dávkový soubor de:Windows Batch es:Batch fr:.bat ko:배치 파일 it:File batch he:קובץ אצווה ms:Fail kelompok nl:Batchbestand ja:バッチファイル pl:Program wsadowy ru:Пакетный файл fi:.BAT sv:Batchfil zh:批处理
If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...
- Pages using deprecated source tags
- Pages with syntax highlighting errors
- Pages with broken file links
- Articles needing additional references from May 2009
- Articles with invalid date parameter in template
- All articles needing additional references
- Articles with example code
- Scripting languages
- DOS on IBM PC compatibles
- Windows administration