[Bat file for window cmd] Bat 파일 비교 조건 연산자 if else

Posted by [하늘이]
2020. 10. 23. 11:45 IT/PC Util 및 Tips
반응형

Bat 파일의 조건 연산자 및 샘플


EQU – 같음
NEQ – 같지 않음
LSS – 보다 작은
LEQ – 작거나 같음
GTR – 보다 큰
GEQ – 크거나 같음


– Syntax

if (condition) dosomething

:: For if..else if
if (condition) (statement1) else (statement2)

Batch File If Else Example To Check If Variable Is Defined Or Not

@echo OFF

::If var is not defined SET var = hello
IF "%var%"=="" (SET var=Hello)

:: This can be done in this way as well
IF NOT DEFINED var (SET var=Hello)

Either way, it will set var to 'Hello' as it is not defined previously.

Batch File If Else Example To Check If A File Or Folder Exists

EXIST command is used to check if a file exists or not. Read this article to know details of EXIST and all the other batch file commands.

@echo OFF

::EXIST command is used to check for existence
IF EXIST D:\abc.txt ECHO abc.txt found
IF EXIST D:\xyz.txt (ECHO xyz.txt found) ELSE (ECHO xyz.txt not found)

PAUSE

Now, let’s assume we have "abc.txt" in D drive and "xyz.txt" doesn’t exist in D: , then it will generate following output.

Refer Url : http://www.trytoprogram.com/batch-file-if-else/

반응형