Batch file - Wikipedia, the free encyclopedia. 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 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. The term "batch" is from batch processing, meaning "non- interactive execution", though a batch file may not process a batch of multiple data. 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.
First let's take a look at a batch file I created that will just open Excel, Calculator and Firefox: NOTE: Examples and file paths are from Windows Vista. XP paths will probably be different for some programs as well as for MS Office. Sometimes we need to create a list of files contained in a folder or drive, but there is no menu in Windows (XP/Vista/7/8) to do this. If you search about how to do this on the internet, you will find many article and tutorial about it, but. This article explains how to create a batch file in Windows operating system which is used to execute a batch of Dos and as well as windows command. This is cool, but lame, i wanted more detailed info, like things you can do.
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. 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)@ECHO OFF. 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 any line prevents the prompt from displaying that command as it is executed. The command ECHO OFF turns off the prompt permanently, or until it is turned on again. The combined @ECHO OFF is often as here the first line of a batch file, preventing any commands from displaying, itself included. 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: IFabc 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: C: \> setfoo="this string is enclosed in quotation marks"C: \> echo"test 1 %foo%""test 1 "this string is enclosed in quotation marks""C: \> 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. C: \> setfoo="this string is enclosed in quotes"C: \> setfoo=%foo: "="""%C: \> echo"test 1 %foo%""test 1 """this string is enclosed in quotes""""C: \> eventcreate /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. C: \> Echo foo ^| bar.
How to create a batch file in Windows 7. How to create a batch file in Windows 7. This article explains how to create a batch file in Windows operating system which is used to execute a batch of Dos and as well as windows command. How to create a batch file in Windows 7. Windows provides so many options in order to improve the performance and usability of the system. Most of the utilities which is provided by Windows operating system can be run from command prompt.
All of the utilities can be run as a command. Batch files can be used to make a set of command to execute in Windows operating system. Support if you want to do series of activities in Windows then you can add all the commands in a batch files and it will executes all the command one by one. It is one of the easy ways to automate a process in Windows operating system.
You can add the system command in the batch processes and you can run the processes in specific time using the scheduler. You can automate an administrator task in batch file easily.
Batch files are simple text file which will have some basic dos command and you can also run the system command that you can run in Windows command prompt. In order to create a sample batch file open notepad and type the following command and save the file as Test. ECHO OFFDir. PAUSEIpconfig. Pause. Cleanmgr. Check the below screenshot. ECHO OFF is DOS( Disk Operating System) command and while running the commands in the command prompt it will not show the command that is executing.
Dir will just list the files and folders in the directory. Pause will wait for the user input, press enter key of other key to continue.
Ipconfig is a Windows command which will display the network configuration of the system. Cleanmgr is a Windows utility command which will open the disk clean manager Window. Once the batch file is created and you can double click the batch file using the mouse and it will start executing the series of Dos commands and other command that you typed in the Batch File. Check the list of DOS commands from the below link. Click here for the List of DOS Command.