BAT file

from Wikipedia, the free encyclopedia
Batch processing file
Windows Batch
Batch file
BatchIcon2.jpg
Icon from Windows Vista
File extension : .BAT  .cmd
Initial release: 1981 with PC DOS 1.0
Type: Batch processing
Extended by: Unix shell
Expanded to: PowerShell

A BAT file (of English. Batch ) is a batch file ( english batch file , and batch file , batch script , batch script , etc.) which in compatible PC-DOS operating systems , OS / 2 and Windows can run.

Technical details and history

  • The file format consists of text files that can be executed as batch processing by a command line interpreter . The command line interpreter for most PC-compatible DOS operating systems such as MS-DOS , PC DOS or DR-DOS is provided by Windows NT is .COMMAND.COMcmd.exe
  • The command line interpreter is used as the runtime environment and interpreter. Therefore, all command line commands can usually also be executed from a batch processing file. Furthermore, also comments useful Anchor links and queries.
  • Under Windows NT most of the commands COMMAND.COMare cmd.exesimulated by. It is therefore possible that a batch file written for MS-DOS will be executable under Windows NT.
  • At Microsoft, a text file is made for batch processing by giving it the filename extension .bat or, under Windows NT and OS / 2, the filename extension .cmd. Such files are automatically processed by the command line interpreter when they are called by name.
  • In 2006 Microsoft released PowerShell , a more modern and cross-platform alternative, with which scripts can be written in the specially developed PowerShell Scripting Language .

use

Batch processing files are used to execute recurring command sequences (e.g. for automated logon processes or loading TSR programs ). It is also possible to make the execution of individual command lines dependent on a condition . Since IBM DOS or MS-DOS 2.0 there have been constructs such as IFor IF NOT. Under Windows NT-based systems, IF- and - FORcommands were greatly enhanced, for example by offering comparisons of values. In addition to these condition operators, variables can also be used, jumps executed, user inputs implemented and comments written.

In PC-compatible DOS file used AUTOEXEC.BATfor system configuration at the start of the operating system. Windows 3.x processes the file when it starts WINSTART.BAT.

Meaning of some batch commands

  • @ECHO OFF: Suppresses the output of the command in the batch file
  • CLS(from English clear screen ): Clears the screen
  • PAUSE: Pauses a batch processing until the user has pressed a key.
  • PAUSE > NUL: Pauses a batch processing until the user has pressed a key, the message "Press any key ..." is redirected to the virtual device file NUL , which means that it is not displayed on the screen.
  • EXIT: Ends the batch file

Hello World example

The output of the classic Hello World example can be generated by a batch processing file. This should be saved beforehand under a suitable file name e.g. B. be hello.batsaved.

REM Text "Hello World!" ausgeben
ECHO Hello World!

An extended version with adapted title line, font and background color and a request to close the program by pressing a key could look like this:

However, the paragraphs are not absolutely necessary, they only serve for better clarity.

REM Befehle mit "@" werden nicht ausgegeben. Befehle mit REM werden nicht verarbeitet (Kommentar). Befehle mit :: oder @REM werden weder ausgegeben noch verarbeitet.
@ECHO OFF

REM Kommandozeilenfenster leeren
CLS

REM Der Titel des Fensters wird zu "Hallo-Welt-Programm" geändert (nur Windows)
TITLE Hallo-Welt-Programm


REM Die Windows-Codepage wird zu "1252" (westeuropäisch) geändert, dadurch kann unter anderem "ü" ausgegeben werden.
CHCP 1252

REM Die Schrift- und Hintergrundfarbe wird geändert
COLOR 4E

REM Text "Hallo Welt!" wird ausgegeben
ECHO Hallo Welt!

REM Leerzeile ausgeben
ECHO.

REM Es wird der Text "Taste drücken zum Beenden" ausgegeben und gewartet bis der Benutzer eine Taste drückt
ECHO Taste drücken zum Beenden
PAUSE > NUL

See also

Web links

Wikibooks: Windows Batch Programming Information  - Learning and Teaching Materials