Common batch file commands and their usage

Source: Internet
Author: User

To create a batch file of your own, you must first understand the commonly used commands in the batch file. Then, let's first understand the common and useful commands and their usage in the next batch file.

---IfAn extremely common command that indicates conditional judgment. I will not talk about it much. Let's talk about its form.
If has three basic forms:
1) if "parameter" = "string" command to be executed
For example, if "% 1" = "A" format:
The command itself is not difficult, but it is annoying to find out what the "parameter" represents. Next time.

2) If exist file name command to be executed
For example, if exist config. sys edit config. sys
Like above, what other commands are similar to "edit"? How are they implemented?

3) If errorlevel: command to be executed
If the return code is equal to the specified number, run the command to be executed.
What is the return code? When the DOS program runs, a number is returned to DOS, which is called an error level or return code.
Example: If errorlevel 2 goto step2

---GotoJump command, jump to the label specified by Goto, the form is very simple, generally used with the if command.
What is the label? Simply put, the name is preceded by a colon, for example, Example
Example: {command1}

Goto somewhere

{Command2}

: Somewhere
Echo "when you execute goto somewhere, command2 is skipped to execute this command"

---ChoiceYou can run different commands by entering a single character. The/C: parameter should be added for use,
C: Enter the characters that can be entered. There is no space between them. 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 whose execution label is defrag, M indicates the program segment whose execution label is MEM, and E indicates that the execution label is end.
Program section, each program segment finally jumps the program to the end label with Goto end, and the program will display good bye,
End the file.

---Loop command in the format of for [% F] In (SET) do [command]
Example: For % C in (*. bat *. txt) do type % C
It indicates that if a file ends with bat or TXT, the file content is displayed.
 

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.