The tutorial will be continuously updated and supplemented later. Newcomers are kindly expected.
You are also welcome to join. plp626 is willing to discuss and share with you if there are new discoveries.
Blog--Study Notes
-------Suitable for those with certain foundation---------
~~~~~~~~~~~~~Pleaseguysdonotpostwaterposts!
Can-----> Put forward suggestions and opinions.
Can-----> Criticize!
Welcome-----> Point out mistakes!!!
~~~~~~~~~~~~~Thankyou!
Declaration: I can't say that all the codes in this post are original, but when citing, be sure to note from cn-dos.net
There are 71 built-in commands in cmd under Windows XP. Each command has its own help information. Now, first export all these helps to the cmdhelp directory on the desktop.
Here is a code:
@echo off&mode con lines=5 cols=50
md cmdhelp ||(pause&exit)
title Exporting cmd help information to the cmdhelp directory, please wait...
chcp 437>nul&call :help E
graftabl 936>nul&call :help C
cd cmdhelp
set mark=───────────────────────────────────
for %%i in (*.E) do (
echo.>>%%i &echo %mark%>>%%i
copy %%~ni.E+%%~ni.C EC_%%~ni.txt>nul
echo %mark%>>EC_%%~ni.txt &echo.>>EC_%%~ni.txt
)
del *.c;*.e
find /v "" *.txt>ALL.help
title Completed. Press any key to view. &pause>nul
start notepad ALL.help
goto :eof
:help
for /f %%i in ('help^|findstr "^"') do help %%i>>cmdhelp\%%i.%1
goto :eof
I still recommend reading the original English help. This combines the English version with the Chinese version. If your English is a bit poor, you can read them together.
I originally wanted to make it in htm format. I haven't figured it out yet. There are already people in the forum who have written relevant codes. You can search and refer to them to modify.
(P.S.: The relevant code has been given by ZJHJ on floor 17, you can refer to it)
The following post is what I wrote while learning. I will add more if I gain something, so modifications are inevitable. Some of the usages may not be explained in the help. These are mostly summarized by me from the predecessors in the forum.
Graspvariableinterception%str:~x,y% means: The offset of %str% is at position x, and the length is y characters.
~~~~~~~~~plp626 on 2008-1-28 ~~~~~~~~~~
start────────────────────────────────────────────────
Often need to use variable interception, so this command must be mastered proficiently. Here, x and y are positive and negative. There are a total of 4 situations. How are they intercepted respectively?
Do a small test, demonstrate the results at the command prompt:
echo off
set str=%date%
echo %str%
2008-01-28 星期一This display result means that the value of variable %str% is "2008-01-28 星期一"
set a=%str:~2,4%
echo %a%
08-0This means that starting from the right side of the 2nd character of %str%, intercept 4 characters backward. The value of variable %a% is this. The same below.
set b=%str:~6,-2%
echo %b%
1-28 星Starting from the right side of the 6th character of %str%, intercept the remaining characters after discarding the last 2 characters of %str%. The value of variable %b% is this.
set c=%str:~-3,2%
echo %c%
星期Starting from the left side of the 3rd character from the end of %str%, intercept 2 characters backward. The value of variable %c% is this.
set d=%str:~-6,-2%
echo %d%
28 星Starting from the left side of the 6th character from the end of %str%, intercept the remaining characters after discarding the last 2 characters of %str%. The value of variable %d% is this.
-------------------------------------------------------------------------------------------------------------------------------
It is inconvenient to remember the above 4 situations. The important thing is to grasp the commonality. The following is my understanding (very unprofessional! Because I am not a computer major):
Observe the above 4 assignment statements and summarize to get the statement
set s=%str:~x,y%
The function is: Intercept characters with length y at offset x of string %str%, and assign to variable s.
Understanding and mastering:
Remember: Left->Right --- positive direction
Right->Left --- negative direction
When x is positive or 0, offset x means the right side of the x-th character along the positive direction.
When x is negative, offset x means the left side of the x-th character along the negative direction.
For example, the offset at -4 of abcdefg is the left side of character d.
When y is positive or 0, intercepting "length" y characters means obtaining y characters along the positive direction.
When y is negative, intercepting "length" y characters means discarding |y| (absolute value of y) characters along the negative direction and getting the remaining characters.
For example, The offset at -4 of abcdefg, intercepting characters with length 2, is the left side of character d, and obtaining 2 characters along the positive direction is "de"
The offset at 2 of abcdefg, intercepting characters with length -4, is the right side of character b, and discarding 4 characters along the negative direction gets the remaining character "c"
In addition:
When y is negative, %str:~y% means obtaining the last |y| characters of %str% (this can be regarded as a short form of %str:~-|y|,|y|%)
When y is positive, %str:~y% means discarding the first y characters of %str% and getting the remaining characters (this is very important, which cannot be expressed by the method of offset + length.)
About the short form:
When one of x or y is 0, 0 can be omitted. For example: %str:~0,3% can be abbreviated as %str:~,3%
When x is positive: %str:~-x,x'% can be abbreviated as %str:~-x% (here x' is any positive number greater than x)
Finally, it should be noted that an unreasonable interception will get an "empty" value.
For example, now execute
set str=abcde
set f=%str:~-2,y%
Obviously, the offset -2 of %str% is the left side of character "d". Since no matter what y is, the obtained string is a subset of the remaining string "de", so to make %f% not empty, reasonable interception is required.
When y takes 0, ±1, 2, etc., %f% is not empty; when y is less than or equal to -2, %f% is empty; when y is greater than or equal to 2, %f% is always "de".
─────────────────────────────────────────────────end
In addition:
Environment variable replacement has the following enhancements:
%PATH:str1=str2%
This will expand the PATH environment variable, replacing each "str1" in the expanded result with "str2".
To effectively remove all "str1" from the expanded result, "str2" can be empty.
"str1" can start with an asterisk; in this case, "str1" will match from the start of the expanded result to the first occurrence of the remaining part of str1.
For example, execute:
set a=123456123456
set b=123456123456
echo %a:2=+%
echo %b:1=%It will display: 1+34561+3456
2345623456
<1>. Very important set command
set has two parameters /a and /p
Typing "set" directly will display the system environment variables and current environment variables.
Typing "set p" will display all variables starting with letter P.
No need to say more about the default parameters. Note two points:
1. Assign empty value:
set "a="
2. Develop a good habit to avoid mistakenly assigning an extra space.
For example:
set str=abc
Add a pair of double quotes:
set "str=abc"
For /a parameter
/A command line switch specifies that the string to the right of the equal sign is an evaluated numeric expression. The evaluator is simple and supports the following operations in decreasing order of priority:
() - Grouping
! ~ - - Unary operators
* / % - Arithmetic operators
+ - - Arithmetic operators
<< >> - Logical shift
& - Bitwise "AND"
^ - Bitwise "XOR"
| - Bitwise "OR"
= *= /= %= += -= - Assignment
&= ^= |= <<= >>=
, - Expression separator
Note the premise:
1. Octal number 0? (0<=?<=7) and hexadecimal number 0x? (0<=?<=15). Those not starting with 0 are decimal numbers.
2. The /a parameter only operates on integers from -(2^31-1) to 2^31 (note that it is the XP version).
This can be tested with code:
@echo off&setlocal enabledelayedexpansion
:Support the maximum number of 1.9950631168807583848837421626836e+3010
set m=1
for /l %%a in (1 1 10000) do (
for /l %%b in (1 1 %%a) do (
set /a m*=2
set /a n+=1
echo !m!=2^^^^!n!
if "!m:~,1!" == "-" echo !m!&set /a mm=!m!-1&echo !mm!=!m!-1 &pause&exit
) )
Unary operators ~ ! -
~ Take complement
Regard -(2^31-1) to 2^31 as a number axis, with "origin O" at the right side of 0 and the left side of -1.
(This number axis can be regarded as a closed number axis at both ends. Adding 1 to 2^31-1 will get -2^31)
In this way, the complement of a binary number (all integers) in the computer can be regarded as finding the "opposite number":
-2^31 ...︺︺︺︺︺︺︺︺︺︺︺︺︺︺︺O︺︺︺︺︺︺︺︺︺︺︺︺︺︺︺... 2^31-1
... -3 -2 -1 0 1 2 3 ...
~ means the opposite number of
For example:
set /a a=~-1%a% is equal to 0
set /a a=~5%a% is equal to -6
! Take NOT
! is 0
! is 1
For example:
set /a a=!2%a% is equal to 0
set /a a=!0%a% is equal to 1
"-" Take negative number
- is consistent with -x in mathematics, but pay attention to the situation when overflow occurs.
For example:
set /a a=-(-2147483648)%a% is -2147483648 instead of 2147483648, because 2147483648 has overflowed:
2147483648=(2^31-1)+1
Arithmetic operators * / % ﹢ -
Arithmetic operators: * / % ﹢ - correspond to
Mathematical symbols: x ÷ mod(remainder) + -
Need to note that "%" is at the command line, while in bat, it should be written as %%
Logical shift: << >>
Note that in batch processing or command line, add a pair of double quotes "" or escape <,> with ^.
set /a "<<"
Means shift the binary number of left by bits
For example:
set /a a=15"<<"1%a% is equal to 30
This is because: 15=bin(00 00000 00000 00000 00000 00000 01111)
Shifting left by 1 bit becomes bin(0 00000 00000 00000 00000 00000 011110)
And bin(0 00000 00000 00000 00000 00000 011110)=2^4+2^3+2^2+2^1=30
After simple mathematical derivation, it can be known that:
"<<"==*2^
">>"==/2^(note overflow)
Similarly, ">>" is right shift, the principle is the same, which is omitted here.
Logical "XOR", "OR", "AND": "^", "|", "&"
Note that in batch processing or command line, add ^ before the operator.
Here: ^ | & correspond to
In discrete mathematics: XOR⊕ Disjunction∨ Conjunction∧
Rules: Conjunction∧(if there is 0, it is 0) Disjunction∨ (if there is 1, it is 1) XOR⊕ (same is 0, different is 1)
For example:
set /a a=15^^5%a% is equal to 10
This is because:
01111
⊕) 00101
─────
=) 01010
And bin(01010)=10
For "|", "&" the principle is the same, which is omitted here.
Assignment operator
"=" needs no explanation.
For *= /= %= += -= &= ^= |= <<= >>
Take "+=" as an example, the same for others.
This is just a shorthand.
For example, the following two lines of code are equivalent:
set /a a+=2
set /a a=a+2
Expression separator ","
This operator can be used to simplify code:
For example:
set /a a=1
set /a b=2
set /a c+=3
It can be abbreviated as:
set /a a=1,b=2,c+=3
Find the decimal part of a fraction
Example: Calculate the 100th decimal place of 7/5:
call:div 7 5 100 ans
@echo off
:div
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Setlocal Enabledelayedexpansion&set/a b=%2,R=%1%%b*10&set "dc="
For /l %%z In (1 1 %3)Do (set/a d=R/b,R=R%%b*10&set dc=!dc!!d!)
endlocal&set "%4=%dc%"
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
exit/bIn addition:
set /a ... and set /p ...
You can omit the space between set and / and write it as
set/a ... and set/p ...
There are many such usages that omit spaces, which will be supplemented later.
Pointer usage of set/a (called pointer for now):
Numerical definition:
@echo off
call:arr arr 1 2 3 4 + d d+ 258 68 944 ddd pp dd
set arr
pause
:arr
set/a n+=1
if %2.==. goto:eof
set %1%n%=%2
shift /2
goto:arrCount the number of occurrences of various characters in a string:
@echo off
set "str=aferefwfwerergrgreaaffwafwa"
set/p= %str% 中有<nul
:loop
set /a %str:~0,1%+=1&set str=%str:~1%&if defined str goto loop
echo a %a% 个
pauseAbout parameter /P
set/p has two usages:
1. Receive keyboard input
@echo off
:begin
SET /p a=Please enter a string:
echo %a%
goto begin
Note that the characters that can be used as variables cannot be constant numbers, but can be letters, Chinese characters, etc., and their strings.
When entering special characters ^&|<>,add ^ in front, otherwise a syntax error occurs.
The statement "SET /p a=Please enter a string:"
Similar to c++:
cout << "Please enter a string:\n";
cin >> a;2. Receive the first line of a file.
This can be understood with the following two codes. %0 means the batch processing itself:
Code 1:
:::::::::::::::::::
@echo off
set /p a=<%0
echo %a%&pause>nul
Code 2:
::::::&color 02
@echo off
set /p a=<%0
echo %a%&pause>nul
3. Special usage of set/p .
Left alignment (note that the tab in the code is):
@echo off&setlocal enabledelayedexpansion
for /l %%i in (1 10 999) do (set/a n+=1&set /p=^%%i <nul
if !n!==5 set n=0&echo.
)
pauseDelay to delete the carriage return character (can also use ping) to display:
@echo off
for /f "tokens=*" %%i in (1.txt) do echo.|set /p=%%i
pauseDelete the carriage return character of each line in 1.txt and output to 2.txt (into one line):
@echo off
for /f "tokens=*" %%i in (1.txt) do set /p=%%i<nul >>2.txt
pauseGenerally, this usage of set /p will be related to | or <nul.
<nul makes the last character of the output not have a newline character, which is very useful.
[ Last edited by plp626 on 2009-4-24 at 05:00 ]
Recent Ratings for This Post
( 12 in total)
Click for details
| Rater | Score | Time |
|---|---|---|
| zjwplp | +1 | 2008-01-29 08:38 |
| Wengier | +6 | 2008-01-29 08:42 |
| wujingyi | +2 | 2008-01-29 18:16 |
| komafd2 | +1 | 2008-02-13 17:41 |
| vkill | +2 | 2008-02-25 01:48 |
| NeverAgain | +2 | 2008-02-27 15:23 |
| 26933062 | +8 | 2008-03-03 01:39 |
| 523066680 | +2 | 2008-03-15 18:12 |
| moniuming | +8 | 2008-11-05 22:22 |
| wangfangjian | +2 | 2009-03-19 09:52 |
| regvip2008 | +2 | 2009-05-27 09:03 |
| 516526966 | +2 | 2010-02-11 21:00 |
