The Full Wiki

Batch file: Wikis

  

Note: Many of our articles have direct quotes from sources you can cite, within the Wikipedia article! This article doesn't yet, but we're working on it! See more info or our list of citable articles.

Encyclopedia

From Wikipedia, the free encyclopedia

Batch file
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) 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 Windows 9x family of operating systems only recognize the .bat extension.

Contents

History

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:

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 is 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, but by default, will be run by cmd.exe.

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). in other words in notepad click save as then write the name of the file like: file.bat under that switch from type "text document (*.txt)" to "all files"

ex.2

@echo off
color 3
title Conditional Shutdown
set /p name=enter a name:
:start 
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

ex3:

@echo off
color a
msg * alt+enter=fullscreen
echo This Is The Magnadox
pause
:a
color a3
color b3
color c2
color e5
color d0
color a2
color f4
color a3
color c8
color b9
color d3
color b4
goto :a

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:

ex.3

message pops up saying "alt+enter=fullscreen" and command prompt flashes random colors

BASIC COMMANDS:

ECHO "Hello" Will show "Hello" in the command prompt (cmd.exe)
Start MSPaint.exe = 'Start' opens an application or file and 'MSPaint.EXE' is the application.
PAUSE = Stops the prompt from engaging/starting 
the next command line, in command prompt, it will appear as 
'Press any key to continue...'
PAUSE >NUL = Same as 'PAUSE' but 
doesn't display 'Press any key to continue...'
IF -
IF EXIST "C:/HI" DEL "C:/HI" = IF THE DIRECTORY "C:/HI" does exist, delete it.
IF NOT EXIST "C:/HI" MKDIR "C:/HI_DID_NOT_EXIST" = 
IF THE DIRECTORY "C:/HI" doesn't exist, make a directory called "C:/HI_DID_NOT_EXIST"
CALL "XCOPY.BAT" = 
'CALL' is like start but start cannot open CMD or BAT files, 
what call does is replace the current Command Prompt file with "XCOPY.BAT"
DIR /S /F "C:/Program Files" > "C:/Program Files List.txt" = 
This is the most complicated of the
 commands so far on this list, what it does is combine commands with switches. 
'DIR' is command that gets all the directory information for the selected directory, 
'/S' is a switch which tells the directory command to list all subfolders, 
'/F' is a switch which tells the directory command to list all files in the 
"C:/Program Files" directory. '>' sends the data to the file, not copying, 
this is a write once command, once the batch file which uses this command is run again, 
the previous data is rid of. And beyond the '>' symbol is the file you wish to place 
the directory data info.

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!. Then the command ECHO. is used which adds the empty line below Hello World!, 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. The . can also be used to prevent the ECHO command from confusing an attempt to output a line beginning with 'ON' or 'OFF' from changing the state of showing input. 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

References

External links








Got something to say? Make a comment.
Your name
Your email address
Message