Batch file - Wikipedia, the free encyclopedia. A batch file is a kind of script file in DOS, OS/2 and Windows.
- A batch file is a kind of script file in DOS, OS/2 and Windows. It consists of a series of commands to be executed by the command line interpreter, stored in a plain.
- An A-Z Index of the Windows CMD command line ADDUSERS Add or list users to/from a CSV file ADmodcmd Active Directory Bulk Modify ARP Address Resolution Protocol.
- Hi, I have a batch file (.bat) that contains 29 lines of commands and REMarks. When I double click on the file name, a command window opens but nothing is executed.
- I would like to make a batch file that: Opens cmd.exe Within that Command Prompt runs net use to display mapped share paths Leaves the window open so that I can run.
Say, if I have foo.exe bar.exe baz.exe How do I run all of them from a batch file asynchronously, i.e. without waiting for the previous program to stop? Hi all, I would like to simultaneously launch multiple instances of a single CMD command. How do I write a batch file to do so? Regards, Nicholas. A Windows Batch File Programming Primer v1.0.2 / 01 jun 14 / greg goebel / public domain * The Microsoft Windows operating system provides a useful tool for.
It consists of a series of commands to be executed by the command line interpreter, stored in a plain text file. A batch file may contain any command the interpreter accepts interactively and use constructs that enable conditional branching and looping within the batch file, such as "if", "for", "goto" and labels. Similar to Job Control Language (JCL) and other systems on mainframe and minicomputer systems, batch files were added to ease the work required for certain regular tasks by allowing the user to set up a script to automate them. 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.Unix- likeoperating systems (such as Linux) have a similar, but more flexible, type of file called a shell script. The filename extension.
DOS and Windows. Windows NT and OS/2 also added . Batch files for other environments may have different extensions, e. DOS, 4. OS2 and 4.
NT related shells. The detailed handling of batch files has changed. Some of the detail in this article applies to all batch files while other details apply only to certain versions.
VariantsIn MS- DOS, a batch file can be started from the command line by typing its name followed by any required parameters and pressing the "enter" key. When MS- DOS loads, the file AUTOEXEC. BAT is automatically executed, so any commands that need to be run to set up the MS- DOS environment for use could be placed in this file. Computer users would have the autoexec file set up the system date and time, initialize the MS- DOS environment, load any resident programs or device drivers, or initialize network connections and assignments.
In MS- DOS, the extension ". COMMAND. COM line by line as if it was a list of commands to be entered, with some extra batch- file- specific commands for basic programming functionality, including a GOTO command for changing flow of line execution.
Early WindowsMicrosoft Windows was introduced in 1. GUI- based alternative to text- based operating systems and was designed to run on MS- DOS. In order to start it, the win command was used, which 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 . Windows in the MS- DOS Prompt.
Windows 3. 1x and earlier, as well as Windows 9x invoked COMMAND. COM to run batch files. The IBMOS/2 operating system supported DOS- style batch files. It also included a version of REXX, which was a more advanced batch- file 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 made no difference with regard to the way batch files worked from COMMAND.
COM. OS/2's batch file interpreter also supports an EXTPROC command. This passes the batch file to the program named on the EXTPROC file as a data file. The named program can be a script file; this is similar to the #! Windows NTUnlike Windows 9x and earlier, the Windows NT family of operating systems does not depend on MS- DOS. Windows NT introduced an enhanced 3.
CMD or . BAT extension. Cmd. exe added additional commands, and implemented existing ones in a slightly different way, so that the same batch file (with different extension) might work differently with cmd.
COMMAND. COM. In most cases, operation is identical if the few unsupported commands are not used. Cmd. exe's extensions to COMMAND. COM can be disabled for compatibility.
Microsoft released a version of cmd. Windows 9x and ME called WIN9.
CMD to allow users of older versions of Windows to use certain cmd. As of Windows 8[update], cmd. COMMAND. COM can be run from within a cmd. Windows able to run 1. Filename extensions. The first filename extension used by Microsoft for batch files. This extension runs with MS- DOS and all versions of Windows, under COMMAND.
COM or cmd. exe, despite the different ways the two command interpreters execute batch files. Used for batch files in Windows NT family and sent to cmd. COMMAND. COM does not recognize this filename extension, cmd. Windows environment by mistake.
In addition, "set", "path", "assoc" and "prompt" commands, when executed from a . It is also used by IBM's OS/2 for batch files.
The extension used by 4. DOS and 4. NT. The scripts that run on 4. DOS and 4. NT are faster, especially with longer ones, as the script is loaded entirely ready for execution, rather than line- by- line.Batch file parametersCOMMAND. COM and cmd. exe support a number of special variables (%0, %1 through %9) in order to refer to the path and name of the batch job and the first nine calling parameters from within the batch job, see also SHIFT. Non- existent parameters are replaced by a zero- length string. They can be used similar to environment variables, but are not stored in the environment.
Microsoft and IBM refer to these variables as replacement parameters or replaceable parameters, whereas Digital Research, Novell and Caldera established the term replacement variables for them. JP Software calls them batch file parameters.ExamplesThis example batch file displays "Hello World!", prompts and waits for the user to press a key, and then terminates. Note: It does not matter if commands are lowercase or uppercase unless working with variables)@ECHOoff. ECHO Hello World! PAUSETo execute the file, it must be saved with the extension .
Windows- NT type operating systems) in plain text format, typically created by using a text editor such as Notepad or a word processor in text mode. When executed, the following is displayed. Press any key to continue . ExplanationThe interpreter executes each line in turn, starting with the first. The @ symbol at the start of the line prevents the prompt from displaying that command. The command ECHO off turns off the prompt permanently, or until it is turned on again. Then the next line is executed and the ECHO Hello World!
Hello World!, as only off and on have special functions. Then the next line is executed and the PAUSE command displays Press any key to continue . After a key is pressed, the script terminates, as there are no more commands. In Windows, if the script is executed from an already running Command Prompt window, the window remains open at the prompt as in MS- DOS; otherwise, Command Prompt window closes on termination. PAUSE command at end of the script causes the window to remain open for as long as the user deems fit. Limitations and exceptionsNull values in variablesVariable expansions are substituted textually into the command, and thus variables which contain nothing simply disappear from the syntax, and variables which contain spaces turn into multiple tokens.
This can lead to syntax errors or bugs. For example, if %foo% is empty, this statement: parses as the erroneous construct: Similarly, if %foo% contains "abc def", then a different syntax error results: IF abc def==bar ECHO Equal. The usual way to prevent this problem is to surround variable expansions in quotes so that an empty variable expands into the valid expression IF ""=="bar" instead of the invalid IF ==bar. The text that is being compared to the variable must also be enclosed in quotes, because the quotes are not special delimiting syntax; these characters represent themselves. IF"%foo%"=="bar"ECHO Equal. The delayed !VARIABLE! Windows 2. 00. 0 and later may be used to avoid these syntactical errors.
In this case, null or multi- word variables do not fail syntactically because the value is expanded after the IF command is parsed: Another difference in Windows 2. As described in previous examples, previous batch interpreter behaviour would have resulted in an empty string. Example. C: \> set My. Var=. C: \> echo%My.
Var%%My. Var%. C: \> if"%My. Var%"=="" (echo My.
Var is Not defined) else (echo My. Var is %My. Var%). My. Var is %My. Var%Batch interpreters prior to Windows 2. My. Var is Not defined"Quotation marks and spaces in passed stringsFor some commands, spaces are treated as delimiters in commands, unless those spaces are enclosed by quotation marks. A single quotation mark (") is not included as part of the string. However, an escaped quotation mark (""") can be part of the string.
For other commands, spaces are not treated as delimiters and do not need quotation marks. If quotes are included they become part of the string. This can cause conflicts where a string contains quotation marks, and is to be inserted into another line of text that must also be enclosed in quotation marks: > set foo="this string is enclosed in quotation marks"> echo"test 1 %foo%""test 1 "this string is enclosed in quotation marks""> eventcreate /T Warning /ID 1 /L System /SO "Source" /D "Example: %foo%"ERROR: Invalid Argument/Option - 'string'. Type "EVENTCREATE /?" for usage. On Windows 2. 00. T Warning /ID 1 /L System /SO "Source" /D "Example: %foo%"SUCCESS: A 'Warning' type event is created in the 'Source' log/source.
Escaped characters in stringsSome characters, such as pipe (" ") characters, have special meaning to the command line. They cannot be printed as text using the ECHO command unless escaped using the caret ^ symbol. C: \> Echo foo bar'bar' is not recognized as an internal or external command. C: \> Echo foo ^ bar. However, escaping does not work as expected when inserting the escaped character into an environment variable. The variable ends up containing a live pipe command when merely echoed. It is necessary to escape both the caret itself and the escaped character for the character display as text in the variable.
C: \> set foo=bar baz'baz' is not recognized as an internal or external command. C: \> set foo=bar ^ baz. C: \> echo%foo%'baz' is not recognized as an internal or external command. C: \> set foo=bar ^^^ baz.
C: \> echo%foo%. The delayed !VARIABLE! CMD /V: ON or with SETLOCAL ENABLEDELAYEDEXPANSION in Windows 2. C: \> cmd/V: ON. Microsoft Windows [Version 6. Copyright (c) 2. 00. Microsoft Corporation.
All rights reserved. C: \> set foo=bar ^ baz. C: \> echo !foo!
Sleep or scripted delayUntil the TIMEOUT command was introduced with Windows Vista, there was no easy way to implement a timed pause, as the PAUSE command halts script activity indefinitely until any key is pressed. Many workarounds were possible, but generally only worked in some environments: the CHOICE command was not available in older DOS versions, PING was only available if TCP/IP was installed, and so on. Simple small programs external were readily available to meet the need; a typical example is the 9.
WAIT. COM executable. WAIT 5 would wait for 5 seconds, then return control to the script. Most such programs are 1. COM files, so are incompatible with 6. Windows. Text output with stripped CR/LFNormally all printed text automatically has the control characters for "carriage return" and "line feed" appended to the end of each line. C: \> type batchtest. C: \> batchtest.
It does not matter if the two echo commands share the same command line; the CR/LF codes are inserted to break the output onto separate lines. C: \> @echo foo& @echo bar. A trick discovered with Windows 2. CR/LF trailing the text.
In this example, the CR/LF does not follow Line 1, but does follow Line 2 and Line 3. C: \> type batchtest. Line 1set/p bar="%foo%"< nulecho Line 2echo Line 3. C: \> batchtest. This can be used to output data to a text file without CR/LF appended to the end: set/p foo="Line 1"< nul> data. Line 2"< nul > > data.
Line 3"< nul > > data. Line 1. Line 2. Line 3. However, there is no way to inject this stripped CR/LF prompt output directly into an environment variable. Setting a UNC working directory from a shortcutIt is not possible to have a command prompt that uses a UNC path as the current working directory; e.
The command prompt requires the use of drive letters to assign a working directory, which makes running complex batch files stored on a server UNC share more difficult. While a batch file can be run from a UNC file path, the working directory default is "C: \windows\system. In Windows 2. 00. PUSHD and POPD command with command extensions.If not enabled by default, command extensions can be temporarily enabled using the "/E: ON" switch for the command interpreter. So to run a batch file on a UNC share, assign a temporary drive letter to the UNC share, and use the UNC share as the working directory of the batch file, a Windows shortcut can be constructed that looks like this: Target: %COMSPEC%/E: ON/C"PUSHD """\\SERVER\SHARE\DIR1\DIR2\""" & BATCHFILE. BAT & POPD"The working directory attribute of this shortcut is ignored.
This also solves a problem related to User Account Control (UAC) on Windows Vista and newer. When an administrator is logged on and UAC is enabled, and they try to run a batch file as administrator from a network drive letter, using the right- click file context menu, the operation will unexpectedly fail. This is because the elevated UAC privileged account context does not have network drive letter assignments, and it is not possible to assign drive letters for the elevated context via the Explorer shell or logon scripts.
However, by creating a shortcut to the batch file using the above PUSHD / POPD construct, and using the shortcut to run the batch file as administrator, the temporary drive letter will be created and removed in the elevated account context, and the batch file will function correctly. The following syntax does correctly expand to the path of the current batch script.
UNC default paths are turned off by default as they used to crash older programs.[1. The Dword registry value Disable. UNCCheck at HKEY_CURRENT_USER\Software\Microsoft\Command Processor[1. UNC. CD command will refuse to change but placing a UNC path in Default Directory in a shortcut to Cmd or by using the Start command. C$ "cmd /k" (C$ share is for administrators).
Character setBatch files use a DOS character set, as defined by the computer, e. Code page 4. 37. The non- ASCII parts of these are incompatible with the Unicode or Windows character sets otherwise used in Windows so care needs to be taken.[1. Non- English file names work only if entered through a DOS character set compatible editor. File names with characters outside this set won't work in batch files. To get output in Unicode into file pipes from an internal command such as dir, one can use the cmd /U command. For example, cmd /U /C dir > files.
Windows characters, in the UTF- 1. LE encoding. Batch viruses and malwareBatch files can be used to write malware and viruses. Some well- known examples in the hacking community are the fork bomb (also called window bomber) malware which opens the same program so many times the computer is exhausted from resources, the neverending copying of the batch file all over the disk, or the DNS poisoning malware in which the batch file modifies the hosts file to make a Windows computer connect to an IP address specified by the batch file's programmer when the user attempts to visit the website of a well- known internet site unrelated to the malware. Batch viruses can also spread themselves via USB flash drives by using Windows' Autorun capability.[1. An example of a batch virus commonly used to delete data is shown below. It deletes all the data in the current directory(folder).
Other Windows scripting languagesThe cmd. Windows up to at least the 2. Windows 8 preview[update]. COMMAND. EXE, which interprets . BAT files, was supported in all 1. Windows 8 preview.[1.
There are other, later and more powerful, scripting languages available for Windows. However, these require the scripting language interpreter to be installed before they can be used: Ki. Xtart (. kix) - developed by a Microsoft employee in 1. Windows Script Host (. Microsoft in 1. 99.
VBScript or JScript. It can run them in windowed mode (with the wscript. They have been a part of Windows since Windows 9. Windows Power. Shell (.
Microsoft and can operate with Windows XP (SP2/SP3) and later versions. Power. Shell can operate both interactively (from a command- line interface) and also via saved scripts, and has a strong resemblance to Unix shells.[1.
Unix- style shell scripting languages can be used if a Unix compatibility tool, such as Cygwin, is installed. Cross- platform scripting tools including Perl, Python, Ruby, Rexx, and PHP are available for Windows. Script files run if the filename without extension is entered.
There are rules of precedence governing interpretation of, say, Do. This if Do. This. Do. This. bat, Do. This. exe, etc. exist; by default Do. This. cmd has highest priority.
This default order may be modified in newer operating systems by the user- settable PATHEXTenvironment variable. See alsoReferences^"Using batch files: Scripting; Management Services". Technet. microsoft. Retrieved 2. 01. 2- 1. To verify that COMMAND.
COM remains available (in the \WINDOWS\SYSTEM3. COMMAND. COM" at the 3. Windows 7 command prompt.^"Difference between bat and cmd WWo. IT - Wayne's World of IT". Retrieved 2. 01. 2- 1.
Cryer. co. uk. Retrieved 2. DR- DOS 7. 0. 2 User Guide, Caldera, Inc., 1. Conn, Rex; Rawson, Tom; Paul, Matthias; Dye, Charles; Georgiev, Luchezar (2. DOS 8. 0. 0 online help. ^"How to do a delay", ericphelps. Utilities for DOS, linking to WAIT.
ZIP (archive of WAIT. COM) and other programs^If Command Extensions are enabled the PUSHD command accepts network paths in addition to the normal drive letter and path. If a network path is specified, PUSHD creates a temporary drive letter that points to that specified network resource and then change the current drive and directory, using the newly defined drive letter.
Temporary drive letters are allocated from Z: on down, using the first unused drive letter found.— The help for PUSHD in Windows 7^ abhttps: //support. Chen, Raymond. "Keep your eye on the code page". Microsoft. ^http: //www. Availability of cmd. COMMAND. COM can be confirmed by invoking them in any version of WIndows (COMMAND. COM not in 6. 4- bit versions; probably only available in Win 8/3. Free download Copy Outlook Data File programs.
Windows Power. Shell - Unix comes to Windows". Geekswithblogs. net. Retrieved 2. 01. 2- 1. External links.