[Windows7] [cmd] if

COM2010. 11. 8. 16:05 |

IF 문을 사용하면 조건분기를 할 수 있다.

구조는 다음과 같다.

IF condition command

조건 부분이 참이면, 뒤에 나오는 명령이 실행된다.

IF NOT   은 조건이 거짓이면, 뒤에 나오는 명령을 실행한다.

EXIST 는 상술된 파일 또는 폴더가 존재하는지를 판단한다.

ERRORLEVEL 숫자  는 최근실행한 명령의 종료코드가 상술된 숫자이상인지 판단한다.

ELSE 문으로 나머지 경우에 대해서도 분기시킬수 있다. ( 단, IF 하고 한줄에 씌거나, IF 블럭의 닫는 괄호와 한줄에 와야한다 )

조건의 비교가 문자열인경우에 " " 를 써줘야 한다.


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 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 (                                                                  if 절의 닫는 괄호와 한줄에 왔으므로 오케이.
        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               del 명령어가 ELSE 포함해서 싸그리 파일명으로 받아들임.
                                                                                                            줄을 바꾸거나 괄호로 감싸야 되는데, 줄을 바꾸면 ELSE 가 안먹음
                                                                                                            그러므로, 괄호로 감싸는게 답임.
                                                                                                            또한,괄호로 감싸면, ELSE를 다음줄로 넘길수도 있음.

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                                                                  이러면 ELSE 명령 모른다고 뜸.

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                                                                 EQU 는 '같다'    이와 같은 표현으로  ==   를 쓸 수 있다.
    NEQ - not equal                                                            NEQ 는 '같지않다'
    LSS - less than                                                             LSS 은 '오른쪽 것보다 작다'
    LEQ - less than or equal                                                LEQ 는 '오른쪽 것보다 작거나 같다'
    GTR - greater than                                                         GTR 은 '오른쪽 것보다 크다'
    GEQ - greater than or equal                                            GEQ 는 '오른쪽 것보다 크거나 같다'

and the /I switch, if specified, says to do case insensitive string compares.                       /I 스위치는 대소문자 구별을 하지 않도록 한다.
                                                                                                                                      ( 디폴트는 대소문자를 구별한다. )
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 EXIST
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 use 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.