Special command
if goto choice for is a more advanced command in batch files. If you use these very skillfully, you are an expert in batch files.
1. If is a conditional statement used to determine whether the specified conditions are met and thus decide to execute different commands. There are three formats:
1. if [not] "parameter" == "string" command to be executed
If the parameter is equal to the string specified by (not means unequal, the same below), the condition is true, run the command, otherwise run the next sentence.
Example: if "%1"=="a" format a:
==== willsort Editor's Note =====================================================================
The description of this point in the command line help of if is:
IF [NOT] string1==string2 command
Here are the following points to note:
1. Double quotes containing strings are not necessary for syntax, but are just a "air defense" character used in habit
2. string1 may not be a parameter, it can also be an environment variable, loop variable, and other string constants or variables.
3. command is not necessary for syntax. string2 followed by a space can form a valid command line.
========================================================================
2. if [not] exist [path\] file name The command to be executed
If there is a specified file, the conditions are true, run the command, otherwise run the next sentence.
For example: if exist c:\ type c:\
Indicates that if a c:\ file exists, its contents will be displayed.
**** willsort Editor's Notes *******
The following usages can also be used:
if exist <device> command
device refers to a device loaded in the DOS system. Under win98, there are usually:
AUX, PRN, CON, NUL
COM1, COM2, COM3, COM4
LPT1, LPT2, LPT3, LPT4
XMSXXXX0, EMMXXXX0
A: B: C: ...,
CLOCK$, CONFIG$, DblBuff$, IFS$HLP$
The specific content will vary slightly depending on the hardware and software environment. When using these device names, the following three points need to be ensured:
1. The device does exist (except devices that are virtualized by the software)
2. The device driver has been loaded (standard devices such as aux, prn, etc. are defined by the system by default)
3. The device is ready (mainly refers to a: b: ..., com1..., lpt1..., etc.)
You can check the devices loaded in your system by using the command mem/d | find "device" /i
In addition, in DOS systems, devices are also considered as special files, and files can also be called character devices; because devices and files are managed using handles, handles are names, similar to file names, except that handles are not applied to disk management, but are used for memory management. The so-called device loading refers to allocating a referenceable handle to it in memory.
========================================================================
3. If errorlevel <number> command to be executed
Many DOS programs will return a numeric value after the run is completed to represent the result (or status) of the program. The if errorlevel command can determine the return value of the program, and determine the execution of different commands based on different return values (the return value must be arranged in order from large to small). If the return value is equal to the specified number, the condition is true and the command is run, otherwise the next sentence will be run.
For example if errorlevel 2 goto x2
==== willsort Editor's Note =====================================================================
The order of return values from large to small is not necessary, but is just an idiom when executing the command as goto. When using set as the command, it is usually arranged from small to large. For example, if you need to place the return code into an environment variable, you need to use the following order form:
if errorlevel 1 set el=1
if errorlevel 2 set el=2
if errorlevel 3 set el=3
if errorlevel 4 set el=4
if errorlevel 5 set el=5
...
Of course, the following loop can also be used instead, and the principle is consistent:
for %%e in (1 2 3 4 5 6 7 8...) do if errorlevel %%e set el=%%e
For more efficient and concise usage, please refer to another article I wrote about obtaining errorlevel
The reason for this phenomenon is that the judgment condition for comparing the return code of errorlevel is not equal to, but greater than or equal to. Due to the jump characteristic of goto, sorting from small to large will cause jump out at the smaller return code; and due to the "repeat" assignment characteristic of the set command, sorting from large to small will cause the smaller return code to "overwrite" the larger return code.
In addition, although if errorlevel=<number> command is also a valid command line, it is just to ignore = as a command line split when interpreting the command line.
========================================================================
2. When the goto batch file is run here, it will jump to the label specified by goto (the label is label, followed by a standard string). The goto statement is generally used in conjunction with if, and different command groups are executed according to different conditions.
like:
goto end
:end
echo this is the end
The label is defined by ": string", and the line where the label is located is not executed.
==== willsort Editor's Note =====================================================================
label is often translated as "label", but this is not widely conventional.
Goto <label> and :<label> can realize the jump in the middle of the execution, and combined with if, it can realize the conditional branch of the execution process, multiple ifs can realize the grouping of commands, similar to the switch case structure in C or the select case structure in Basic, large-scale and structured command grouping can realize the function functions in high-level languages. The following is a comparison between batch processing and C/Basic in syntax structure:
Batch C / Basic
goto&: goto&:
goto&:&if if{}&else{} / if&elseif&endif
goto&:&if... switch&case / select case
goto&:&if&set&envar... function() / function(),sub()
========================================================================
3. Choose Use this command to let the user enter a character (for selection), so as to return different errorlevels according to the user's choice, and then cooperate with if errorlevels to run different commands according to the user's choice.
Note: The choice command is an external command provided by DOS or Windows systems. The syntax of choice commands in different versions will be slightly different. Please use choice /? to view the usage.
The command syntax of choice (this syntax is the syntax of the choice command in Windows 2003, and the command syntax of other versions of choice is similar to this):
CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]
describe:
This tool allows the user to select an item from the selection list and return the index of the selected item.
Parameter list:
/C choices Specifies the list of options to create. The default list is "YN".
/N Hide the list of options in the prompt. The message above is displayed.
The option is still enabled.
/CS Allows the option to select case-separated options. By default, this tool
It is case-free.
/T timeout Number of seconds paused before making the default selection. The acceptable value is from 0
to 9999. If 0 is specified, there will be no pause, the default option
Will get a choice.
/D choice specifies the default options after nnnn seconds. Characters must be selected in /C
The item specifies a set of selections; at the same time, nnnn must be specified with /T.
/M text Specifies the message to be displayed before prompting. If not specified, the tool only
Show prompt.
/? Shows a help message.
Notice:
The ERRORLEVEL environment variable is set to the key index selected from the selection set. The first choice listed
Select to return 1, the second option returns 2, etc. If the key pressed by the user is not a valid choice,
This tool will emit a warning sound. If the tool detects an error status, it returns a 255
ERRORLEVEL value. If the user presses Ctrl+Break or Ctrl+C, the tool returns 0
ERRORLEVEL value. When using the ERRORLEVEL parameter in a batch program, drop the parameter
Order.
Example:
CHOICE /?
CHOICE /C YNC /M "Please press Y for confirmation, no, or press C for cancellation."
CHOICE /T 10 /C ync /CS /D y
CHOICE /C ab /M "Option 1 Please select a, option 2 Please select b."
CHOICE /C ab /N /M "Option 1 Please select a, option 2 Please select b."
==== willsort Editor's Note =====================================================================
I have listed the usage help of choice under win98, and it has been divided
Waits for the user to choose one of a set of choices.
Wait for the user to select one of the sets of characters to be selected
CHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]
/C[:]choices Specifies allowable keys. Default is YN
Specify allowed keys (characters to be selected), default to YN
/N Do not display choices and ? at end of prompt string.
Question marks and characters to be selected in the prompt string are not displayed
/S Treat choice keys as case sensitive.
Case sensitivity when processing characters to be selected
/T[:]c,nn Default choice to c after nn seconds
Select c by default after nn seconds
text Prompt string to display
Prompt string to display
ERRORLEVEL is set to offset of key user presses in choices.
ERRORLEVEL is set as the offset value of the character typed by the user in the character to be selected.
========================================================================
If I run the command: CHOICE /C YNC /M "Confirm, press Y, no, press N, or cancel, press C."
The screen will display:
To confirm, press Y, press N, or press C to cancel. [Y,N,C]?
Previous page12345Next pageRead the full text