BAT script parameter if goto choice for use of learning notes.

Source: Internet
Author: User

Wrote several times the bat script, but has not summed up, recently found a webpage introduced bat, summed up very well, transferred from http://www.jb51.net/article/49627.htm:

This article only summarizes I will not, the comprehensive view original webpage is possible.

1 parameters

'% ': parameter,%[1-9] is a parameter, and multiple arguments are separated by a space or tab. Variables can represent the batch command itself from%0 to%9,%0, and the other parameter strings are represented in the%1 to%9 order .

    1. Example 3: C: Root directory The next batch file name is T.bat, the content is: @echo off type%1 type%2
    2. Then run C:\>t a.txt b.txt%1: a.txt%2: Represents b.txt so the above command will sequentially display the contents of the A.txt and b.txt files.

2 if

1. If [NOT] "parameter" = = "string" command to execute

Example: If "%1" = = "A" format a:

2. If [not] exist [path \] filename to execute command

Example: if exist C:\Config.sys type C:\Config.sys
Indicates that if a C:\Config.sys file exists, its contents are displayed.

3. If errorlevel < digital > Pending command (This command is very interesting, so write more.) )

Very DOS programs will return a numeric value to indicate the result of the program running after the run, and the IF ERRORLEVEL command can determine the return value of the program, depending on the return value to determine the execution of different commands (the return value must be in the order from large to small ).

Example: if errorlevel 2 goto x2.

Here are the return values of several commonly used commands and what they mean:

Backup 0 Backup succeeded 1 not found back file 2 file sharing conflict blocked backup completed 3 user aborted backup with CTRL-C 4 aborted backup operation due to fatal error

Diskcomp 0 Disk Compare same 1 disk compare different 2 user by Ctrl-c Abort compare Operation 3 due to fatal error make comparison operation abort 4 preset error abort comparison

diskcopy 0-Disk copy operation succeeded 1 non-fatal disk read/write error 2 user end copy operation via ctrl-c 3 abort copy operation due to fatal processing error 4 preset error

Format 0 formatted successfully 3 users by Ctrl-c abort formatting 4 due to fatal processing error to make formatting abort 5 at the prompt "Proceed with format (y/n)?" Under User type N end

xcopy 0 successfully copied file 1 not found copy file 2 user aborted copy operation via CTRL-C 4 preset error preventing file copy operation 5 write error during copy

3 Goto This is often seen and does not require much explanation:

Example: Goto end

: End echo This is the end

The label is defined with a ": string" and the line of the label is not executed.

4 Choice Use this command to allow the user to enter a character (for selection) to use with the if errorlevel.

Note: The choice command is a DOS or Windows system-provided external command , different versions of the choice command syntax will be slightly different, please use the choice/? View usage.

Example:
CHOICE/?
CHOICE/C ync/m "Confirm please press Y, no press N, or cancel please press C. "
choice/t 10/c ync/cs/d y:10 seconds timeout, with y n C three options, default Y.
choice/c ab/m "option 1 Please select a, option 2 please select B. ": Provides a B two option, with a text.
CHOICE/C ab/n/M "option 1 Please select a, option 2 please select B. ": function as above, but hide the list of options in the prompt.

Example: The contents of Test.bat are as follows (note that when you use if errorlevel to determine the return value, you want to sort by the return value from high to low): @echo off choice/c dme/m "Defrag,mem,end" if errorlevel 3 goto end if errorlevel 2 goto mem If errotlevel 1 goto defrag

:d Efrag C:\dos\defrag Goto End

: Mem Mem Goto END

: End echo Good bye

5 for this usage is a bit special, there is not understand the place, but also need to understand:

Syntax: Executes a specific command for each file in a set of files.

For%%variable in (set) do command [Command-parameters]

%%variable specifies a single-letter replaceable parameter. (set) specifies one or a set of files. Wildcard characters can be used. command specifies the commands to execute on each file. Command-parameters specify parameters or command-line switches for specific commands.

For example, there is a single line in a batch file: for%%c in (*.bat *.txt) does type%%c

The command line displays the contents of all files with the bat and txt extensions in the current directory.

BAT script parameter if goto choice for use of learning notes.

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.