The _dos/bat of CMD if condition condition judgment

Source: Internet
Author: User
Tags echo d goto
1, to determine whether the drive, file or folder exists, with if exist statement;
2, to determine whether the two strings are equal, with the If "string 1" = "string 2" statement;
3, to determine whether the two values are equal, using the IF value 1 equ value 2 statements;
4, to determine whether a variable has been assigned value, using if defined STR statement;
The full format of the IF statement is this: if conditional expression (statement 1) Else (statement 2), which means that if the conditional expression is set up, then the statement 1 is executed, otherwise the statement 2 is executed.
For each of the above four scenarios, you can use the following code:
1. If exist D:\test.txt (Test.txt exists under echo D disk) Else (test.txt not present under Echo D disk)
2, if "abc" = = "xyz" (Echo string ABC equals string xyz) Else (Echo string ABC is not equal to string xyz)
3, if 1 equ 2 (echo 1 equals 2) Else (echo 1 is not equal to 2)
4, if defined str (echo variable str has been assigned a value of%str%) else (the value of echo variable str is null)
When judging whether strings are equal, if the case is case-sensitive, for example, the simple if statement will assume that the string ABC is not the same as the string ABC, and if you do not want to be case-sensitive, you need to add the/I switch, using the if/i "string 1" = "string 2" format; Rather than a separate "=".
To determine the size relationship between two values, in addition to being equal to equ, there are other relationship symbols, and all the relationship symbols that apply to if statements are shown in the following table:
Chinese meaning relation character English explanation
Equal to Equ equal
Greater than gtr greater than
Greater than or equal to geq greater than or equal
Less than LSS less than
Less than or equal to leq less than or equal
Not equal to NEQ no equal
The IF statement also has a thin format: an If conditional expression statement, which means that if a conditional expression is set up, the statement is executed, otherwise, nothing is done.

Instance:

Save the following as a bat file

Copy Code code as follows:

@echo off
If exist E:\123.bat Goto St
copy/y C:\123.bat E:\123.bat
Goto St
: St
Start E:\123.bat
Exit


Description

To determine if a file exists, exist, execute, and do not exist, copy the file to the target and then execute it. Can be changed according to the file name, can be EXE, BAT, CMD, COM and other executable files.

bat Programming-choice command

Choice command

The choice command allows the user to enter a character to run a different command with the command format "choice [/C choices] [/n] [/cs] [/T timeout/d Choice] [/M text]". The parameters have the following meanings:

/C choices Specifies the list of options to create. The default list is "y/n".
/n Hides the list of options in the prompt. Prompts the previous message that the Display option is still enabled.
/cs allows you to select options that are case-insensitive. By default, this tool is not case-by-case.
/T Timeout The number of seconds to pause before making a default selection. The acceptable value is from the 0~9999. If 0 is specified, it is not paused and the default option is selected.
/d choice Specifies the default option after n seconds. The character must be in a set of selections specified with the/C option, and n must be specified with/T.
/M text Specifies the message to display before prompting. If not specified, the tool displays only prompts.

Example: use of the choice command

In the Command Line window, enter "CHOICE/C ync/m" to confirm, press Y, no, press N, or cancel, press C. "", as shown in the figure after running.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.