CMD character command use

Source: Internet
Author: User
Tags echo command echo message

CMD character command use

\ \ Hide the echo of the command.

\ \ In for indicates the use of an enhanced variable extension;
A string in set that represents the location specified using the extended environment variable;
The bitwise inverse is indicated in the set/a.

\ \ uses two% to contain a string representing the reference environment variable. For example, a%time% can be extended to the current system time;
A single% followed by a number of 0-9 indicates a reference command-line argument;
Used to denote a reference loop variable in for;
A continuous two% indicates that the execution is off to a%.

\ \ Cancels the escape character and closes the escape function for all escape characters. For example, to display some special characters on the screen, such as >>> |^, you can put a ^ symbol in front of it to display this ^ after the character, ^^ is to display a ^,^| is to display a | character;
In the set/a is the bitwise XOR;
In findstr/r [] indicates that the specified character set is not matched.

\ \ command to connect characters. For example, if I want to execute two commands simultaneously on a single line of text, I can connect the two commands with the & command;
In SET/A is the bitwise-With.

\ \ represents any arbitrary character, which is what we usually call "wildcard"; for example, to find all the text files (. txt) in the C packing directory in the root directory of the C drive, you can enter the command "Dirc:\*.txt";
In set/a, it is multiplication. For example "set/a x=4*2", the result is 8;
In findstr/r, it means that the previous character is matched multiple times.

The \ \ command contains or is a qualifier with precedence, such as the for command to use this (), we can also see it in the If,echo command;

\ \ Range notation, such as date lookup, which can be used in the tokens operation of the for command;
Connect two characters in findstr/r to indicate the matching range;
-a switch that is followed by the/post of certain commands to take the reverse.

\ \ is mainly used in the copy command to use it, indicating that many files are merged into a file, it is necessary to use the + character;
In set/a is the addition.

\ \ pipe character. is to put the output of the previous command as input to the next command. " Dir/a/b |more "You can display the information output by the dir command on a screen-by-page basis;
In the set/a is a bitwise OR;
The two switches, options, or parameters indicated in the Help document are two selected.

\ \ label Locator, you can accept the label pointed to by the Goto command. For example, in a batch file, you define a ": Begin" tag, and you can use the "gotobegin" command to go to the ": Begin" transition to execute the batch command.

" "
\ \ delimiter, in the representation of a path with a space is often used "" to enclose the path, in some commands also need "" "symbol;
The content that they contain is treated as string parsing in for/f;
In for/f "USEBACKQ" means that they contain content as a file path and analyze the contents of their files;
In other cases, the content is a complete string in which >, >>, <, &, |, spaces, and so on, are no longer escaped.

\ \ Indicates that the following character (string) is the function switch (option) of the command. For example, "dir/s/b/a-d" means the different parameters specified by the "dir" command;
The Division is represented in the set/a.

\ \ command Redirect to redirect the output of the preceding command to the device behind it, and the contents of the subsequent device are overwritten. For example, "dir> lxmxn.txt" can be used to output the results of the "dir" command to the "lxmxn.txt" text file;
The right boundary of the matched word is represented in findstr/r, which needs to be combined with the escape character \ use.

\ \ Command redirection. Redirect the output of the preceding command to the device behind it, and the contents of the back device are not overwritten.

\ \ Takes the contents of the file that follows it as input to its preceding command.
The left boundary of the matched word is represented in findstr/r, which needs to be combined with the escape character \ use.

\ \ assignment symbol for assigning values to variables. For example, "set a=windows" means to assign the string "Windows" to the variable "a";
An arithmetic operation is represented in set/a, such as "set/a x=5-6*5".

\ \ This "" symbol, in some cases, represents the root directory of the current path. For example, the current directory under C:\Windows\System32, then you "dir" words, it is quite with "Dir C:"
Represents a regular escape character in findstr/r.

In for/f, they are used as the command line to execute and parse the output of the content they contain;
In for/f "Usebackq", the strings they contain are represented as string parsing.

When a path is immediately behind or appears separately:
A. Represents the current directory;
Two. Indicates the previous level of the directory;
When the file name appears in the path:
The last one. Represents the separation of the primary file name from the extension file name.

\ \ Connect two commands and execute && after the && command is successful;

\ \ Connect two commands, when | | The previous command failed before execution | | After the command.

\ \ In the findstr command indicates the end of a line.

In for/f, the content that they contain is executed as the command line and parsed by its output.

The switches, options, or parameters that are represented in the Help document are optional;
In Findstr/r, the character set that is specified in the match.

\ \ In findstr/r to match an arbitrary character in this position;
? In the path indicates that any character is to be fitted in this position;
A help document that immediately follows/after the GET command.

\ \ When the variable delay is enabled, use!! The variable name is expanded to represent a reference to the value of the variable;
Represents a logical non in set/a. For example set/a a=!0, at this point A is a representation of logic 1.

Other information:

1,% is an escape character, usually translated as an escape character, but also has a more image of the characters, escape characters and so on. That is, the% will not only escape the specific string associated with it and replace it with a specific string, but will also be "off" itself. and similar to the escape character in the C language "", the double will escape and take off as a single, and four off to double.

2, for itself is a special command, similar to a specialized command interpreter, because its function implementation needs to execute multiple statements, so it must also have the command line (specifically the command line after do) parsing processing functions. While Command/cmd implements a for, it naturally borrows its own original command-line parsing module, so for has a two-level escape feature, the for-do statement is parsed and interpreted at two levels, and the first level is read into and interpreted for the for command row in Command/cmd. The second level usually interprets the same command line several times when the for reads in and interprets the Do command.

We can then note that when using command-line parameter variables and environment variables in do, there is no need for double-percent, because these variables are replaced with a specific invariant string constant after the first level escape and participate in all execution of the For loop Instead, the substitution variable requires constant dynamic changes during execution (in the sub-command line of Do), which naturally still needs to be implemented by the escape character, so the use of double is a necessary choice.

Also, it is important to note that there is no need for double% to use for at the command line, which stems from the way that the command interpreter handles the command line and batch processing differently. In earlier versions of DOS,% was not considered an escape character in the command line, so it was not escaped and was removed, so the environment variable could not be referenced directly at the command line at that time. When using for, only one% is needed for the for to escape and the release is sufficient. In a later version of the command interpreter, support for command-line escaping was added (primarily support for environment variables), but the tradition of command line for single use was still preserved.

The variable delay substitution in CMD is a special case, but it does not violate the above escaping principle, except that the environment variable in for is no longer a constant.

RMDIR/S/q%mhnet% 2>nul 1>nul do a simple explanation

The main idea of this code is to delete the directory specified by%mhnet%,/s to delete the subdirectories,/Q for the deletion of the directory tree without prompting for confirmation, 1>nul indicates that the directory tree will be properly removed information to suppress the output, 2>nul indicates that the deletion process of the error information to suppress the output

Both 1 and 2 are the addresses that represent a data flow input and output (NT cmd is called a handle, Msdos is called a device), and the following table (from the WinXP Help document, "using the Command redirection operator (redirectionoperators)") lists the available handles.

Numeric code description for handle handle
STDIN 0 Keyboard input
STDOUT 1 output to the command Prompt window
STDERR 2 Error output to command Prompt window
UNDEFINED 3-9 These handles are defined separately by the application and each specific tool

0 keyboard input
1 output to the command Prompt window
2 Error output to command Prompt window
3-9 These handles are defined separately by the application and by each specific tool.
2 > NUL indicates that the program error message is not displayed.
Call Attrib-r-H c:\autoexec.bat >nul
This sentence is actually:
Call Attrib-r-H C:\autoexec.bat 1 > nul
These 1,2,0 and so on is the handle, plainly is the code name, you just know 1 is the representative output information, 2 is to represent the error message, 0 is to represent the keyboard input on the line.
If there's anything you don't understand, you can see more help and support.

The command line does not limit the position where the redirect symbol appears, as long as the redirect symbol immediately follows the "character device", so the following statement is equivalent:

echo Hello world> Hello.txt
echo hello> Hello.txt World
Echo> Hello.txt Hello World
> Hello.txt echo Hello World

In the NT Series command line, the scope of redirection is changed from the entire command line to a single command statement, and is subject to the command delimiter &,&&,| | And the restriction of the block of statements.

echo message1> Msg1.txt & echomessage2> Msg2.txt
If "%target%" = "" (Echo message to screen) else (echo message tofile>%target%)

In summary, >nul intended to redirect the standard output request resulting from this command to the empty device, which is equivalent to masking the output information of this statement (not hidden), and 2>nul is masking the standard error message output when the program executes an error. They are used in conjunction, which is to mask all output information that this statement is likely to produce.

"Redirect" is a command-line feature that is present in MSDOS, which is responsible for transferring input and output requests from the specified command or statement to other "devices" by the default "console", which is the "redirect symbol" (including ">,>>,<" three, See [1] for their respective meanings in the sentence.

The General command-line input and output requests are completed by internally defining three "ports" (called "handles" under NT, not defined in Dos), respectively, as standard input stdin, standard output stdout, and standard error stderr. They typically point to the device as the console (console, code con), where stdin points to the console's keyboard and stdout/stderr the monitor to the console. As a result, the console usually refers to a combination of keyboards and monitors, a concept that is reflected on the terminals of an earlier mainframe. The stdin can be < redirected, stdout can be >, >> redirect, and stderr in DOS is not redirected directly, only through Ctty or other commands to transfer the control of the system to other devices, to indirectly complete.

"Device" means a device driver or port code that controls the PC hardware or port, which is typically implemented and supported by the system's underlying or hardware driver. For example, the console con implemented by Io.sys, the system clock clock$, the unknown device config$, the first serial Port AUX, the first and the Port PRN, all the serial port COM1~COM4, all the same port LPT1~LPT3, the available drive letter a:-x: And the above mentioned empty device nul. There are many other devices, such as the Emmxxxx0,ifshlp.sys implementation of the Xmsxxxx0,emm386.exe implementation of the Himem.sys implementation of ifs$hlp$, and so on.

In these devices, the input and output information can be processed very little, only con, nul and connected with the input and output hardware (printer, modem, etc.) of the serial or the same port device. They are called "character devices", and disk files are selected as a special character device, which greatly expands the degree of freedom and practicality of redirection, so that many people also refer to redirection as "file redirection."

The empty device nul is a special device because it has no controllable PC hardware or port, but only a fictitious device or port, it only exists at the software level. Because of this, it can accept all redirected input and output requests without giving any response (end the input request without giving any input information under NT, and terminating the response after the DOS is padded with 127 bytes of 0), which makes it much like the "black hole" in astronomy that can devour all matter and information, It is similar to the philosophy can turn yin and yang out of the "Xuan Xuan Tao". It exists because we need a "Recycle Bin" that can silently and unconditionally absorb a variety of redundant output information or input requests, just as a "black hole" is like a huge "cosmic dump".

CMD is not insane, is set processed by the integer is too large, set using double-byte storage integer, there is 32-bit storage range limit, that is, its processing range is 2^-31~2^31-1, your disk space exceeded this range overflow.

I do not have a very good solution to this, only after discarding the three bits after the approximate algorithm divided by 1049.

for/f "Tokens=3"%%a in (' dir/-c c:\^|find ' free bytes "') do setfreesize=%%a
set/a Freesize=%freesize:~0,-3%/1049>nul
Echo freesize:%freesize%

> Create a file
>> Append to a file
@ prefix character. Indicates that the line is not displayed in CMD when executed, and can be turned off using echo off
^ Leading characters for special symbols (> < &). The first one just shows AAA second output file BBB
Echo 123456 ^> AAA
echo 1231231 > BBB
() contains commands
(Echo AA & Echo BB)
, and the default separator symbol, like a space.
; Comment, which is followed by a comment
: Marking function
│ Pipeline operation
Symbols can be used for different purposes when the command is the same; isolated but with no change in execution. If an error occurs during execution, only the error report is returned but the program will continue to execute

First, @ is not a command, but rather a special token for the DOS batch, only for masking command-line echo. Here are some special tokens that you might see in the DOS command line or batch:
CR (0D) command line terminator
Escape (1B) ANSI escape character guide
Space (20) commonly used parameter specifiers
Tab (09); = Non-commonly used parameter qualifiers
+ Copy command file connector
* ? File wildcard characters
"" String delimiter
| Command pipe character
< > >> file redirection
@ command-line Echo mask
/Parameter Switch guide
: Batch Label Guide
% Batch Variable guide

Second,:: indeed can play the role of REM annotation, and more concise and effective; But there are two points to note:
First, in addition to: outside, any of the following: The beginning of the word lines, in the batch is regarded as the label, and directly ignore all the contents thereafter, just to distinguish from the normal marking, we recommend using Goto is not recognized by the label, that is, immediately after a non-alphanumeric special symbol.
Second, unlike REM, the following character lines is not echoed at execution time, regardless of whether the command-line echo is turned on with Echo on, because the command interpreter does not consider him to be a valid command line, and for this point, REM will be more applicable in some cases than:: In addition, REM can be used in Config.sys files.

You can also use the following usage:
If exist command
Device refers to devices that have been loaded in a DOS system, usually under Win98:
A:B: C: ...,
clock$, config$, dblbuff$, ifs$hlp$
The specific content will vary slightly depending on the hard software environment, and the following three points need to be guaranteed when using these device names:
1. The device does exist (except for devices that are virtual by the software)
2. The device driver is loaded (AUX, PRN and other standard devices are defined by the system default)
3. The device is ready (mainly refers to a:b: ..., com1 ..., lpt1 ... such as
Available by Command Mem/d | Find "Device"/I to review the devices that are loaded in your system
Also, in a DOS system, a device is considered a special file, and a file can also be called a character device, because the device and the file are managed using a handle (handle), and the handle is the name, similar to the file name, except that the handle is not applied to disk Management. Instead, it is applied to memory management, so-called device loading refers to allocating a reference handle to it in memory.

For your safety, please only open URLs with reliable sources

Open site cancellation


CMD character command use

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: 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.