[[/S] [/C | /K] string]
Among them: CMD is indispensable!
/C executes the command specified by the string and terminates.
/K Executes the command specified by the string, but keeps it.
/S modify string handling after /C or /K (see below)
/Q close response
/D Disable the Autorun command from the registry (see below).
/A Output the command to an internal pipeline or file ANSI.
/U makes the command output to an internal pipeline or file Unicode.
/T:fg sets the foreground COLOR/ background color (see color/? )
/E:ON enables command extension (see below)
/E:OFF disables command expansion (see below)
/F:ON enables file and directory name completion characters (see below).
/F:OFF disables the file and directory name completion character (see below).
/V: on will! Start extending the delayed environment variables as delimiters. Such as: /V:ON will allow! var! Allow during execution! var! Extended variable var. Var syntax extends variables at the input, which is different from FOR loop.
/V:OFF disables delay environment extension.
Note that if the string has quotation marks, you can use the command delimiter'&; &' Separate multiple commands. Moreover, for compatibility reasons, /X is the same as /E:ON, /Y is the same as /E:OFF, and /R is the same as /c, ignoring any other command line switches. If /C or /K is specified, the remaining command lines after the command line switch will be regarded as command lines; In this case, the following logic is used to handle the quotation mark character ("):
1. If all the following conditions are met, the quotation mark characters on the command line will be
Reservation:
-without /S command line switch
-exactly two quotation mark characters
-There is no special character between two quotation mark characters, and the special character is one of the following characters.
One:
-There is at least one blank character between two quotation marks.
-The name of at least one executable file is between two quotation marks.
2. Otherwise, check whether the first character is a quotation mark character. If so, the start character is discarded and the last quotation mark character on the command line is deleted, and the text after the last quotation mark character is retained.
If /D is not specified on the command line, when CMD.EXE starts, it will look for the following REG_SZ/REG_EXPAND_SZ registry variable. If one or both of them exist, these two variables will be executed first.
HKEY _ local _ machine \ software \ Microsoft \ command processor \AutoRun
And/or
By default, HKEY _ Current _ User \ Software \ Microsoft \ CommandProcessor \ Autorun command extension is enabled. You can also use /E:OFF to disable the extension of a specific call. By setting one or two REG_DWORD values in the registry of REGEDT32, you can enable or disable all extensions called by CMD.EXE on the machine and/or the user login session. EXE:
HKEY _ LOCAL _ MACHINE \ Software \ Microsoft \ Command Processor \ enable extensions
And/or
HKEY _ Current _ User \ Software \ Microsoft \ Command Processor \ Enable Extensions
To 0x 1 or 0x0. User-specific settings take precedence over machine settings. Command line switches take precedence over registry settings. The above is the introduction of the basic command, and the following is the command line extension. By default, delay variable environment extension is not enabled. You can use /V:ON or /V:OFF.
Command line switch, which is used to enable or disable the extension of the delayed environment variable called by CMD.EXE.
By setting one or two REG_DWORD values in the registry of REGEDT32, you can enable or disable the completion of all CMD.EXE calls and/or user login sessions on the computer. EXE:
HKEY _ LOCAL _ MACHINE \ Software \ Microsoft \ Command Processor \ DelayedEXPansion
And/or
HKEY _ Current _ User \ Software \ Microsoft \ Command Processor \ Delay Extension
To 0x 1 or 0x0. User-specific settings take precedence over machine settings. Command line switches take precedence over registry settings.
If deferred environment variable extension is enabled, the exclamation point character can be used to replace the value of the environment variable at execution time. By default, file and directory name completion is not enabled. You can use the /F:ON or /F:OFF command-line switch to enable or disable file name completion for CMD.EXE calls. By setting one or two REG_DWORD values in the registry of REGEDT32, you can enable or disable the completion of all CMD.EXE calls and/or user login sessions on the computer. EXE:
HKEY _ LOCAL _ MACHINE \ Software \ Microsoft \ Command Processor \ completion char
HKEY _ LOCAL _ MACHINE \ Software \ Microsoft \ Command Processor \ PathCompletionChar
And/or
HKEY _ Current _ User \ Software \ Microsoft \ Command Processor \CompletionChar
HKEY _ Current _ User \ Software \ Microsoft \ Command Processor \PathCompletionChar
Take the hexadecimal value of the control character as a specific parameter (for example, 0x4 is Ctrl-D and 0x6 is Ctrl-F). User-specific settings take precedence over machine settings. Command line switches take precedence over registry settings.
If the completion function is enabled by using the /F:ON command line switch, then the two control symbols to be used are: Ctrl-D for directory name completion and Ctrl-F for file name completion. To deactivate a character in the registry, use a space value (0x20) because the character is not a control character.
If you type one of the two control characters, finish will be called. The completion function takes the path string to the left of the cursor. If there are no wildcards, wildcards will be appended to the left and a matching path list will be established. Then, the first matching path is displayed. If there is no matching path, it will beep without affecting the display. After that, pressing the same control character repeatedly will cycle through the matching path list. Pressing the Shift key and the control character at the same time will display the list backwards. If you edit the line and press the control character again, the saved list of matching paths will be discarded and a new list will be generated. If you switch between file name and directory name completion, the same phenomenon will occur. The only difference between these two control characters is that the file ending character matches the file and directory names, while the directory ending character only matches the directory names. If the file completion is used for the built-in directory command (CD, MD or RD), the directory completion is used.
Enclose the matching path in quotation marks, and the completed code can correctly handle file names containing spaces or other special characters. At the same time, if you back up and then call file completion from this line, the text called on the right side of the cursor will be discarded.
Special characters that need quotation marks are:
& amp()[]{}^=; ! '+,`~
The application method of command line extension is introduced in detail below: Friends who are familiar with dos will find that many contents are the same as dos.
1, delete or erase
Delete one or more files.
DEL[/P][/F][/S][/Q][/A[[:] attribute]] name
Erase [/P][/F][/S][/Q][/A[[:] attribute]] name.
Names specifies a list of one or more files or directories. Wildcards can be used to
Delete multiple files. If a directory is specified, the
All files will be deleted.
/P Prompt for confirmation before deleting each file.
/F Force the deletion of read-only files.
/S Deletes the specified file from all subdirectories.
/Q quiet mode. When deleting global wildcards, no confirmation is required.
/A Select the file to delete according to the attribute.
Read-only file system file
H hide file archive file
-Prefix of "No"
If command extension is enabled, DEL and ERASE will change as follows:
The display syntax of the /S switch will be reversed, that is, only deleted files will be displayed, and files that cannot be found will not be displayed.
2. Color
Set the default console foreground and background colors.
Color [attribute]
Attr specifies the color attribute of the console output.
The color attribute is specified by two hexadecimal digits-the first is the background and the second is.
Prospect. Each number can be one of the following values:
0 = black 8 = gray
1 = blue 9 = light blue
2 = green A = light green
3 = lake blue B = light green
4 = red C = light red
5 = purple D = lavender
6 = yellow E = light yellow
7 = white F = bright white
If no parameters are given, this command will restore the color to the color when CMD.EXE started. This value comes from the current console window, the /T switch, or the DefaultColor registry value.
If the COLOR command is executed with the same foreground and background colors, the color command will set the ERRORLEVEL to 1.
For example, "Color fc" produces bright red on bright white.
3. CD or directory
Displays the current directory name or changes the current directory.
CHDIR[/D][ Drive:] [Path]
CHDIR [..]
CD[/D][ drive:] [path]
CD [..]
.. specify that parent directory to change to.
Type CD drive: displays the current directory in the specified drive.
Type a CD without parameters, and the current drive and directory will be displayed.
Using the /D command line switch, in addition to changing the current directory of the drive,
You can also change the current drive.
If extended command names are enabled, CHDIR will change as follows:
The current directory string will be converted to use the case of the disk name. So, if this is the case on the disk, CD C: \TEMP will set the current directory to C: \TEMP.
The CHDIR command does not use spaces as separators, so you can change the directory name to a subdirectory name with spaces but no quotation marks. For example:
Cd \ winnt \ profiles \ username \ programs \ start menu
Same as the following:
CD " \ win nt \ profiles \ username \ programs \ start menu "
When the extended function is disabled, you must type the above command.
4.MD or MKDIR
Create a directory.
MKDIR[drive:] path
MD[drive:] path
If command extension is enabled, MKDIR will change as follows:
MKDIR creates an intermediate directory in the path if necessary. For example, if \a does not exist, then:
mkdir \a\b\c\d
Use:
mkdir \a
chdir \a
mkdir b
chdir b
mkdir c
chdir c
mkdir d
Same. If the extension is disabled, you need to type mkdir \ a \ b \ c \ d.
5. Tips
Change the cmd.exe command prompt.
Prompt [text]
Text specifies a new command prompt.
Tips can consist of ordinary characters and the following specific codes:
$ A& (abbreviation and symbol)
Billion dollars | (pipeline)
$C ((left parenthesis)
$D current date
$E escape code (ASCII 27)
$F) (right bracket)
$ G> (greater than sign)
$H backspace key (delete the previous character)
$ L< (less than sign)
$ TERM current drive
$P current drive and path
$Q = (equal sign)
$S (space)
$T current time
$V Windows XP version number
$ _ newline
$ $ $ (currency symbol)
If command extension is enabled, the prompt command supports the following format characters:
$+Zero or more plus (+) characters according to the depth of the PUSHD directory stack; Each pushed layer has a role.
$M If the current drive is not a network drive, the remote name associated with the current drive letter or empty string is displayed.
6. Promote
Save the current directory of the POPD command, and then switch to the specified directory.
PUSHD [path |..]
Path specifies that the directory is the current directory.
If command extension is enabled, the PUSHD command accepts network paths in addition to regular drive letters and paths. If a network path is specified, PUSHD will create a temporary drive letter pointing to the specified network resource, and then change the current drive and directory with the drive letter just defined. You can assign a temporary drive letter from z: down and use the first unused drive letter you find.
7、POPD
Command options to the directory saved in the PUSHD command.
POPD
If command extension is enabled, when the POPD drive is pushed from the directory stack, the POPD command will delete the temporary drive letter created by PUSHD.
8. Settings
Display, set or delete cmd.exe environment variables.
SET [variable=[string]]
Variable specifies the name of the environment variable.
String specifies a series of strings to assign to a variable.
To display the current environment variable, type SET without parameters.
If command extension is enabled, the collection will change as follows:
You can activate the SET command with only one variable, and the equal sign or value will not show the values of all variables whose prefixes match the names already used by the SET command. For example:
Set p
All variables starting with the letter p will be displayed. If the variable name cannot be found in the current environment, the SET command will set the ERRORLEVEL.
Set to 1.
The SET command does not allow variable names to contain equal signs.
Two new command line switches have been added to the SET command:
Settings/expressions
SET /P variable=[promptString]
The /A command-line switch specifies the string to the right of the equal sign as the numeric expression to be evaluated. The expression calculator is simple and supports the following operations in descending order of priority:
()-Grouping
! ~-unary operator
*/%-arithmetic operator
+-arithmetic operator
& lt>>-logical shift
-bitwise AND
-Bitwise "different"
|-Bitwise or
= * =/=% =+=-Assignment
& amp= ^= | = & lt; & lt= & gt& gt=
-expression delimiter
If you use any logical or remainder operators, you need to enclose the expression string in quotation marks. Any non-numeric string key in the expression is used as the environment variable name, and its value has been converted to a number before use. If an environment variable name is specified but not defined in the current environment, the value will be set to zero. This allows you to calculate using the values of environment variables without typing those% symbols to get their values. If SET /A is executed on a command line other than the command script, it will display the last value of the expression. An environment variable name is required on the left side of the assignment operator. Except for hexadecimal prefix 0x and octal prefix 0, the numerical values are all decimal. Therefore, 0x 12 is the same as 18 and 022. Note that octal formulas are easily confused: 08 and 09 are invalid numbers because 8 and 9 are not valid octal numbers.
The /P command-line switch allows the variable value to be set to the line input entered by the user. Displays the specified promptString before reading the input line. PromptString can be empty.
Environment variable substitution has been enhanced as follows:
%PATH:str 1=str2%
The PATH environment variable will be expanded, and each "str 1" in the expansion result will be replaced by "str2".
In order to effectively delete all "str 1" from the extended result, "str2" can be empty.
"str 1" can start with an asterisk; In this case, "str 1" will be matched from the beginning of the extended result to the place where the rest of str 1 first appears.
You can also specify a substring for the extension.
% path: ~ 10/0,5%
The PATH environment variable is expanded, and then only five characters starting from the character 1 1 (offset 10) are used in the expanded result. If no length is specified, the default value, that is, the remainder of the variable value, is adopted. If both numbers (offset and length) are negative, the number used is the digital length of the environment variable plus the specified offset or length.
% path: ~- 10%
Extract the last ten characters of the path variable.
% path: ~0, -2%
Except for the last two characters, all characters of the PATH variable are extracted.
Finally, support for extension of delay environment variables is added. By default, this support is always disabled, but it can also be enabled/disabled through CMD. EXE /V command line switch.
Considering the limitation of current expansion when reading a line of text, it is useful to delay the expansion of environment variables, not at execution time. The following example illustrates the problem of direct variable extension:
Set VAR=before.
If "%VAR%" ==" in "(
Set VAR=after.
If "%var%" after "@echo" = = "If you see this, it works.
)
No message will be displayed because %VAR% in both if statements will be replaced when reading the first IF statement; The reason is that it contains the style of IF, which is a compound statement. Therefore, if "before" and "after" are actually compared in a compound statement, they will never be equal. Similarly, the following examples will not achieve the expected results:
Collection list =
Do set LIST=%LIST% %i for %i in (*)
Echo% List%
The reason is that it does not create a file LIST in the current directory, but only sets the list variable to the last file found. This is also because %LIST% is only extended once when reading the FOR statement; Moreover, the LIST variable at that time was empty. Therefore, the for loop we really execute is:
Do set LIST= %i for %i in (*)
This loop continues to set the list to the last file found.
Delayed environment variable expansion allows you to use different characters (exclamation points) to expand environment variables at execution time. If delay variable extension is enabled, you can write the above example as follows to achieve the desired effect:
Set VAR=before.
If "%VAR%" ==" in "(
Set VAR=after.
If! VAR! == "after" @echo If you see this, it works.
)
Collection list =
For %i in (*), do set LIST=! List! %i
Echo% List%
If command extension is enabled, several dynamic environment variables can be extended, but they will not appear in the variable list displayed by SET. Each time the variable values are extended, these variable values will be calculated dynamically. If a user uses any of these names to define a variable, the definition will replace the dynamic definition described below:
% CD %- expand to the current directory string.
% DATE %- Extend to the current DATE in the same format as the date command.
% TIME %- Extend to the current TIME in the same format as the time command.
% random%-any decimal number between 0 and 32767.
% ERRORLEVEL %- extends to the current error level value.
% cmtextversion %- Extended to the current command processor extension version number.
% cmdcline%-Extends to the original command line that called the command processor.
9, set the local
Start localization of environmental changes in batch files. Environmental changes made after the execution of SETLOCAL are limited to batch files. To restore the original settings, you must execute ENDLOCAL. When the end of the batch file is reached, an implicit ENDLOCAL command will be executed for each SETLOCAL command that has not been executed in the batch file.
SETLOCAL
If command extension is enabled, SETLOCAL will change as follows:
The SETLOCAL batch command can now accept optional parameters:
Enable expansion/disable expansion
Enables or disables command processor extensions. Please refer to CMD/? .
ENABLEDELAYEDEXPANSION/DISABLEDELAYEDEXPANSION
Start or stop the extension of delay environment variables. For more information, please
See the introduction of SET.
No matter what their settings are before the SETLOCAL command, these modifications will remain until the matching ENDLOCAL command.
If there are parameters, the SETLOCAL command will set the value of ERRORLEVEL.
If there is one of the two valid parameters, the value is zero. Using the following techniques, you can use it in batch scripts to determine whether extensions are available:
Verify that other 2> are empty.
SETLOCAL ENABLEEXTENSIONS
If ERRORLEVEL 1 echo cannot enable extension.
This method is effective, because in the old version of CMD.EXE, SETLOCAL did not set the ERRORLEVEL value. The VERIFY command with incorrect parameters initializes the ERRORLEVEL value to a non-zero value.
10, end local
End localization of environment changes in batch files. The environmental changes made after the ENDLOCAL execution are no longer limited to batch files. After the batch file ends, the original settings cannot be restored.
End local
If command extension is enabled, ENDLOCAL will change as follows:
If the corresponding SETLOCAL enables or disables the command extension with the new ENABLEEXTENSIONS or DISABLEEXTENSIONS option, the enabled/disabled state of the command extension will be restored to the state before executing the corresponding SETLOCAL command after ENDLOCAL.
1 1, if
Perform conditional processing in a batch program.
IF [NOT] ERRORLEVEL number command
IF [NOT] string 1==string2 command
If the file name command does not exist
NOT specifies that Windows XP works only when the condition is false.
This command should be carried out.
ERRORLEVEL number If the exit code returned by the last running program is equal to or greater than the specified number, the specified condition is true.
String 1==string2 If the specified text strings match, the specified condition is true.
If the specified file name exists, the specified condition is true.
Command If the condition is met, specify the command to be executed. If the specified
If the condition is false, the command can be followed by an execute ELSE.
The ELSE command of the command after the keyword.
The ELSE clause must appear on the same line after IF. For example:
If a file name exists. (
Del file name
)ELSE(
Echo the file name. Missing.
)
Because the del command needs to end with a new line, the following clause will be invalid:
If a file name exists. Del file name ELSE echoes the file name. miss
Since the ELSE command must be on the same line as the end of the IF command, the following clause will be invalid:
If a file name exists. Del file name
ELSE echoes the file name. miss
If they are all on the same line, the following clauses are valid:
If a file name exists. (del file name. ) ELSE echoes the file name. miss
IF command extension is enabled, if will change as follows:
IF [/I] string 1 comparison operation string2 command
If the CMDEXTVERSION command
If a variable command is defined
Where, the comparison operator can be:
Equal equals
NEQ- not equal to
Less is less than.
Less is less than or equal to
GTR- greater than
GEQ- greater than or equal to
And /I switch; If this switch is specified, it means that the string comparison to be made is case-insensitive. The /I switch can be used in the form of string 1==string2 of IF. These comparisons are universal; The reason is that if both string 1 and string2 are composed of numbers, then the strings will be converted into numbers for comparison.
The function of the CMDEXTVERSION condition is the same as that of the ERRORLEVEL, except that it is compared with the build number associated with the command extension. The first version is 1. Every time there is a considerable enhancement in the command extension, the version number will be increased by one.
When the command extension is disabled, the CMDEXTVERSION condition is not true.
If an environment variable has been defined, the defined condition has the same function as EXISTS, except that it takes an environment variable and the returned result is true.
If there is no environment variable named ERRORLEVEL, %ERRORLEVEL% will be expanded to a string expression of the current value of ERRORLEVEL. Otherwise, you will get its value. After running the program, the following statement illustrates the usage of ERRORLEVEL:
Go to answer %ERRORLEVEL%
: Answer 0
Echo program return code is 0.
: Answer 1
The echo program returned the code 1
You can also use the above numerical comparison:
If %ERRORLEVEL% LEQ 1 goes to ok,
If there is no environment variable named CMDCMDLINE, %CMDCMDLINE% will be extended to the original command line passed to CMD.EXE before CMD.EXE does any processing. Otherwise, you will get its value.
If there is no environment variable named cmtextversion,% cmtextversion% will be expanded to the current value of cmtextversion.
String character expression; Otherwise, you will get its value.
12, for
Execute command [command-parameter] on% variable in (set)
%variable specifies a one-letter replaceable parameter.
(set) Specifies a file or a set of files. Wildcards can be used.
Command specifies the command to execute on each file.
command parameter
Specify parameters or command-line switches for a specific command.
When using the FOR command in a batch file, use %%variable instead of %variable to specify variables. Variable names are case-sensitive, so %i is different from% i. If command extension is enabled, the following additional FOR command formats are supported:
FOR/D% variable IN (set) DO command [command-parameter]
If the collection contains wildcards, specify that it matches the directory name, not the file name.
FOR/R[[drive:]path]% variable in (set) docommand [command-parameters]
Check the directory tree with the root directory [drive:]path and point to the FOR statement in each directory. If no directory is specified after /R, the current directory is used. If the set is just a point (. ) character, enumeration directory tree.
FOR/L% variable IN (start, step, end) DO command [command-parameter]
This set represents a series of numbers that increase from beginning to end.
Therefore, (1, 1, 5) will produce the sequence 1 2 3 4 5, (5, 1, 1) will produce the sequence (5 4 3 2 1).
FOR/F[" options "]% variable IN (file-set) DO command [command-parameter]
For/f ["options"]% variablein ("string") do command [command-parameter]
FOR/F[" options "]% variable in ('command') docommand [command-parameters]
Or, if you have the usebackq option:
FOR/F[" options "]% variable IN (file-set) DO command [command-parameter]
For/f ["options"]% variablein ("string") do command [command-parameter]
FOR/F[" options "]% variable in ('command') docommand [command-parameters]
Filenameset is one or more file names. Each file has been opened, read and processed before moving to the next file in the filenameset.
Processing involves reading a file, dividing it into text lines, and then parsing each line into zero or more symbols. Then the For loop is called with the found symbol string variable value. By default, /F passes the first blank symbol separated in each line of each file. Skip blank lines. You can override the default parsing operation by specifying the optional "options" parameter. This quoted string contains one or more keywords that specify different parsing options. These keywords are:
Eol = c- indicates the end of the line comment character (only one)
Skip = n- Indicates the number of lines ignored at the beginning of the file.
Delims = XXX- Represents a delimiter set. This default delimiter set replaces spaces and tabs.
Tokens=x, y, m-n- refers to which symbol of each line is passed to for itself of each iteration. This will cause additional variable names to be assigned. M-n format is a range. The m-th symbol is designated by the n-th symbol. If the last character in the symbol string is an asterisk, an extra variable will be allocated and the reserved text of the line will be accepted after the last symbol is parsed.
Usebackq- Specifies that the new syntax has been used in the following situations:
Executes the string with back quotation marks as a command, executes the single quotation character as a text string command, and allows the file name to be extended with double quotation marks in filenameset.
Some examples may be helpful:
FOR/F " eol =; tokens=2,3* delims=," %i in (myfile.txt) do @echo %i %j %k
Will analyze every line in myfile.txt, ignore those lines that start with semicolons, and pass the second and third symbols in each line to the for program body; Symbols separated by commas and/or spaces. Note that the statement in the body of this for program references %i to get the second symbol, %j to get the third symbol, and %k to get all the remaining symbols after the third symbol. For file names with spaces, you need to enclose the file names in double quotes. In order to use double quotation marks in this way, you also need to use the usebackq option, otherwise double quotation marks will be understood as defining a string to be analyzed.
%i is specifically described in the for statement, and %j and %k are specifically described in the tokens= option. As long as you don't try to describe variables higher than the letters "z" or "z", you can specify up to 26 symbols with tokens= one line. Remember that the FOR variable is single letter, case-sensitive and global; but