Use several advanced commands for dos batch file processing to determine

Source: Internet
Author: User

If goto choice for is a relatively advanced command in the batch processing file. if you are familiar with these commands, you are an expert in batch processing files.

If indicates whether the specified conditions are met, and then different commands are executed. There are three formats:
1. if "parameter" = "string" command to be executed
If the parameter is equal to the specified string, the condition is true. Run the command. Otherwise, run the next sentence. (Note that there are two equal signs)
For example, if "% 1" = "a" format:

2. if exist file name command to be executed
If a specified file exists, the condition is true. Run the command. Otherwise, run the next sentence. For example, if exist config. sys edit config. sys

3. if errorlevel: command to be executed
If the return code is equal to the specified number, the condition is true. Run the command. Otherwise, run the next sentence. For example, if errorlevel 2 goto x2 DOS returns a number to DOS when running the program, which is called the error code errorlevel or return code.

When the goto batch file runs here, it will jump to the label specified by goto, which is generally used with if. For example:

Goto end

: End
Echo this is the end

The label is represented by a string. The row where the label is located is not executed.

Choice uses this command to allow users to enter a single character to run different commands. The/c: parameter should be added for use, and c: should be followed by a prompt to enter characters without spaces. Its return code is 1234 ......

For example: choice/c: dimethyl defrag, mem, end
Will display
Defrag, mem, end [D, M, E]?

For example, the content of test. bat is as follows:
@ Echo off
Choice/c: dimethyl defrag, mem, end
If errorlevel 3 goto defrag, first judge the highest error code.
If errorlevel 2 goto mem
If errotlevel 1 goto end

: Defrag
C: \ dos \ defrag
Goto end

: Mem
Mem
Goto end

: End
Echo good bye

After this file is run, defrag, mem, end [D, M, E]? You can select d m e, and then the if statement will make a judgment. d indicates the program segment with the execution label as defrag, and m indicates the program segment with the execution label as mem, e indicates the program segment whose execution label is end. Each program segment finally jumps the program to the end label using goto end. Then, the program displays good bye, and the file ends.

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.