What is batch file? Definition from What.
Is. com. A batch file is a text file that contains a sequence of commands for a computer operating system. It's called a batch file because it batches (bundles or packages) into a single file a set of commands that would otherwise have to be presented to the system interactively from a keyboard one at a time.
A batch file is usually created for command sequences for which a user has a repeated need. Commonly needed batch files are often delivered as part of an operating system. You initiate the sequence of commands in the batch file by simply entering the name of the batch file on a command line.
By submitting your email address, you agree to receive emails regarding relevant topic offers from Tech. Target and its partners. You can withdraw your consent at any time. Contact Tech. Target at 2. Grove Street, Newton, MA.
You also agree that your personal information may be transferred and processed in the United States, and that you have read and agree to the Terms of Use and the Privacy Policy. In the Disk Operating System (DOS), a batch file has the file name extension ".
BAT". (The best known DOS batch file is the AUTOEXEC. BAT file that initializes DOS when you start the system.) In UNIX- based operating systems, a batch file is called a shell script. In IBM's mainframe VM operating systems, it's called an EXEC.
Note: A note on NT 4's SET /A switch from Walter Zackery in a message on alt.msdos.batch.nt: 'The SET /A command has a long list of problems. Syntax Definition Files. Last updated. These add-ons to TextPad are made available on an as-is basis.
Batch files - The SET command: Windows NT 4. Windows 7 Syntax. Windows NT 4. Windows 7 Syntax.
- Computer dictionary definition for what batch file and batch job means including related links, information, and terms.
- Syntax Definition Files. Last updated: 10 November 2014. These add-ons to TextPad are made available on an as-is basis.
- A batch file is a text file that contains a sequence of commands for a computer operating system. It's called a batch file because it batches (bundles or packages.
Displays, sets, or removes cmd. SET [variable=[string]]variable. Specifies the environment- variable name. Specifies a series of characters to assign to the variable. Type SET without parameters to display the current environment variables. If Command Extensions are enabled SET changes as follows: SET command invoked with just a variable name, no equal sign or value will display the value of all variables whose prefix matches the name given to the SET command.
For example: SET Pwould display all variables that begin with the letter 'P'SET command will set the ERRORLEVEL to 1 if the variable name is not found in the current environment. SET command will not allow an equal sign (=) to be part of the name of a variable (with the exception of some dynamic environment variables).
However, SET command will allow an equal sign in the value of an environment variable in any position other than the first character. Integer Math. The /A switch was introduced in Windows NT 4: SET /A expression. The /A switch specifies that the string to the right of the equal sign is a numerical expression that is evaluated. The expression evaluator is pretty simple and supports the following operations, in decreasing order of precedence: ()grouping* / %arithmetic operators+ - arithmetic operators< < > > logical shift& bitwise andˆbitwise exclusive or|bitwise or= *= /= %= += - =& = ˆ= |= < < = > > =assignment,expression separator. If you use any of the logical or modulus operators, you will need to enclose the expression string in quotes. Any non- numeric strings in the expression are treated as environment variable names whose values are converted to numbers before using them.
If an environment variable name is specified but is not defined in the current environment, then a value of zero is used. This allows you to do arithmetic with environment variable values without having to type all those % signs to get their values.
If SET /A is executed from the command line outside of a command script, then it displays the final value of the expression. The assignment operator requires an environment variable name to the left of the assignment operator.
Numeric values are decimal numbers, unless prefixed by 0x for hexadecimal numbers, 0b for binary numbers and 0 for octals numbers. So 0x. 12 is the same as 0b. Please note that the octal notation can be confusing: 0.
Note: A note on NT 4's SET /A switch from Walter Zackery in a message on alt. The SET /A command has a long list of problems.
I wouldn't use it for much more than simple arithmetic, although even then it truncates all answers to integers.". On the other hand, limited though it may seem, the SET command's math function can even be used for a complex task like calculating the date of Easter Day for any year.
But always keep in mind that it is limited to 1. Windows version. More on integer math, and its limitations, can be found on my Batch math and PHP based batch files pages.
Prompt For Input. The /P switch was introduced in Windows 2. SET /P variable=[prompt. String]The /P switch allows you to set the value of a variable to a line of input entered by the user.
Displays the specified prompt. String before reading the line of input. The prompt. String can be empty. Note: If nothing is entered, and only the Enter key is pressed, variable will remain unchanged instead of being cleared.
My own Second. Choice. Enter to get a preset default value. String Substitution. Environment variable substitution has been enhanced as follows: %PATH: str. PATH environment variable, substituting each occurrence of "str.
Note: I'm not sure about other Windows versions, but in Windows XP and Windows 7 string substitution is case- insensitive, e. SET Var=%Var: A=A% will convert all occurrences of the letter a in variable Var into upper case As (tip by Brad Thone & Brian Williams). Substrings. May also specify substrings for an expansion.%PATH: ~1. PATH environment variable, and then use only the 5 characters that begin at the 1. If the length is not specified, then it defaults to the remainder of the variable value. As of Windows 2. 00. PATH: ~- 1. 0%would extract the last 1.
PATH variable.%PATH: ~0,- 2%would extract all but the last 2 characters of the PATH variable. Delayed Variable Expansion.
Finally, support for delayed environment variable expansion has been introduced in Windows 2. This support is always disabled by default, but may be enabled/disabled via the /V command line switch to CMD. EXE. See CMD /? Delayed Variable Expansion. Delayed environment variable expansion is useful for getting around the limitations of the current expansion which happens when a line of text is read, not when it is executed. The following example demonstrates the problem with immediate variable expansion: SET VAR=before. IF "%VAR%" == "before" (.
SET VAR=after. IF "%VAR%" == "after" @ECHO If you see this, it worked. VAR% in BOTH IF statements is substituted when the first IF statement is read, since it logically includes the body of the IF, which is a compound statement. So the IF inside the compound statement is really comparing "before" with "after" which will never be equal. Similarly, the following example will not work as expected: SET LIST=. FOR %A IN (*) DO SET LIST=%LIST% %A.
ECHO %LIST%in that it will NOT build up a list of files in the current directory, but instead will just set the LIST variable to the last file found. Again, this is because the %LIST% is expanded just once when the FOR statement is read, and at that time the LIST variable is empty.
So the actual FOR loop we are executing is: FOR %A IN (*) DO SET LIST= %Awhich just keeps setting LIST to the last file found. Delayed environment variable expansion allows you to use a different character (the exclamation mark) to expand environment variables at execution time. If delayed variable expansion is enabled, the above examples could be written as follows to work as intended: SET VAR=before. IF "%VAR%" == "before" (.
IF "! VAR!" == "after" @ECHO If you see this, it worked. FOR %A IN (*) DO SET LIST=! LIST! %A. ECHO %LIST%Dynamic Environment Variables. As of Windows 2. 00.
Command Extensions are enabled, then there are several dynamic environment variables that can be expanded but which don't show up in the list of variables displayed by SET. These variable values are computed dynamically each time the value of the variable is expanded.
If the user explicitly defines a variable with one of these names, then that definition will override the dynamic one described below: %=C: %expands to the current directory string on the C: drive%=D: %expands to the current directory string on the D: drive if drive D: has been accessed in the current CMD session%=Exit. Code%expands to the hexadecimal value of the last return code set by EXIT /B%=Exit. Code. Ascii%expands to the ASCII value of the last return code set by EXIT /Bif greater than 3. APPDIR__%expands to the requesting executable's (CMD. EXE for batch files) parent folder, always terminated with a trailing backslash%__CD__%expands to the current directory string, always terminated with a trailing backslash%CD%expands to the current directory string (no trailing backslash, unless the current directory is a root directory)%CMDCMDLINE%expands to the original command line that invoked the Command Processor%CMDEXTVERSION%expands to the current Command Processor Extensions version number%DATE%expands to current date using same format as DATE command%ERRORLEVEL%expands to the current ERRORLEVEL value%HIGHESTNUMANODENUMBER%expands to the highest NUMA node number on this machine%RANDOM%expands to a random decimal number between 0 and 3.
TIME%expands to current time using same format as TIME command. Notes: 1. The first 6 dynamic variables listed here (the ones on a grey background) are not listed in SET's on- screen help text. 2. The first 4 dynamic variables listed (the ones starting with the equal sign) were discovered by "Smart. Genius". The current values are revealed using the command SET ""I do not know if these variables are available in Windows 2. Smart. Genius" supplied the following demo script: @ECHO OFFSETLOCAL ENABLEDELAYEDEXPANSIONFOR %%A IN (8. DO (CMD /C EXIT /B %%ASET /P "Var=!=Exit.
Code. Ascii!" < NUL)ECHO. ENDLOCALTry it, it is harmless.. 3. The following sample code, by Thomas Freundt, lists all %=D: % like variables: FOR /F "tokens=* delims==" %%A IN ('SET "" ˆ| FINDSTR. EXE /B "="') DO @ECHO.%%AOr a simplified version for the command line, if you don't mind a more cluttered output: SET "" | FINDSTR. EXE /B "=" 4. If %=D: % is not available (not verified for Windows 2. NT 4) it can be emulated by the following code: PUSHD D: SET CDD=%CD%POPD 5.
The dynamic variable %__APPDIR__% was discovered by Carlos Montiers. I do not know if this variables is available in Windows 2. The dynamic variable %__CD__% was discovered by Menno Vogels.
I do not know if this variables is available in Windows 2. Error. Levels. Andrew Pixley reported that in Windows XP, "unsetting" a variable (SET variable=) will set the Error. Level to 1, whereas setting it again (SET variable=value) will not reset the Error. Level to 0. In Windows 7, SET only sets the Error. Level to 1 with a SET variable command (no equal sign) if variable is not set.