Dos - If

Card Puncher Data Processing

About

To get more help on the if statement, type on the command line prompt:

help if

Syntax

Statement

If Else

IF condition (
   command
   ) ELSE (
   command
   )

where:

When using the else construct:

  • The command needs to be terminated by a newline or the block command must be defined with brackets.
  • The else statement must be in the same flow than the if
IF condition (command) ELSE command

Short If

IF condition command

Condition

text and number comparison

Syntax
[/I] [NOT] string1==string2

and

[/I] string1 compare-op string2

where

  • the /I switch specify a case insensitive comparison.
  • 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
Example
  • Text Comparison
@echo off
set hello=hello
if "%hello%"=="hello" echo %hello% world
hello world

  • Number
if 2 GTR 1 (echo Yes) else echo No
Yes

exist

Syntax
[NOT] EXIST filename

where:

  • the parts enclosed between bracket are optional (ie NOT)
  • filename is the name of the file
Example
@echo off
if exist hello.bat (
    echo hello.bat exist
    )
hello.bat exist

Errorlevel

Without comparison

Syntax:

ERRORLEVEL number 

This condition will return a true condition if the last program run returned an exit code (Errorlevel) equal to or greater than the number specified. When a command executes without error it terminates with an exit status of zero.

With comparison

With the string comparison

[NOT] %ERRORLEVEL% [compare-op]  number 

where:

  • %ERRORLEVEL% is the dynamic variable of ERRORLEVEL
  • compare-op can be one of the following
    • EQU - equal
    • NEQ - not equal
    • LSS - less than
    • LEQ - less than or equal
    • GTR - greater than
    • GEQ - greater than or equal

See Error level example

Defined

Syntax:

[NOT] DEFINED variableName

Example:

if not defined v (echo v is not defined) else echo %v%
v is not defined

set v=Call Me Nico
if not defined v (echo v is not defined) else echo %v%
Call Me Nico

Support

was unexpected at this time.

This error occurs really often when you test a condition over a variable that is not set.

The command interpreter first replace the variable with its value before starting the command and the result is an error in the syntax as the variable has no value.





Discover More
Card Puncher Data Processing
DOS - (Rem|Remarks|Comments)

Comments can be added to batch script by using: the rem command or the alias :: (if ) To get more information on Rem, type in your console: If you use the comment alias :: in a block (if...
Card Puncher Data Processing
DOS - Delayed environment variable

From the set help. The current expansion happens when a text block is read, not when it is executed. Here below, you have the example of an immediate variable expansion with an if block The following...
Card Puncher Data Processing
DOS - File

File in DOS. See ... in a for loop: the file name without extension. Fully Qualified Path Name See if exist statement See: ERASE Deletes one or more files. type...
Card Puncher Data Processing
DOS - Parse a property file

How to parse a property file in DOS with the F option of the FOR loop. Dos Script to list the content Conditional processing with a if statement:
Card Puncher Data Processing
DOS - String Variable

data type in DOS When you want to use the time, you will get colon that you can't use in a file name. To overcome this problem, you can replace the colon with an underscore. The slice operation...
Card Puncher Data Processing
DOS ErrorLevel - How to manage errors with the exit code ?

This article shows you how to manage error handling with the exit code of DOS. In dos: When the errorlevel is: * = 0, then No Error occurred * > 0, then an error occurred You can test if with...
Card Puncher Data Processing
Dos - (Batch) Script

The batch script is a text file with the extension .bat or .cmd that is interpreted by a batch interpreter. You can call a batch script by : writing its name in the source script: The script execution...
Card Puncher Data Processing
Dos - (Environment) Variable

How to manage variable in dos. A variable name: must only be made up of: alphabetic characters, numeric characters and underscores, cannot begin with a numeric character. cannot be...
Card Puncher Data Processing
Dos - Command

A command is: a Dos command (See below). of an utility. The DOS command are extended with management tools located in C:\Windows\System32...
Card Puncher Data Processing
Dos - Command line (Argument|Parameter)

Command line arguments for DOS are passed to: the batch script through the cmd or call command: the function (subroutine) through the call command: variablesIf Defined Variable statement “”...



Share this page:
Follow us:
Task Runner