IF: Performs conditional processing in batch programs.


... "IF" Excerpt from Microsoft Windows Help
... The examples for the command "IF"
... Important information, tips for the "IF" command

The command: "IF" is on Windows 11, 10, .. available

"IF" Excerpt from Microsoft Windows Help

Microsoft Windows [Version 10.0.19045.3693]
(c) Copyright 1985-2023 Microsoft Corp.

C:\\WINDOWS>

Performs conditional processing in batch programs.

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command

  NOT               Specifies that Windows XP should carry out 
                    the command only if the condition is false.

  ERRORLEVEL number Specifies a true condition if the last program run
                    returned an exit code equal to or greater than the number
                    specified.

  string1==string2  Specifies a true condition if the specified text strings
                    match.

  EXIST filename    Specifies a true condition if the specified filename
                    exists.

  command           Specifies the command to carry out if the condition is
                    met.  Command can be followed by ELSE command which
                    will execute the command after the ELSE keyword if the
                    specified condition is FALSE

The ELSE clause must occur on the same line as the command after the IF.  For
example:

    IF EXIST filename. (
        del filename.
    ) ELSE (
        echo filename. missing.
    )

The following would NOT work because the del command needs to be terminated
by a newline:

    IF EXIST filename. del filename. ELSE echo filename. missing

Nor would the following work, since the ELSE command must be on the same line
as the end of the IF command:

    IF EXIST filename. del filename.
    ELSE echo filename. missing

The following would work if you want it all on one line:

    IF EXIST filename. (del filename.) ELSE echo filename. missing

If Command Extensions are enabled IF changes as follows:

    IF [/I] string1 compare-op string2 command
    IF CMDEXTVERSION number command
    IF DEFINED variable command

where compare-op may be one of:

    EQU - equal
    NEQ - not equal
    LSS - less than
    LEQ - less than or equal
    GTR - greater than
    GEQ - greater than or equal

and the /I switch, if specified, says to do case insensitive string
compares.  The /I switch can also be used on the string1==string2 form
of IF.  These comparisons are generic, in that if both string1 and
string2 are both comprised of all numeric digits, then the strings are
converted to numbers and a numeric comparison is performed.

The CMDEXTVERSION conditional works just like ERRORLEVEL, except it is
comparing against an internal version number associated with the Command
Extensions.  The first version is 1.  It will be incremented by one when
significant enhancements are added to the Command Extensions.
CMDEXTVERSION conditional is never true when Command Extensions are
disabled.

The DEFINED conditional works just like EXISTS except it takes an
environment variable name and returns true if the environment variable
is defined.

%ERRORLEVEL% will expand into a string representation of
the current value of ERRORLEVEL, provided that there is not already
an environment variable with the name ERRORLEVEL, in which case you
will get its value instead.  After running a program, the following
illustrates ERRORLEVEL use:

    goto answer%ERRORLEVEL%
    :answer0
    echo Program had return code 0
    :answer1
    echo Program had return code 1

You can also using the numerical comparisons above:

    IF %ERRORLEVEL% LEQ 1 goto okay

%CMDCMDLINE% will expand into the original command line passed to
CMD.EXE prior to any processing by CMD.EXE, provided that there is not
already an environment variable with the name CMDCMDLINE, in which case
you will get its value instead.

%CMDEXTVERSION% will expand into a string representation of the
current value of CMDEXTVERSION, provided that there is not already
an environment variable with the name CMDEXTVERSION, in which case you
will get its value instead.

The examples for the command "IF"

The `IF` command in Windows Command Prompt is used to execute conditional statements in batch scripts. Here are some examples: Example 1: Simple equality check:

@ECHO OFF
SET Variable1=10
SET Variable2=10

IF %Variable1% EQU %Variable2% (
    ECHO The variables are the same.
) ELSE (
    ECHO The variables are not equal.
)

Description: In this example, the variables `Variable1` and `Variable2` are compared. If they are equal, the message "The variables are equal." output, otherwise "The variables are not equal." Example 2: Checking for file existence:

@ECHO OFF
IF EXIST C:\Example.txt (
    ECHO The file exists.
) ELSE (
    ECHO The file does not exist.
)

Description: This checks whether the file “Example.txt” exists in the C:\ directory. If the file exists, the corresponding message is output, otherwise the other one. Example 3: Checking for user input:

@ECHO OFF
SET /P UserInput=Please enter a value:

IF "%UserInput%"=="" (
    ECHO You have not entered a value.
) ELSE (
    ECHO You entered "%UserInput%".
)

Description: This checks whether the user has entered a value. If no input has been made, the message "You have not entered a value." output, otherwise the entered character string is displayed. Example 4: Checking file size:

@ECHO OFF
SET file=C:\Example.txt
SET MinimumSize=1024

FOR %%A IN (%File%) DO SET FileSize=%%~zA

IF %FileSize% GEQ %MinimalSize% (
    ECHO The file meets the minimum size.
) ELSE (
    ECHO The file is too small.
)

Description: The size of the “Example.txt” file is checked here. If the file size is greater than or equal to the minimum size (`MinimalSize`), the message "The file meets the minimum size." output, otherwise "The file is too small." Example 5: Checking the operating system:

@ECHO OFF
VER | FIND "Windows 10" > NUL

IF %ERRORLEVEL% EQU 0 (
    ECHO You are using Windows 10.
) ELSE (
    ECHO You are using a different operating system.
)

Description: This checks whether the operating system is Windows 10. If so, the message "You are using Windows 10." otherwise "You are using a different operating system." The `IF` statement makes it possible to check various conditions in batch scripts and take appropriate actions based on the results.

Important information, tips for the "IF" command

There are a few important points to note when using the `IF` statement in the Windows Command Prompt: 1. Syntax of Condition: The syntax of `IF` statement requires correct formulation of the condition. The comparison operator (`EQU`, `NEQ`, `LSS`, `LEQ`, `GTR`, `GEQ`) must be between the values ??to be compared. Note the spaces in the syntax to avoid unexpected errors. 2. Use of quotation marks: When validating strings, it is important to use quotation marks to ensure that the condition is evaluated correctly. Example: `IF "%Variable%"=="Value" ...` 3. Variables: If you use variables in the condition, make sure they are correctly wrapped with `%`, as in `%Variable%`. This ensures that the value of the variable is correctly inserted into the condition. 4. ELSE Statement: The `ELSE` statement must be on the same line as the closing parenthesis of `IF` or start on a new line. Example:

    IF condition (
        Echo condition is true.
    ) ELSE (
        Echo condition is false.
    )
    
5. Note `%ERRORLEVEL%` after a command: If you use the `IF` command after another command, you can use `%ERRORLEVEL%` to check the success or failure of that command. Example:

    DIR C:\NonExistingDirectory
    IF %ERRORLEVEL% NEQ 0 (
        Echo Error executing DIR command.
    )
    
6. Numerical comparisons: When making numerical comparisons, you should use `EQU`, `NEQ`, `LSS`, `LEQ`, `GTR`, `GEQ` to ensure that the comparisons are performed correctly. 7. Logical Operators: You can use logical operators like `AND` (`&&`) and `OR` (`||`) to create complex conditions. Example:

    IF Exist File.txt IF %Variable% EQU Value (
        Echo Both conditions are true.
    )
    
8. Combination with other commands: You can combine the `IF` statement with other commands to perform different actions based on conditions. 9. Whitespace: Be careful not to use unnecessary whitespace in your conditions or commands, as this can lead to unexpected behavior. It is important to clearly understand the syntax of the `IF` statement and ensure that the conditions are formulated correctly to achieve the desired behavior. If you are unsure, you can consult official Microsoft documentation or online resources.


Deutsch
English
Español
Français
Italiano
日本語 (Nihongo)
한국어 (Hangugeo)
汉语 (Hànyǔ)
Türkçe
Português
Português
Svenska
Norsk
Dansk
Suomi
Nederlands
Polski









Windows-10


... Windows 10 FAQ
... Windows 10 How To


Windows 10 How To


... Windows 11 How To
... Windows 10 FAQ



The command IF - Performs conditional processing in batch programs.

HTTP: ... console/en/038.htm
0.062
14692

The user account picture directory / directory under Windows 10/11!

Überprüfen welche Grafikkarte an Ihrem Computer ist!

Where is the night mode in Windows 10, 11 please?

Deactivate the antivirus application Defender quickly!

Does the SIEMENS NIXDORF keyboard work on Windows 10/11?

The lost Internet Explorer on Windows 10!



(0)