DEV Community

Victor James
Victor James

Posted on • Originally published at windowscmd.com on

IF

IF

IF

The IF windows command-line tool is for Conditionally performing a command.

CMD Line

CMD Line is a text-based interface that transfers the command from the user to the OS.

CLI-Command Line Interpreter

The command-line interpreter (CLI) for windows is CMD.EXE. Also, you can use PowerShell which can automize many system administration tasks and these tasks are not approachable in command prompt. With the help of the CLI, you can communicate with your system by typing the text in commands. As a result, your system answers to the text that you have typed previously, and then the user can proceed with the next command line that shows up.

To open the Run command Window

Run command window helps you to run programs, open the files and folders. And there are several ways to open up the run command dialog box. This run command box is useful for you to head straight to the destined program easily.

By using the Keyboard shortcut Key:

WINDOWS+R

One of the most commonly used methods is the Shortcut key method. Also, this method is in all versions of Windows. You need to press the Windows+ R key concurrently to open the Run command dialog box.

Run command box
Run command box

What does the IF command do?

This windows command-line tool performs conditionally processing in batch programs. This command is for Conditionally performing a command. The command is an internal command. This command is utilized in batch files.

Syntax

File Syntax


 IF [NOT] EXIST filename command

 IF [NOT] EXIST filename (command) ELSE (command)

Enter fullscreen mode Exit fullscreen mode

String Syntax


   IF [/I] [NOT] item1==item2 command
   IF [/I] [NOT] "item1" == "item2" command

   IF [/I] item1 compare-op item2 command

   IF [/I] item1 compare-op item2 (command) ELSE (command)

Enter fullscreen mode Exit fullscreen mode

Error Check Syntax


   IF [NOT] DEFINED variable command

   IF [NOT] ERRORLEVEL number command 

   IF CMDEXTVERSION number command

Enter fullscreen mode Exit fullscreen mode

Parameter Explanation

  • item – A text string or environment variable , for more complex comparisons, a variable can be modified using either Substring or Search syntax.
  • command – Specifies the command to perform.
  • filename – A file to test or a wildcard pattern.
  • NOT – Performs the command if the condition is false.
  • == – Performs the command if the two strings are equal.
  • /I – Do a case Insensitive string comparison.
  • compare-op – can be one of
    • EQU : Equal
    • NEQ : Not equal
    • LSS : Less than <
    • LEQ : Less than or Equal <=
    • GTR : Greater than >
    • GEQ : Greater than or equal >=

The command will only analyze numbers while one of these (EQU, NEQ, LSS, LEQ, GTR, GEQ) is used.

The == comparison operator always results in string comparison.

ERRORLEVEL

  • The Errorlevel checking is of two methods , the first syntax (IF ERRORLEVEL …) provides compatibility with ancient batch files from the days of Windows 95.
    • IF ERRORLEVEL n statements should be read as IF Errorlevel >= number
    • That is as
    • IF ERRORLEVEL 0 will return TRUE whether the error level is 0, 1 or 5 or 64
    • IF ERRORLEVEL 1 will return TRUE whether the error level is 1 or 5 or 64
    • And IF NOT ERRORLEVEL1 means if ERRORLEVEL is less than 1 (zero or negative).
  • This method is not that clear , and identifying the negative error numbers seems difficult.

  • Another method is the %ERRORLEVEL% variable, available in Windows 2000 or later versions. This method is a more logical method of checking Error levels,

    • IF %ERRORLEVEL% NEQ 0 Echo An error was found
    • IF %ERRORLEVEL% EQU 0 Echo No error found
    • IF%ERRORLEVEL% EQU 0 (Echo No error found) ELSE (Echo An error was found)
    • IF %ERRORLEVEL% EQU 0 Echo No error found || Echo An error was found
  • From this method, you can trap errors and can also test for particular errors too.

  • You can use EXIT /B to deliberately raise an ERRORLEVEL in a batch script.

Testing if a Variable is Empty

  • In order to test for the existence of a command-line parameter , you can use empty brackets like this:

IF [%1]==[] ECHO Value Missing
or
IF [%1] EQU [] ECHO Value Missing

Enter fullscreen mode Exit fullscreen mode
  • The [] brackets are used while comparing a variable that might be empty, so [] the command has something to compare.
    • As a result, IF [] EQU [] will return True.
  • You can use any characters like ‘ ~’ or curly brackets, {} or even the number 7 , but the square brackets are given because they did not have any special meaning.

The filenames and path files should be given inside the quotes.

Testing if a variable is NULL

  • In case, a variable is NULL, it will remove the definition of the variable , so it becomes

IF NOT DEFINED _example ECHO Value Missing

  • The result of the variable if it has any value will return as true.
  • You can use the IF DEFINED VariableName to test for the existence of a user variable.

Testing the files and folders existence

  • IF EXIST filename finds the existence of a file or a folder.
  • The scrip t empty.cmd will confirm if the folder is empty or not.

Parenthesis

  • The Parenthesis can be used to split commands across multiple lines. This enables writing more complex IF… ELSE… commands like,

IF EXIST foldername.txt (
    Echo deleting foldername.txt
    Del foldername.txt
 ) ELSE ( 
    Echo The folder was not found.
 )

Enter fullscreen mode Exit fullscreen mode
  • While combining the ELSE statement with parenthesis , it will always place the opening parenthesis on the same line as ELSE. Because CMD does a one-line-at-a-time parsing of the command.

Pipes

  • The expressions are evaluated from left to right while piping commands
  • For example
    • IF SomeCondition Command1 | Command2 is equal to
    • (IF SomeCondition Command1 ) | Command2
  • Regardless of whether SomeCondition is TRUE or FALSE, the pipe is created and Command2 is always run.

Remarks

  • In case if the string is analyzed by an IF command includes delimiters such as [Space] or [Comma], then either the delimiters must be escaped with a caret ^ or the whole string must be “quoted”.
  • As a result, the statement will treat the string as a single item and not as several separate strings.

Testing Numeric values

  • Only when the compare-op operators are used, the c ommand analyses the number.
  • The == comparison operator always results in string comparison.
  • The correct form of numeric comparison is
    • IF 3 GEQ 15 echo “bigger”
  • The command should work within the full range of 32-bit signed integer numbers (-2,147,483,648 through 2,147,483,647)
    • C:> if 2147483646 GEQ 2147483647 (Echo Larger) Else (Echo Smaller) Smaller > correct

Examples

  • To go to the okay label when the value of the ERRORLEVEL environment variable is less than or equal to 2 use

if %errorlevel% LEQ 2 goto okay

Enter fullscreen mode Exit fullscreen mode
  • To delete the foldername from the current directory or display a message or else when foldername is not found, type the following lines in a batch file

IF EXIST foldername.txt (
    Echo deleting foldername.txt
    Del foldername.txt
 ) ELSE ( 
    Echo The folder was not found.
 )

Enter fullscreen mode Exit fullscreen mode

Verdict

Through this article, hope you get to know about the IF command. Let us know in the comment section, about your suggestions or questions regarding this article, we will update it to you shortly.

Related Articles

  • IF The IF windows command-line tool performs conditionally processing in batch programs. This command is for Conditionally performing a command.
  • LGPO The LGPO windows command-line tool is for the Local Group Policy Object utility. This command automates the management of local group policy.
  • FORFILES The FORFILES command-line tool is to execute a command on a file or set of files. This command was executed mostly in batch files.
  • FORMAT The FORMAT windows command line is for formatting a disk. To format a disk for the use of Windows.
  • FTP The FTP stands for File Transfer Protocol. This command-line tool is transfers files from a computer executing an FTP server service.

The post IF appeared first on Windows Commands- SS64 Commands.

Discussion (0)