Sideway
output.to from Sideway
Draft for Information Only

Content

MS DOS Command: IF
 Description
 Syntax
 Parameters
 Examples

MS DOS Command: IF

Reference from Microsoft MS-DOS cmd help

Description

Performs conditional processing in batch programs. If the condition specified by an IF command is true, MS-DOS carries out the command that follows the condition. If the condition is false, MS-DOS ignores the command. You can use this command only in batch programs.

Syntax

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

Parameters

NOT Specifies that MS-DOS should carry out the command only if the condition is false.
ERRORLEVEL number Specifies a true condition only if the previous program run by COMMAND.COM returned an exit code equal to or greater than number.
command Specifies the command that MS-DOS should carry out if the preceding condition is met.
string1==string2 Specifies a true condition only if string1 and string2 are the same. These values can be literal strings or batch variables (%1, for example). Literal strings do not need quotation marks.
EXIST filename Specifies a true condition if filename exists.

Examples

The following example tests for the existence of a directory. The IF command cannot be used to test directly for a directory, but the null (NUL) device does exist in every directory on the hard drive. Therefore, you can test for the null device to determine whether a directory exists on the hard drive.

if exist c:\mydir\nul goto process

The following example displays the message "Can't find data file" if MS-DOS cannot find the file PRODUCT.DAT:

if not exist product.dat echo Can't find data file

When a program stops, it returns an exit code to MS-DOS. For example, a value of 0 is typically used to indicate that a program was successfully executed. The ERRORLEVEL parameter lets you use exit codes as conditions.

The following example displays an error message if an error occurs during formatting of the disk in drive A. If no error occurs, the error message is skipped.

  • :begin
  • echo off
  • format a: /s
  • if not errorlevel 1 goto end
  • echo An error occurred during formatting.
  • :end
  • echo End of batch program.

For another example of how the ERRORLEVEL parameter is used, see the CHOICE command.


©sideway

ID: 110700199 Last Updated: 7/28/2011 Revision: 0


Latest Updated LinksValid XHTML 1.0 Transitional Valid CSS!Nu Html Checker Firefox53 Chromena IExplorerna
IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 339

Reference 79

Computer

Hardware 249

Software

Application 213

Digitization 32

Latex 52

Manim 205

KB 1

Numeric 19

Programming

Web 289

Unicode 504

HTML 66

CSS 65

SVG 46

ASP.NET 270

OS 429

DeskTop 7

Python 72

Knowledge

Mathematics

Formulas 8

Algebra 84

Number Theory 206

Trigonometry 31

Geometry 34

Coordinate Geometry 2

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Natural Sciences

Matter 1

Electric 27

Biology 1

Geography 1


Copyright © 2000-2024 Sideway . All rights reserved Disclaimers last modified on 06 September 2019