Errorlevel date bat

errorlevel date bat

To check errorlevels during batch file development, use either COMMAND /Z yourbatch.bat to display the errorlevel of every command executed in. Steps on how to use the choice and set command in a batch file to give CHOICE /N /C:123 /M "PICK A NUMBER (1, 2, or 3)"%1 IF ERRORLEVEL. files\%DATE:~4,2%-%DATE:~7,2%-%DATE:~10,4% Load Details" set errorlevel=0 echo %DEST% pause REM Create Directory md %DEST% pause REM.

watch the thematic video

BATCH scripting: Pratctical use of ERRORLEVEL in multiple scripts

How-to: Windows Environment Variables

Environment variables are mainly used within batch files, errorlevel date bat, they can be created, errorlevel date bat, modified and deleted for a session using the SET command. To make permanent changes, use SETX

Variables can be displayed using either SET or ECHO.

Variables have a percent sign on both sides: %ThisIsAVariable%
The variable name can include spaces, punctuation and mixed case: %_Another Ex.ample%
(This is unlike Parameter variables which only have one % sign and are always one character long: %A )

A variable name may include any of the following characters:
A-Z, a-z, 0-9, # $ ' ( ) * +-. ? @ [ ] _ ` { } ~
The first character of the name must not be numeric.

Standard (built-in) Variables

VariableDynamic (update every time used)
Volatile (Set at Logon)
Shell VariableUser Environment Variable
System Environment Variable
Default value assuming the system drive is C:
Predefined machine-wide system variable.
APPDATA YY  C:\Users\{username}\AppData\Roaming
CDY Y  The current directory (string).
ClientName YY  Terminal servers only - the ComputerName of a remote host.
CMDEXTVERSIONY Y  The current Command Processor Extensions version number. (NT = "1", errorlevel date bat, Win2000+ = "2".)
CMDCMDLINEY Y  The original command line that invoked the Command Processor.
CommonProgramFiles  Y  C:\Program Files\Common Files
COMMONPROGRAMFILES(x86)  Y  C:\Program Files (x86)\Common Files
COMPUTERNAME  Y  {computername}
COMSPEC    YC:\Windows\System32\cmd.exe or errorlevel date bat running a 32 bit WOW - C:\Windows\SysWOW64\cmd.exe
Comspec is used whenever the command shell spawns a 301 error thinkpad process, e.g. the FOR command, if comspec is not defined those processes will fail to launch.
DATEY Y  The current date using same region specific format as DATE.
ERRORLEVELY Y  The current ERRORLEVEL value, automatically set when a program exits.

  Y  Internet Explorer
These are undocumented variables for the Edge browser in Windows 10.
HighestNumaNodeNumberY (hidden) Y  The highest NUMA node number on this computer.
HOMEPATH YY  \Users\{username}
HOMESHARE Y   Network home folder.
LOCALAPPDATA YY  C:\Users\{username}\AppData\Local
LOGONSERVER YY  \\{domain_logon_server}
NUMBER_OF_PROCESSORS    YThe Number of processors running on the machine.
OneDrive   Y OneDrive synchronisation folder.
OS    YOperating system on the user's workstation.
PATH   YYC:\Windows\System32\;C:\Windows\;C:\Windows\System32\Wbem;{plus program paths}
  Y.COM; .EXE; .BAT; .CMD; .VBS; .VBE; .JS ; .WSF; .WSH; .MSC
Determine the default executable file extensions to search for and use, and in which order, errorlevel date bat, left to right.
The syntax is like the PATH variable - semicolon separators.
PROCESSOR_ARCHITECTURE    YAMD64/IA64/x86 This doesn’t tell you the architecture of the processor but only of the current process, so it returns "x86" for a 32 bit WOW process running on 64 bit Windows. See detecting OS 32/64 bit
PROCESSOR_ARCHITEW6432     =%PROCESSOR_ARCHITECTURE% (but only available to 64 bit processes)
PROCESSOR_IDENTIFIER    YProcessor ID of the user's workstation.
PROCESSOR_LEVEL    Y Processor level of the user's workstation.
PROCESSOR_REVISION    Y Processor version of the user's workstation.
ProgramData  Y  C:\ProgramData
ProgramFiles  Y  C:\Program Files or C:\Program Files (x86)
ProgramFiles(x86)1  Y  C:\Program Files (x86)   (but only available when running under a 64 bit OS)
ProgramW6432     =%ProgramFiles%(but only available when running under a 64 bit OS)
PROMPT  Y  Code for current command prompt format,usually $P$G
PSModulePath    Y%SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\
Public  Y  C:\Users\Public
RANDOMY    A random errorlevel date bat number, anything from 0 to 32,767 (inclusive).
%SessionName%     Terminal servers only - for a terminal server session, SessionName errorlevel date bat a combination of the connection name, followed by #SessionNumber. For a console session, SessionName returns "Console".
SYSTEMROOT  Y  The main Windows system folder. By default, C:\Windows
Windows can be installed to a different drive letter or (rarely) a different folder.
systemroot is a predefined machine-wide read-only system variable that will resolve to the correct location.
Defaults in early Windows versions are C:\WINNT, errorlevel date bat, C:\WINNT35 and C:\WTSRV
TEMP and TMP   YYC:\Users\{Username}\AppData\Local\Temp
Under XP this was \{username}\Local Settings\Temp
TIMEY    The current time using same format as TIME.
UserDnsDomain Y
 Y Set if a user is a logged on to a domain and returns the fully qualified DNS domain that the currently logged on user's account belongs to.
USERDOMAIN YY  {userdomain}
USERDOMAIN_roamingprofile Y   The user domain for RDS or standard roaming profile paths. Windows 8/10/2012.
USERNAME Y  YDefined as "SYSTEM", resolves as {username}
USERPROFILE YY  %SystemDrive%\Users\{username}
This is equivalent to the $HOME environment variable in Unix/Linux

Set by default as windir=%SystemRoot%
%WinDir% pre-dates Windows NT, errorlevel date bat, its use in many places has been replaced by the system variable: %SystemRoot%

1 Only on 64 bit systems, is used to store 32 bit programs.

Environment variables are stored in the registry:

User Environment Variables: HKCU\Environment
System Environment Variables: HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

By default, files stored under Local Settings do not roam with a roaming profile.

Dynamic environment variables are computed each time the variable is expanded, this makes them inherently read-only.
When all variables are listed with SET, these will not appear in the list.

Volatile variables are defined under the registry: HKCU\Volatile Environment
Do not attempt to directly SET a volatile variable, while you could change them, the system will overwrite them with newly derived values. The majority of volatile variables are CMD shell variables, errorlevel date bat, so like all CMD shell variables they are stored in memory only.


When a new process is started, the variables will be loaded in the following order:

      1. System Environment Variables
      2. Shell Variables (per user)
      3. User Environment Variables
      4. Shell Variables (other)

After the process has started, additional shell variables can be defined with SET, these will be available only to the current CMD shell session, but they will take precedence over any environment variables with the same name.

For example, if the SET command is used to modify the PATH, errorlevel date bat, or if it is removed completely with PATH ; that will affect the current process, but not any other programs or CMD sessions opened before or after the current one.

This precedence is important to understand because if you try to set a System Environment variable PATH = %APPDATA%;C:\Windows, it will fail because the %APPDATA% Shell variable is not created until after the System environment variables are imported to the session.
However, you can use %APPDATA% to build a User environment variable PATH.

Running the SET command with no options will display all Shell variables plus all User and System Environment variables, in other words every variable available to be read by that session. In PowerShell the same list is available via the env: drive

Undocumented Dynamic variables (read only)

%__APPDIR__%   The directory path to the current application .exe, terminated with a trailing backslash. (Global) - discuss
%__CD__%   The current directory, terminated with a trailing backslash. (Global)
%=C:%   The current directory of the C: drive. ( See Raymond Chen's explanation of this.)
%=D:%   The current directory of the Errorlevel date bat drive if drive D: has been accessed in the current CMD session.
%DPATH%   Related to the (deprecated) DPATH command.
%=ExitCode%   The most recent exit code returned by an external command, such as CMD /C EXIT n, errorlevel date bat, converted to hex.
%=ExitCodeAscii%   The most recent exit code returned by an external command, as ASCII. (Values 0-32 do not display because those map to ASCII control codes.)
%FIRMWARE_TYPE% The boot type of the system: Legacy, UEFI, errorlevel date bat, Not implemented, Unknown Windows 8/2012.
%KEYS%   Related to the (deprecated) KEYS command.

It is impossible to use SET to define or alter these variables because SET does not allow '=' in a variable name.
More detail on these undocumented variables can be found in this stackoverflow answer from Dave Benham.

Undocumented Dynamic variables (read/write)

%__COMPAT_LAYER%   Set the ExecutionLevel to either RunAsInvoker (asInvoker), RunAsHighest(highestAvailable) or RunAsAdmin(requireAdministrator) for more see elevation and Q286705 / Application Compatibility Toolkit for other Compatibility Layers (colours,themes etc).

Pass variables between batch scripts

There are several ways to pass values between batch files, or between a batch file and the command line, see the CALL and SETLOCAL pages for full details.

A child process by default inherits a copy of all environment variables from its parent, errorlevel date bat, this makes environment variables unsuitable for storing secret information such as API keys or user passwords, especially in rare occasions like crashes errorlevel date bat a errorlevel date bat log will often include the full OS environment at the time of the crash. PowerShell/Get-Credential is a more secure approach.

If Command Extensions are disabled, the following dynamic variables will be not accessible:

“Men may be convinced, errorlevel date bat, but they cannot be pleased against their will. But though taste is obstinate, errorlevel date bat, it is errorlevel date bat variable, and time often prevails when arguments have failed” ~ Samuel Johnson

Related commands:

How-to: Array Variables in CMD.
How-to: User Shell Folders - Profile, Start Menu - Location of user profile folders.
How-to: Detecting 32 vs 64 bit Windows.
CALL - Evaluate environment variables.
SET - View environment variables, set local variables.
SETX - Set environment variables.
How the environment-building process works - Raymond Chen [MSFT].
PowerShell equivalent: Working with Environment variables.
Q100843 - The four types of environment variable.
Q286705 - Set compatibility variables in a batch file.
Q242557 - Registry Settings for Folder Redirection.
StackOverflow - Storing a Newline in a variable.


Copyright © 1999-2022
Some rights reserved

FIND "<03>"') DO ECHO.%%a %%A :: Done ENDLOCAL GOTO:EOF :Syntax ECHO Display logged on users and their workstations. ECHO Usage: ACTUSR [ filter ] IF "%OS%"=="Windows_NT" ECHO Where: filter is the first part of the computer name^(s^) to be displayed

You can now see that the code has become more understandable to users who have not developed the code and hence is more maintainable.

In DOS, a string is an ordered collection of characters, such as "Hello, World!".

S.NoStrings & Description
1Create String

A string can be created in DOS in the errorlevel date bat way.

2Empty String

Empty String

3String Interpolation

String interpolation is a way to construct a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal.

4String Concatenation

You can use the set operator to concatenate two strings or a string and a character, or two characters. Following is a simple example which shows how to use string concatenation.

5String length

In DOS scripting, there is no length function defined for finding the length of a string. There are custom-defined functions which can be used for the same. Following is an example of a custom-defined function for seeing the length of a string.


A variable which has been set as string using the set variable can be converted to an integer using the /A switch which is using the set variable. The following example shows how this can be accomplished.

7Align Right

This used to align text to the right, which is normally used to improve hab error 0x4e of number columns.

8Left String

This is used to extract characters from the beginning of a string.

9Mid String

This is used to extract a substring via the position of the characters in the string.


The string substitution feature can also be used to remove a substring from another string.

11Remove Both Ends

This is used to remove the first and the last character of a string.

12Remove All Spaces

This is used to remove all spaces in a string via substitution.

13Replace a String

To replace a substring with another errorlevel date bat use the string substitution feature.

14Right String

This is used to extract characters from the end of a string.

Arrays are not specifically defined as a type in Batch Script but can be implemented. The following things need to be noted when arrays are implemented in Batch Script.

  • Each element of the array needs to be defined with the errorlevel date bat command.
  • The ‘for’ loop would be required to iterate through the values of the array.

Creating an Array

An array is created by using the following set command.

set a[0]=1

Where 0 is the index of the array and 1 is the value assigned to the first element of the array.

Another way to implement arrays is to define a list of values and iterate through the list of values. The following example show how this can be implemented.


@echo off set list = 1 2 3 4 (for %%a in (%list%) do ( echo %%a ))


The above command produces the following output.

1 2 3 4

Accessing Arrays

You can retrieve a value from the array by using subscript syntax, errorlevel date bat, passing the index of the value you want to retrieve within square brackets immediately after the name of the array.


@echo off set a[0]=1 echo %a[0]%

In this example, the index starts from 0 which means the first element can be accessed using index as 0, the second element can be accessed using index as 1 and so on. Let's check the following example to create, initialize and access arrays −

@echo off set a[0] = 1 set a[1] = 2 set a[2] = 3 echo The first element of the array is %a[0]% toshiba l755 bios verify error The second element of the array is %a[1]% echo The third element of the array is %a[2]%

The above command produces the following output.

The first element of the array is 1 The second element of the array is 2 The third element softice start failure error code 31 the array is 3

Modifying an Array

To add an element to the end of the array, you can use the set element along with the last index of the array element.


@echo off set a[0] = 1 set a[1] = 2 set a[2] = 3 Rem Adding an element at the end of an array Set a[3] = 4 echo The last element of the array is %a[3]%

The above command produces the following output.

The last element of the array is 4

You can modify an existing element of an Array by assigning a new value at a given index as shown in the following example −

@echo off set a[0] = 1 set a[1] = 2 set a[2] = 3 Rem Setting the new value for the second element of the array Set a[1] = 5 echo The new value of the second element of the array is %a[1]%

The above command produces the following output.

The new value of the second element of the array is 5

Iterating Over an Array

Iterating over an array is achieved by using the ‘for’ loop and going through each element of the array. The following example shows a simple way that an array can be implemented.

@echo off setlocal enabledelayedexpansion set topic[0] = comments set topic[1] = variables set topic[2] = Arrays set topic[3] = Decision making set topic[4] = Time and date set topic[5] = Operators for /l %%n in (0,1,5) do ( errorlevel date bat echo !topic[%%n]! )

Following things need to be noted about the above program −

  • Each element of the array needs to be specifically defined using the set command.

  • The ‘for’ loop with the /L parameter for moving through ranges is used to iterate through the array.


The above command produces the following output.

Comments variables Arrays Decision making Time and date Operators

Length of an Array

The length of an array is done by iterating over the list of values in the array since there is no direct function to determine the number of elements in an array.

@echo off set Arr[0] = 1 set Arr[1] = 2 set Arr[2] = 3 set Arr[3] = 4 set "x = 0" :SymLoop if defined Arr[%x%] ( call echo %%Arr[%x%]%% set /a "x+=1" GOTO :SymLoop ) echo "The length of the array is" %x%


Output The above command produces the following output.

The length of the array is 4

Creating Structures in Arrays

Structures can also be implemented in batch files using a little bit of an extra coding for implementation. The following example shows how this can be achieved.


@echo off set len = 3 set obj[0].Name = Joe set obj[0].ID = 1 set obj[1].Name = Mark set obj[1].ID errorlevel date bat 2 set obj[2].Name = Mohan set obj[2].ID = 3 set i = 0 :loop if %i% equ %len% goto :eof set cur.Name= set cur.ID= for /f "usebackq delims==.tokens=1-3" %%j in (`set obj[%i%]`) do ( set cur.%%k=%%l errorlevel date bat ) echo Name = %cur.Name% echo Value = %cur.ID% set /a i = %i%+1 goto loop

The following key things need to be noted about the above code.

  • Each variable defined using the set command has 2 values associated with each index of the array.

  • The variable i is set to 0 so that we can loop through the structure will the length of the array which is 3.

  • We always check for the condition on whether the value of i is equal to the value of len and if not, we loop through the code.

  • We are able to access each element of the structure using the obj[%i%] notation.


The above command produces the following output.

Name = Joe Value = 1 Name = Mark Value = 2 Name = Mohan Value = 3

Decision-making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

S.NoStrings & Description
1If Statement

The first decision-making statement is the ‘if’ statement.

2If/else Statement

The next decision making statement is the If/else statement. Following is the general form of this statement.

3Nested If Statements

Sometimes, there is a requirement to have multiple ‘if’ statement embedded inside each other. Following is the general form of this statement.

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.

In batch script, the following types of operators are possible.

  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Assignment operators
  • Bitwise operators

Arithmetic Operators

Batch script language supports the normal Arithmetic operators as any language. Following are the Arithmetic operators available.

Show Example

+Addition of two operands1 + 2 will give 3
Subtracts second operand from the first2 − 1 will give 1
*Multiplication of both operands2 * 2 will give 4
/Division of the numerator by the denominator3 / 2 will give 1.5
%Modulus operator and remainder of after an integer/float division3 % 2 will give 1

Relational Operators

Relational operators allow of the comparison of objects. Below are the relational operators available.

Show Example

EQUTests the equality between two objects2 EQU 2 will give true
NEQTests the difference between two objects3 NEQ 2 will give true
LSSChecks to see if the left object is less than the right operand2 LSS 3 will give true
LEQChecks to see if the left object is less than or equal to the right operand2 LEQ 3 will give true
GTRChecks to see if the left object is greater than the right operand3 GTR 2 will give true
GEQChecks to see if the left object is greater than or equal to the right operand3 GEQ 2 will give true

Logical Operators

Logical operators are used to evaluate Boolean expressions. Following are the logical operators available.

The batch language is equipped with a full set of Boolean logic operators like AND, OR, XOR, but only for binary numbers. Neither are there any values for Errorlevel date bat or FALSE, errorlevel date bat. The only logical operator available for conditions is the NOT operator.

Show Example

ANDThis is the logical “and” operator
ORThis is the logical “or” operator
NOTThis is the logical “not” operator

Assignment Operators

Batch Script language also provides assignment operators. Following are the assignment operators available.

Show Example

+=This adds right operand to the errorlevel date bat operand and assigns the result to left operand

Set /A a = 5

a += 3

Output will be 8

-=This subtracts the right operand from the left operand and assigns the result to the left operand

Set /A a = 5

a -= 3

Output will be 2

*=This multiplies the right operand with the left operand and assigns the result to the left operand

Set /A a = 5

a *= 3

Output will be 15

/=This divides the left operand with the right operand and assigns the result to the left operand

Set /A a = 6

a/ = 3

Output will be 2

%=This takes modulus using two operands and assigns the result to the left operand

Set /A a = 5

a% = 3

Output will be 2

Bitwise Operators

Bitwise operators are also possible in batch script. Following are the operators available.

Show Example

&This is the bitwise “and” operator
FIND "?" >NUL :: Command line parameter check IF NOT ERRORLEVEL 1 GOTO Syntax IF NOT [%2]==[] GOTO Syntax :: Keep variable local SETLOCAL :: Initialize variable SET WSS= :: Parse command line parameter IF NOT [%1]==[] FOR /F "tokens = 1 delims = \ " %%A IN ('ECHO.%~1') DO SET WSS = %%A :: Use NET VIEW and NBTSTAT to find computers and logged on users FOR /F "tokens = 1 delims = \ " %%a IN ('NET VIEW ^

Batch Script - Return Code

By default when a command line execution is completed it should either return zero when execution succeeds or non-zero when execution fails. When a batch script returns a non-zero value after the execution fails, the non-zero value will indicate what is the error number, errorlevel date bat. We will then use the error number to determine what the error is about and resolve it accordingly.

Following are the common exit code and their description.

Error CodeDescription
0Program successfully completed.
1Incorrect function. Indicates that Action has attempted to execute non-recognized command in Errorlevel date bat command prompt cmd.exe.
2The system cannot find the file specified. Indicates that the file cannot be found in specified location.
3The system cannot find the path specified. Indicates that the errorlevel date bat path cannot be found.
5Access is denied. Indicates that user has no access right to specified resource.



Program is not recognized as an internal or external command, operable program or batch file. Indicates that command, application name or path has been misspelled when configuring the Action.




Not enough virtual memory is available.

It indicates that Windows has run out of memory.




The application terminated errorlevel date bat a result of a CTRL+C, errorlevel date bat. Indicates that the application has been terminated either by the user's keyboard input CTRL+C or CTRL+Break or closing command prompt window.




The application failed to initialize properly. Indicates that the application has been launched on a Desktop to which the current user has no access rights. Another possible cause is that either gdi32.dll or user32.dll has failed to initialize.

Error Level

The environmental variable %ERRORLEVEL% contains the return code of the last executed program or script.

By default, errorlevel date bat, the way to check for the ERRORLEVEL is via the following code.


IF %ERRORLEVEL% NEQ 0 ( DO_Something )

It is common to use the command EXIT /B %ERRORLEVEL% at the end of the batch file to return the error codes from the batch file.

EXIT /B at the end of the batch file will stop execution of a batch file.

Use EXIT /B < exitcodes > at the end of the batch file to return custom return codes.

Environment variable %ERRORLEVEL% contains the latest errorlevel in the batch file, which is the latest error codes from the last command executed. In the batch file, it is always a good practice to use environment variables instead of constant values, since the same variable get expanded to different values on different computers.

Let’s look at a quick example on how to check for error codes from a batch file.


Let’s assume we have a batch file called Find.cmd which has the following code. In the code, we have clearly mentioned that we if don’t find the file called lists.txt then we should set the errorlevel to 7, errorlevel date bat. Similarly, if we see that the variable userprofile is not defined then we should set the errorlevel code to 9.

if not exist c:\lists.txt exit 7 if not defined userprofile exit 9 exit 0

Let’s assume we have another file called App.cmd that calls Find.cmd first. Now, if the Find.cmd returns an error wherein it sets the errorlevel to greater than 0 then it would exit the program. In the following errorlevel date bat file, after calling the Find.cnd find, errorlevel date bat, it actually checks to see if the errorlevel is greater than 0.

Call Find.cmd if errorlevel gtr 0 exit echo “Successful completion”


In the above program, we can have the following scenarios as the output −

  • If the file c:\lists.txt does not exist, then nothing will be displayed in the console output.

  • If the variable userprofile does not exist, then nothing will be displayed in the console output.

  • If both of the above errorlevel date bat passes then the string “Successful completion” will be displayed in the command prompt.


In the decision making chapter, we have seen statements which have been executed one after game maker error cannot find runner data other in a sequential manner. Additionally, implementations can also be done in Batch Script to alter the flow of control in a program’s logic. They are then classified into flow of control statements.

S.NoLoops & Description
1While Statement Implementation

There is no direct while statement available in Batch Script but we can do an implementation errorlevel date bat this loop very easily by using the if statement and labels.

2For Statement - List Implementations

The "FOR" construct offers looping capabilities for batch files. Following is the common construct of the ‘for’ statement for working with a list of values.

3Looping through Ranges

The ‘for’ statement also has the ability to move through a range of values. Following is the general form of the statement.

4Classic for Loop Implementation

Following is the classic ‘for’ statement which is available in most programming languages.

Looping through Command Line Arguments

The ‘for’ statement can also be used for checking command line arguments. The following example shows how the ‘for’ statement can be used to loop through the command line arguments.


@ECHO OFF :Loop IF "%1"=="" GOTO completed FOR %%F IN (%1) DO echo %%F SHIFT Errorlevel date bat Loop :completed


Let’s assume that our above code is stored in a file called Test.bat. The above command will produce the following output if the batch file passes the command line arguments of 1,2 and 3 as Test.bat 1 2 3.

1 2 3
S.NoLoops & Description
1Break Statement Implementation

The break statement is used to alter the flow of control inside loops within any programming language. The break statement is normally used in looping constructs and is used to cause immediate termination of the innermost enclosing loop.

Previous Page Print PageNext Page  

FIND /I /V "%%a" ^ errorlevel date bat


Leave a Comment

Proudly Powered By WordPress.

Theme Kaira by .