----^--->
/\

//\\
//The\
/Sy
stem\
////
Guard
\

In the mount of the LORD it shall be seen. Genesis 22:14

IF

Booming Or Fuming? | MasterCatalog | ntlib (FREE) | NTCmdLib | MtCmds | Scripts | Almost Free | TheGuardBook | Help

 

Common
Command Set
INTERNAL

  ASSOC | BREAK | CALL | CD | CHDIR | CLS | COLOR | COPY | DATE | DEL | DIR | ECHO | ENDLOCAL | ERASE | EXIT | FOR | FTYPE | GOTO | IF | MD | MEM | MKDIR | MOVE | PATH | PAUSE | POPD | PROMPT | PUSHD | RD | REM | REN | RENAME | RMDIR | SET | SETLOCAL | SHIFT | START | TIME | TITLE | TYPE | VER | VERIFY | VOL

K

E

Y

Common Commands INTERNAL External /SWITCH Parameter Help text AddonTool
Mounted Commands .Mount/\Command CmdShorthand #Constant $FUNCTION :Procedure !GuardNote
Operating Systems NT/2K/XP/K3 NT Only NT/2K 2K Only 2K/XP XP Only XP/K3 K3 Only 2K/XP/K3

Related Resources from the NT/2K/XP/K3 Command Library

Resource

  Short Description
.ifOS   Evaluates true ONLY if running on specified OS (OS =  NT, 2K, XP or K3)
.ifNotOS   Evaluates true ONLY if NOT running on specified OS (OS = NT, 2K, XP or K3)
.ifDOW   Evaluates true if DayOfWeek is present in the output of DATE /t.
.ifELn   Evaluates true if errorlevel is EXACTLY n. (unlike IF ERRORLEVEL n)
.ifOK   Evaluates true if the errorlevel is 0 or less (-1, -2, etc.)
.ifErr   Evaluates true if the errorlevel is 1 or more (2, 3, etc.)
.if/switch
.ifNot/switch
  Evaluates true if /switch was or was not specified when calling the script.
See the Parse_Command_Line_Switches procedure for details and examples.

Go straight to !GuardNotes. (updated 2004-07-13)


This is the Mounted Help Text.  We also archive the Common Help Text for NT, 2K, XP and K3

Description

Performs conditional processing in batch programs.

Syntax

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

Parameters and Switches

NOT

  Specifies that Windows NT 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.

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

string1==string2

  Specifies a true condition if the specified text strings match.

EXIST filename

  Specifies a true condition if the specified filename exists.

Examples, Notes and Instructions

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.

NT only

Using this and the above numerical comparision operators, you can do the following

choice
GOTO answer%ERRORLEVEL%
:answer0
ECHO You typed Y for yes
:answer1
ECHO You typed N for no

you can also using the numerical comparisons above:

2K/XP/K3

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 the 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.


GuardNotes

Things that are different (by design, by accident or otherwise)

Lots of things - We're getting them all together!  Check back soon!


Common
Command Set
INTERNAL

  ASSOC | BREAK | CALL | CD | CHDIR | CLS | COLOR | COPY | DATE | DEL | DIR | ECHO | ENDLOCAL | ERASE | EXIT | FOR | FTYPE | GOTO | IF | MD | MEM | MKDIR | MOVE | PATH | PAUSE | POPD | PROMPT | PUSHD | RD | REM | REN | RENAME | RMDIR | SET | SETLOCAL | SHIFT | START | TIME | TITLE | TYPE | VER | VERIFY | VOL
/\
//\\
//
The\
/Sy
stem\
////
Guard
\
COME ON UP WITH US!
and
Congratulations On Your Decision
to
RISE ABOVE
THE REST!

^
/
!\
/LIB\

TheSystemGuard.com [Revised: 2007-03-29]
Copyright © 1995-2007, JWC
Computer Communications,
All Rights Reserved, Worldwide.
 Disclaimer       Privacy       About Us       More


Communication Is Business!

We Master It With Knowledge!

^
/
.\
/M^C\