|
plp626
银牌会员
     钻石会员
积分 2278
发帖 1020
注册 2007-11-19
状态 离线
|
 『楼 主』:
CMD【学习】
使用 LLM 解释/回答一下
教程后续不断更新,补充,新手敬请期待
也欢迎你的加入,有新发现plp626愿与你共同探讨,分享.
网志--学习笔记
-------适合于有一定基础者---------
~~~~~~~~~~~~~请大家暂不要发水贴!
可以-----> 提出建议与意见.
可以-----> 批评!
欢迎-----> 指出错误!!!
~~~~~~~~~~~~~谢谢!
声明:我不敢说本帖的代码都是原创但引用时 务必注明 from cn-dos.net
windows,xp下的cmd共有71个内置命令,每个命令都自带了帮助信息,现在先把这些帮助全部导出来放到桌面的cmdhelp目录里
这里给个代码:
@echo off&mode con lines=5 cols=50
md cmdhelp ||(pause&exit)
title 正在导出cmd帮助信息到cmdhelp目录内, 稍等...
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 已完成 按任意键查看. &pause>nul
start notepad ALL.help
goto :eof
:help
for /f %%i in ('help^|findstr "^"') do help %%i>>cmdhelp\%%i.%1
goto :eof
本人还是推荐看英文原版帮助的,这个把英文的版本与中文版本放在一起了,英语差点可以结合着看,
本想做成htm格式,这个我还没想好,论坛里已经有人写过相关代码,搜下借鉴着改*改,就可以了.
( P.S.:17楼ZJHJ已经给出了相关代码,可以参考参考)
下面的帖子是我边学别写的,有收获就添,所以修改是难免的,里面的用法有些可能帮助里没有解释,那这些大都是我从论坛里前辈那总结的,
掌握变量截取%str:~x,y%即:%str%的偏移量为x处,长度为y的字符
~~~~~~~~~plp626于 2008-1-28 ~~~~~~~~~~
start────────────────────────────────────────────────
经常要用到变量的截取,所以这个命令自然得熟练掌握,这里x,y有正有负,总共也就4种情况,分别都是怎么截取呢?
做个小试验,命令提示符下演示结果:
echo off
set str=%date%
echo %str%
2008-01-28 星期一
这个显示结果是说, 变量%str%的值为"2008-01-28 星期一"
set a=%str:~2,4%
echo %a%
08-0
这个说明,从%str%第2个字符右侧,往后截取4个字符, 就是变量%a%的值.下同理
set b=%str:~6,-2%
echo %b%
1-28 星
从%str%第6个字符右侧,往后截取%str%最后2个字符所剩余的字符, 就是变量%b%的值
set c=%str:~-3,2%
echo %c%
星期
从%str%的倒数第3个字符左侧,往后截取2个字符, 就是变量%c%的值
set d=%str:~-6,-2%
echo %d%
28 星
从%str%的倒数第6个字符左侧,往后截取%str%最后2个字符所剩余的字符,就是变量%d%的值
-------------------------------------------------------------------------------------------------------------------------------
上面4种情况记忆是不方便的,重要的是抓住共性,下面是我的理解(很不专业!因为偶不是jsj专业的):
观察上面4个赋值语句并归纳得到语句
set s=%str:~x,y%
的作用就是: 在字符串%str%的偏移量x处,取"长度"为y的字符,然后赋给变量s.
理解与掌握:
记: 左->右 ---正方向
右->左 ---负方向
x为 正或0时,偏移量x处表示沿着 正方向第x个字符的 右侧处.
x为 负时, 偏移量x处表示沿着 负方向第x个字符的 左侧处.
例如 abcdefg的偏移量为-4处就是字符d的左侧处
y为正或0时,取"长度"为y的字符表示沿正方向获取y个字符.
y为负时, 取"长度"为y的字符表示沿负方向舍弃|y|(y的绝对值)个字符所得的剩余字符.
例如 abcdefg的偏移量为-4处,获取长度为2的字符,就是字符d的左侧处,沿正方向获取2个字符即:"de"
abcdefg的偏移量为2处,获取长度为-4的字符,就是字符b的右侧处,沿负方向舍弃4个字符即得到剩余的字符:"c"
另外:
当y为负时, %str:~y%表示获取%str%的后|y|字符(这个可以看做%str:~-|y|,|y|%的简写)
当y为正时, %str:~y%表示舍弃%str%的前y个字符后剩余的字符( 这个很重要,用偏移量+长度的方法不能表示的.)
关于简写:
x或y其中之一为0时,0可省略. 比如: %str:~0,3% 可简写为 %str:~,3%
x为正时 : %str:~-x,x'% 可简写为 %str:~-x% (这里x'是大于x的任意正数)
最后说下,不合理的截取将会得到"空"值.
比如现在执行
set str=abcde
set f=%str:~-2,y%
显然%str%的偏移量-2处为字符"d"的左侧,由于不管y为多少,所获取的字符串都是剩余字符串"de"的子集,所以要想%f%不为空,就要合理截取.
当y取0,±1,2其中之一时%f%不为空;当y小于或等于-2时%f%为空,而当y大于或等于2时%f%恒为"de".
─────────────────────────────────────────────────end
另外:
环境变量替换已有如下增强:
%PATH:str1=str2%
会扩展 PATH 环境变量,用 "str2" 代替扩展结果中的每个 "str1"。
要有效地从扩展结果中删除所有的 "str1","str2" 可以是空的。
"str1" 可以以星号打头;在这种情况下,"str1" 会从扩展结果的
开始到 str1 剩余部分第一次出现的地方,都一直保持相配。
比如执行:
set a=123456123456
set b=123456123456
echo %a:2=+%
echo %b:1=%
将显示: 1+34561+3456
2345623456
<1>.很重要的set 命令
set共两个参数/a 与/p
直接键入"set" 会显示系统化境变量及当前环境变量,
而 键入"set p" 会显示所有以字母 P 打头的变量
缺省参数的情况不用多说,注意两点
1.赋空值:
set "a="
2.养成好的习惯,以免多赋值一个空格出错.
比如:
set str=abc
时加上一对双引号
set "str=abc"
对于/a 参数/A 命令行开关指定等号右边的字符串为被评估的数字表达式。该表达式
评估器很简单并以递减的优先权顺序支持下列操作:
() - 分组
! ~ - - 一元运算符
* / % - 算数运算符
+ - - 算数运算符
<< >> - 逻辑移位
& - 按位“与”
^ - 按位“异”
| - 按位“或”
= *= /= %= += -= - 赋值
&= ^= |= <<= >>=
, - 表达式分隔符
注意前提:
1. 8进制数0?(0<=?<=7)与16进制数0x?(0<=?<=15),首字符不为0者为10进制数.
2. /a参数只对-(2^31-1)至2^31之间整数进行操作(注意是xp版本).
这点可以用代码来测试:
@echo off&setlocal enabledelayedexpansion
:支持最大数为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
) )
一元运算符~ ! -
~取反
将-(2^31-1)至2^31看做一个数轴,"原点O"为0的左侧与-1的右侧
(此数轴可以看做一个首尾封闭的数轴,对2^31-1加1将得到-2^31)
这样可以将计算机里的二进制数(全是整数)的取反看做求"相反数"了:
-2^31 ...︺︺︺︺︺︺︺︺︺︺︺︺︺︺︺O︺︺︺︺︺︺︺︺︺︺︺︺︺︺︺... 2^31-1
... -3 -2 -1 0 1 2 3 ...
~就表示的反数
比如:
set /a a=~-1
%a%就等于0
set /a a=~5
%a%就等于-6
!取非
!为0
!为1
比如:
set /a a=!2
%a%就等于0
set /a a=!0
%a%就等于1
"-"取负数
-与数学上的-x是一致的,只是要注意发生溢出时的状况
比如:
set /a a=-(-2147483648)
%a%为-2147483648而非2147483648,这是因为2147483648已溢出:
2147483648=(2^31-1)+1
算术运算符 * / % ﹢ -
算术运算符: * / % ﹢ - 分别对应
数学符号: x ÷ mod(取余) + -
需要注意的是"%"是在命令行下,而在bat中则要用%%
逻辑移位: << >>
注意在批处理中或命令行下要加上一对双引号""或用^对<,>进行转义.
set /a "<<"
表示对的二进制数左移位
比如:
set /a a=15"<<"1
%a%就等于30
这是因为:15=bin(00 00000 00000 00000 00000 00000 01111)
左移1位就成了bin(0 00000 00000 00000 00000 00000 011110)
而bin(0 00000 00000 00000 00000 00000 011110)=2^4+2^3+2^2+2^1=30
经过简单的数学推导就可以知道:
"<<"==*2^
">>"==/2^(注意溢出)
同理">>"就是右移,原理一样,这里略.
逻辑 "异", "或", "与": "^", "|", "&"
注意在批处理中或命令行下要在运算符前加上^
这里的: ^ | & 对应于
离散数学上的: 异或⊕ 析取∨ 合取∧
规则: 合取∧(有0则0) 析取∨ (有1则1) 异或⊕ (同0反1)
比如:
set /a a=15^^5
%a%就等于10
这是因为:
01111
⊕) 00101
─────
=) 01010
而bin(01010)=10
对于"|", "&"原理一样,这里略.
赋值运算符
"=" 这个不用说了
对于 *= /= %= += -= &= ^= |= <<= >>
拿"+="为例,其他同理.
这只是一种简写
比如下面两行代码等效:
set /a a+=2
set /a a=a+2
表达式分隔符","
这个运算符可以用来简化代码:
比如:
set /a a=1
set /a b=2
set /a c+=3
就可以简写为:
set /a a=1,b=2,c+=3
求分数的小数部分
例计算7/5小数点100位:
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/b
另外:
set /a ... 与 set /p ...
可以省略掉set 与 /之间的空格写成
set/a ... 与 set/p ...
关于这类省略空格的用法,还有不少,后续再补充.
set/a的指针用法(有点像指针姑且这么叫吧):
数值定义:
@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:arr
统计字符串中各种字符出现次数:
@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% 个
pause
关于参数/P
set/p 有两种用法:
1.接收键盘的输入
@echo off
:begin
SET /p a=请输入一个字符串:
echo %a%
goto begin
注意能用来作为变量的字符不能是常量数字,可以是字母,汉字等,及其字符串.
当输入特殊字符^&|<>时,前要加上^,否则出现语法错误.
语句"SET /p a=请输入一个字符串:"
类似c++里的:
cout << "请输入一个字符串:\n";
cin >> a;
2.接收文件的首行.
这个用下面两个代码可以理解%0表示批处理本身:
代码一:
:::::::::::::::::::
@echo off
set /p a=<%0
echo %a%&pause>nul
代码二:
::::::&color 02
@echo off
set /p a=<%0
echo %a%&pause>nul
3, set/p 特殊用法 .
左对齐(注意代码中的是制表符):
@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.
)
pause
删回车符延迟(也可用ping)显示:
@echo off
for /f "tokens=*" %%i in (1.txt) do echo.|set /p=%%i
pause
删除1.txt每行的回车符后输出到2.txt里(拼成一行):
@echo off
for /f "tokens=*" %%i in (1.txt) do set /p=%%i<nul >>2.txt
pause
一般地set /p 的这种用法都会与|或<nul 联系起来.
<nul使得输出的最后一个字符没有换行符,这点很有用.
Last edited by plp626 on 2009-4-24 at 05:00 ]
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-0
This 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/b
In 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:arr
Count 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% 个
pause
About 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.
)
pause
Delay 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
pause
Delete 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
pause
Generally, 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 ]
|
|
2008-1-28 10:01 |
|
|
plp626
银牌会员
     钻石会员
积分 2278
发帖 1020
注册 2007-11-19
状态 离线
|
     『第 2 楼』:
使用 LLM 解释/回答一下
<2].for 命令详解
start────────────────────────────────────────────────
FOR path] %variable IN (set) DO command
1. 参数缺省
for %i in (cn dos 5 2 1 ! ??.* cmd\*) do echo %i
//依次打印字符串:cn,dos,5,2,1,!,"当前路径下与??.*匹配的非隐藏文件全名","当前路径下cmd目录内所有非隐藏文件全名".这里用空格对元素进行分界.
//对于? *这两个字符,for会当作通配符去匹配文件名,而不会显示这两个字符
2. 参数d 是dirctory目录的缩写,作用在于集合里有通配符时匹配目录名而非文件名.
for /d %a in (cn dos 5 2 1 ! * cmd\*) do echo %a
//依次打印cn,dos,5,2,1,!,"当前路径下所有非隐藏目录的目录名" “当前路径下cmd目录内所有非隐藏目录名”
3. 参数r 是road路径的缩写,作用在于它将会给集合里的元素加上指定路径下的目录树(请注意目录树包含了隐藏目录,子母录)再打印
for /r %windir% %i in (plp626 . *) do echo %i
//依次打印"%windir%目录树\plp626" "%windir%目录树\." "%windir%目录树\相应的文件全名(注意相应目录内的文件不包含隐藏属性的!)"
//当%windir%缺省时,默认为当前路径.
4. 参数l 是ladder阶梯的缩写,作用就是按照 "起步 步长 结束"的递进方式来循环,这三个元素必须是自然数,当步长为0时,将成为死循环.
for /l %a in (-10 1 10) do echo %a
//依次显示 -10 -9 ... 10 这句表示 开始之为-10 以步长为1递增,当递增到10时循环结束
5. 参数 f 这个很要紧,认真看,f是file文件的缩写,和其他参数不同,这一个参数就有3种用法,个个神通广大:
5.1 集合的元素是文件名
for /f "eol=; skip=3 tokens=2,3* delims=, " %a in (test_1.txt test_2.txt) do echo %a %b %c
//会依次分析 test_1.txt test_2.txt 中的每一行,忽略以分号打头(eolith原始的,开头)的那些行,跳过(skip)前3行
以","为界定符(delimit划界),显示第2个,第3个,和剩余的字符串(token记号).
5.2 集合的元素是外部命令的输出结果
for /f "delims=" %a in ('more test.txt') do echo %a
//取消默认的分割符(空格)对more test.txt打印的结果进行逐行分析,空格也会成为有效字符串.
for /f "tokens=*" %a in ('more test.txt') do echo %a
//对more test.txt打印的结果进行逐行分析,由于此时默认的分割符为空格,行前的空格字符将不会显示.
//请注意括号里这对单引号,它有执行的意思,将执行对象的输出作为"文件"分析,这就决定了对象的输出必须是写入内存的,如果它没有这样,比如将more test.txt改为start test.txt或着test.txt它将打开test.txt,而%a由于没有接收到回车符这个命令,....
//两外,这个外部命令还支持复合句,比如:
for /f %%a in ('if exist 1.txt ^(echo ++^) else echo -- ')do echo %%a
5.3 集合的元素是字符串
for /f "tokens=*" %a in (" cn dos 帅 !") do echo %a
for /f "delims=" %a in (" cn dos 帅 !") do echo %a
//这两句都会显示cn dos 帅 !但不同的是前者将cn前的空格略去,后者原样打印.
对于5.1如果有个test 3.txt(文件名为test 3)即文件名含空格,得用双引号将它括起来以表示文件名正确解释.
但是这样就成立5.3的字符串用法,为了解释器能正确解释,它是个文件名,而非字符串,得用关键字"usebakeq",
下面这句显示文件test 3.txt的相关内容(默认分割符为空格)解释器会把test 3.txt当作文件名,而非字符串.
for /f "usebackq" %i IN ("test 3.txt") DO echo %i
有时要对移动硬盘或u盘进行判断,用这个命令
wmic logicaldisk get name,description,drivetype
6. for 变量的扩充
以C:\Documents and Settings\pp365\桌面\ 路径下的 "test.bat"为例,对for 语句的变量测试如下
(这里是用%0为例,在for语句中可以用相应的变量代换0):
用百分号0得到本批处理文件的带双引号的自身完整路径:
"C:\Documents and Settings\pp365\桌面\test.bat"
用 百分号~0
打印结果: C:\Documents and Settings\pp365\桌面\test.bat
用 百分号~f0
打印结果: C:\Documents and Settings\pp365\桌面\test.bat
用 百分号~d0
打印结果: C:
用 百分号~p0
打印结果: \Documents and Settings\pp365\桌面\
用 百分号~n0
打印结果: test
用 百分号~x0
打印结果: .bat
用 百分号~s0
打印结果: C:\DOCUME~1\pp365\桌面\test.bat
用 百分号~a0
打印结果: --a------
用 百分号~t0
打印结果: 2008-03-05 15:51
用 百分号~z0
打印结果: 947
用 百分号~$PATH:0
打印结果: C:\Documents and Settings\pp365\桌面\test.bat
----------------可以用组合修饰符来得到多重结果:
用 百分号~dp0
打印结果: C:\Documents and Settings\pp365\桌面\
用 百分号~nx0
打印结果: test.bat
用 百分号~fs0
打印结果: C:\DOCUME~1\pp365\桌面\test.bat
用 百分号~dp$PATH:0
打印结果: C:\Documents and Settings\pp365\桌面\
用 百分号~ftza0
打印结果: --a------ 2008-03-05 15:51 947 C:\Documents and Settings\pp365\桌面\test.bat ──────────────────────────────────────────────end
Last edited by plp626 on 2008-3-23 at 10:53 PM ]
<2].for CommandDetailExplanation
start────────────────────────────────────────────────
FOR path] %variable IN (set) DO command
1. Default Parameters
for %i in (cn dos 5 2 1 ! ??.* cmd\*) do echo %i
//Print the strings in turn: cn, dos, 5, 2, 1, "The full name of non-hidden files matching ??.* in the current path", "The full name of non-hidden files in the cmd directory in the current path". The elements are delimited by spaces here.
//For the characters? * these two, for will match the file name as a wildcard, and will not display these two characters
2. Parameter d is the abbreviation of directory. The function is that when there are wildcards in the set, it matches the directory name instead of the file name.
for /d %a in (cn dos 5 2 1 ! * cmd\*) do echo %a
//Print cn, dos, 5, 2, 1, !, "The directory name of all non-hidden directories in the current path", "The directory name of all non-hidden directories in the cmd directory in the current path" in turn
3. Parameter r is the abbreviation of road. The function is that it will add the directory tree (note that the directory tree includes hidden directories and subdirectories) of the specified path to the elements in the set and then print
for /r %windir% %i in (plp626 . *) do echo %i
//Print in turn "%directory tree of %windir%\plp626" "%directory tree of %windir%\." "The full name of the corresponding file in the directory tree of %windir% (note that the files in the corresponding directory do not have the hidden attribute!)"
//When %windir% is omitted, it defaults to the current path.
4. Parameter l is the abbreviation of ladder. The function is to loop in the way of "start step end" progression. These three elements must be natural numbers. When the step is 0, it will become an infinite loop.
for /l %a in (-10 1 10) do echo %a
//Display -10 -9...10 in turn. This sentence means that the starting value is -10, increasing by 1 step, and the loop ends when it increases to 10
5. Parameter f This is very important. Look carefully. f is the abbreviation of file. Different from other parameters, this one parameter has 3 usages, each of which is very powerful:
5.1 The elements of the set are file names
for /f "eol=; skip=3 tokens=2,3* delims=, " %a in (test_1.txt test_2.txt) do echo %a %b %c
//It will analyze each line in test_1.txt test_2.txt in turn, ignore those lines starting with semicolon (eolith original, starting), skip (skip) the first 3 lines
Use "," as the delimiter (delimit), and display the 2nd, 3rd, and remaining strings (token marks).
5.2 The elements of the set are the output results of external commands
for /f "delims=" %a in ('more test.txt') do echo %a
//Cancel the default delimiter (space) to analyze the results printed by more test.txt line by line, and spaces will also become valid strings.
for /f "tokens=*" %a in ('more test.txt') do echo %a
//Analyze the results printed by more test.txt line by line. Since the default delimiter is space at this time, the space characters at the beginning of the line will not be displayed.
//Please pay attention to this pair of single quotes in the parentheses. It has the meaning of execution. The output of the execution object is used as a "file" for analysis, which determines that the output of the object must be written to memory. If it is not like this, for example, change more test.txt to start test.txt or test.txt, it will open test.txt, and %a does not receive the return character command,....
//In addition, this external command also supports compound sentences, such as:
for /f %%a in ('if exist 1.txt ^(echo ++^) else echo -- ')do echo %%a
5.3 The elements of the set are strings
for /f "tokens=*" %a in (" cn dos 帅 !") do echo %a
for /f "delims=" %a in (" cn dos 帅 !") do echo %a
//Both of these two sentences will display cn dos 帅 ! but the difference is that the former skips the space before cn, and the latter prints it as it is.
For 5.1, if there is a test 3.txt (the file name is test 3), that is, the file name contains spaces, you need to enclose it with double quotes to indicate that the file name is correctly interpreted.
But this becomes the string usage of 5.3. For the interpreter to interpret correctly, it is a file name, not a string. You need to use the keyword "usebakeq".
The following sentence displays the relevant content of the file test 3.txt (the default delimiter is space), and the interpreter will treat test 3.txt as a file name, not a string.
for /f "usebackq" %i IN ("test 3.txt") DO echo %i
Sometimes you need to judge the mobile hard disk or USB flash drive, use this command
wmic logicaldisk get name,description,drivetype
6. Expansion of for Variables
Take "test.bat" in the path C:\Documents and Settings\pp365\Desktop\ as an example, and test the for statement variables as follows
(Here, %0 is used as an example, and the corresponding variable can be substituted for 0 in the for statement):
Get the full path of this batch file with double quotes using percent 0:
"C:\Documents and Settings\pp365\Desktop\test.bat"
Use percent ~0
Print result: C:\Documents and Settings\pp365\Desktop\test.bat
Use percent ~f0
Print result: C:\Documents and Settings\pp365\Desktop\test.bat
Use percent ~d0
Print result: C:
Use percent ~p0
Print result: \Documents and Settings\pp365\Desktop\
Use percent ~n0
Print result: test
Use percent ~x0
Print result: .bat
Use percent ~s0
Print result: C:\DOCUME~1\pp365\Desktop\test.bat
Use percent ~a0
Print result: --a------
Use percent ~t0
Print result: 2008-03-05 15:51
Use percent ~z0
Print result: 947
Use percent ~$PATH:0
Print result: C:\Documents and Settings\pp365\Desktop\test.bat
---------------- Multiple results can be obtained using combined modifiers:
Use percent ~dp0
Print result: C:\Documents and Settings\pp365\Desktop\
Use percent ~nx0
Print result: test.bat
Use percent ~fs0
Print result: C:\DOCUME~1\pp365\Desktop\test.bat
Use percent ~dp$PATH:0
Print result: C:\Documents and Settings\pp365\Desktop\
Use percent ~ftza0
Print result: --a------ 2008-03-05 15:51 947 C:\Documents and Settings\pp365\Desktop\test.bat ──────────────────────────────────────────────end
Last edited by plp626 on 2008-3-23 at 10:53 PM ]
|
|
2008-1-28 10:05 |
|
|
plp626
银牌会员
     钻石会员
积分 2278
发帖 1020
注册 2007-11-19
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
<3>.熟练掌握 CALL start
call------单进程”传递“
start-----开启一个新的进程。
用call对变量进行截取:
call 具有二次预处理功能,这源于它的函数调用功能(个人愚见) .
由于%v:~%x%,%y%%会对%预处理,比如直接%v:~%x%,%y%%对最外边的一对%留下,这让我们想到用call再进一步进行预处理读取.
@echo off
::在命令行下不能正确显示
set a=12345678901234567890
set /p b=输入两个正整数(不大于9)用空格隔开:
call echo %%a:~%b:~,1%,%b:~-1%%%
pause
call :标签 "参数1" "参数2" ...
从:开始到首次出现的空格,当作标签的结束符.(标签可作为%0参数,即标签前与后的空格将被视作参数分隔符,)
call :^^^.... 将会是个空语句.
主标签中&(非^&)后的字符被当作语句执行.执行完后再跳转到副标签处.
副标签中含|&><:5个字符其中之一时,后面的字符被当作注释,将不进行任何解释.
标签字符不能为单独的<space> &()^=;%+,:|但以下特殊字符作为的标签是合法的:
`, ' ,^^ , ^^& , "<space>" , " , "", @,,{,},?,/,\,*,-,$,#,~,.,
当启用延迟环境变量时,!不能作为标签.
汉字,与其他扩展字符,以及混合字符也可以作为标签.
虽然不提倡用这些特殊字符作为标签,但是它们可以实现一些一般方法难以实现的脚本.
参数最多9个,再多就要借助"shift"命令(见后面)
call 在跳转的标签时遵循下面规则:
1, 大小写不区分,
2, 先在call 语句后面找,后面没有再在前面找.
3, 多个相同标签时只执行找到的第一个标签.
将这三点归纳成一句话就是:
,大小写不分,先后再前,执行一次.
另外
A.bat文件
@echo off
call %*
goto :eof
:a
echo a任务 %*
goto :eof
...
若想要执行a任务,B.bat文件里可以这样调用(若是命令行下可以省去call)
@echo off
call A.bat :a 1 2 5
pause
这里在非命令行下运行B.bat时,B.bat内那个call是必须的,若是在命令行下直接B.bat调用则B.bat内那个call则可以省去l
模板:
@echo off & setlocal ENABLEEXTENSIONS
set x=2
set y=3
call :Area %x% %y% answer
echo/The area is: %answer%
pause
goto :EOF
:Area %width% %height% result
setlocal
set /a res=%1*%2
endlocal & set "%3=%res%"
goto :EOF
Last edited by plp626 on 2008-4-16 at 11:32 AM ]
<3>. Skilled in mastering CALL start
call------"Pass" in a single process
start-----Start a new process.
Using call to intercept variables:
call has a secondary preprocessing function, which originates from its function call function (personal humble opinion).
Because %v:~%x%,%y%% will preprocess %, for example, directly %v:~%x%,%y%% leaves the outermost pair of %, which makes us think of using call to further preprocess and read.
@echo off
::Cannot be displayed correctly in the command line
set a=12345678901234567890
set /p b=Enter two positive integers (not greater than 9) separated by spaces:
call echo %%a:~%b:~,1%,%b:~-1%%%
pause
call :label "parameter 1" "parameter 2" ...
From : to the first space encountered, regarded as the end of the label. (The label can be used as the %0 parameter, that is, the spaces before and after the label will be regarded as parameter separators.)
call :^^^.... will be an empty statement.
The characters after & (not ^&) in the main label are executed as statements. After execution, it jumps to the sub-label.
If one of the five characters &|><: is contained in the sub-label, the characters after it are regarded as comments and will not be explained.
The label characters cannot be a single <space> &()^=;%+,:|, but the following special characters as labels are legal:
`, ' ,^^ , ^^& , "<space>" , " , "", @,,{,},?,/,\,*,-,$,#,~,.,
When delayed environment variables are enabled,! cannot be used as a label.
Chinese characters, other extended characters, and mixed characters can also be used as labels.
Although it is not recommended to use these special characters as labels, they can implement some scripts that are difficult to implement by general methods.
There are up to 9 parameters. If there are more, you need to use the "shift" command (see later)
call follows the following rules when jumping to the label:
1, Case-insensitive,
2, Look for it after the call statement first, and look for it before if not found later.
3, Only the first label found is executed when there are multiple identical labels.
Summarize these three points into one sentence:
, case-insensitive, first later then before, execute once.
In addition
A.bat file
@echo off
call %*
goto :eof
:a
echo a task %*
goto :eof
...
If you want to execute task a, you can call it like this in B.bat file (if it is in the command line, you can omit call)
@echo off
call A.bat :a 1 2 5
pause
Here, when running B.bat under non-command line, the call in B.bat is necessary. If it is called directly in the command line as B.bat, the call in B.bat can be omitted.
Template:
@echo off & setlocal ENABLEEXTENSIONS
set x=2
set y=3
call :Area %x% %y% answer
echo/The area is: %answer%
pause
goto :EOF
:Area %width% %height% result
setlocal
set /a res=%1*%2
endlocal & set "%3=%res%"
goto :EOF
Last edited by plp626 on 2008-4-16 at 11:32 AM ]
|
|
2008-1-28 12:49 |
|
|
plp626
银牌会员
     钻石会员
积分 2278
发帖 1020
注册 2007-11-19
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
<4>. if ... else ...用法
if 条件? command A
if 条件? (command A) else command B
EQU - 等于
NEQ - 不等于
LSS - 小于
LEQ - 小于或等于
GTR - 大于
GEQ - 大于或等于
==与equ不同,当command A为赋值语句时条件?若是?==?则需用?equ?
注意复合语句内的变量赋值.
Last edited by plp626 on 2008-3-14 at 03:05 PM ]
<4. Usage of if...else...
if condition? command A
if condition? (command A) else command B
EQU - equals
NEQ - not equals
LSS - less than
LEQ - less than or equal to
GTR - greater than
GEQ - greater than or equal to
== is different from equ. When command A is an assignment statement and the condition? is?==?, then equ should be used.
Note the variable assignment within the compound statement.
Last edited by plp626 on 2008-3-14 at 03:05 PM ]
|
|
2008-1-29 08:20 |
|
|
plp626
银牌会员
     钻石会员
积分 2278
发帖 1020
注册 2007-11-19
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
<5>.查找命令find & findstr
find
Searches for a text string in a file or files.
FIND ] "string" filename]
/V Displays all lines NOT containing the specified string.
/C Displays only the count of lines containing the string.
/N Displays line numbers with the displayed lines.
/I Ignores the case of characters when searching for the string.
/OFF Do not skip files with offline attribute set.
"string" Specifies the text string to find.
filename
Specifies a file or files to search.
If a path is not specified, FIND searches the text typed at the prompt
or piped from another command.
如果没有指定路径,FIND 将搜索键入的或者由另一命令产生的文字。
使用find "?" 文件
查找文件后到结果总会在首行来个(表示下面的内容是来自*.*这个文件)
---------- *.*
当/v /c 参数连用时是(n为整数)
---------- *.*: n
现在来说参数的用法:
/v参数 不含"str"的行 如帮助所说,显示不包含指定字符串的行
find /v "?" ... 显示不包含?的行
但是常用find /v "" ...
它是打印文件所有内容,因为文件不会包含空字符.
/c 参数count 行数不会显示内容,只显示包含搜索字符串的行数,
这里要理解find在分析一个文件时它将回车换行符作为标志来认定为一行的结束与下行的开始,
find /c "?"... 不显示含?的行,但显示含?的行数,
find /c /v "?" ... 不显示行的内荣, 显示不包含?的行的行数,
find /c /v "" ... 仅显示不含空字符的行的行数,即文件的总行数.
/n参数 nember of lines若显示行的内容则给行首带上行号
/n总是与/v连用时才能有效,与/c参数连用是无效.
/i 参数Ignores 对?忽略大小写.
若"string"含有空格,则空格不会被解释为逻辑或,而是有效字符串
find 参数 "?" *.txt *.c在当前路径的所有txt文件里进行查找(文件也可以枚举的格式:1.txt 2.txt 3.txt)
常用格式:
find /c "?" *.txt *.bat
find /v "?" *.txt *.bat
find /n "?" 1.txt
find /c /v "" *.*
findstr
FINDSTR
]
strings filename]
/B Matches pattern if at the beginning of a line.
/E Matches pattern if at the end of a line.
/L Uses search strings literally.
/R Uses search strings as regular expressions.
/S Searches for matching files in the current directory and all
subdirectories.
/I Specifies that the search is not to be case-sensitive.
/X Prints lines that match exactly.
/V Prints only lines that do not contain a match.
/N Prints the line number before each line that matches.
/M Prints only the filename if a file contains a match.
/O Prints character offset before each matching line.
/P Skip files with non-printable characters.
/OFF Do not skip files with offline attribute set.
/A:attr Specifies color attribute with two hex digits. See "color /?"
/F:file Reads file list from the specified file(/ stands for console).
/C:string Uses specified string as a literal search string.
/G:file Gets search strings from the specified file(/ stands for console).
/D:dir Search a semicolon delimited list of directories
strings Text to be searched for.
filename
Specifies a file or files to search.
Use spaces to separate multiple search strings unless the argument is prefixed
with /C. For example, 'FINDSTR "hello there" x.y' searches for "hello" or
"there" in file x.y. 'FINDSTR /C:"hello there" x.y' searches for
"hello there" in file x.y.
Regular expression quick reference:
. Wildcard: any character
* Repeat: zero or more occurances of previous character or class
^ Line position: beginning of line
$ Line position: end of line
Character class: any one character in set
Inverse class: any one character not in set
Range: any characters within the specified range
\x Escape: literal use of metacharacter x
\<xyz Word position: beginning of word
xyz\> Word position: end of word
For full information on FINDSTR regular expressions refer to the online Command
Reference.
最多搜索字符串字节数为127
一般表达式举例:
1.
findstr . 2.txt 或 findstr "." 2.txt
从文件2.txt中查找任意字符(包括空格),不包括空字符或空行(过滤空行),此用法对unicode的字符也有效,下同.
2.
findstr .* 2.txt 或 findstr ".*" 2.txt
从文件2.txt中查找任意字符包括空行(原样打印)
3.
findstr "" 2.txt
从文件2.txt中查找包括数字0-9的字符串或行
4.
findstr "" 2.txt
从文件2.txt中查找包括任意字符(52个英文字母)的字符串或行
5.
findstr "" 2.txt
从文件2.txt中查找包括a b c e z y字母的字符串或行
6.
findstr "" 2.txt
从文件2.txt中查找小写字符a-f l-z的字符串,但不包含g h I j k这几个字母。
7.
findstr "MY" 2.txt
从文件2.txt中可以匹配 MahY , MbiY, MahY等…..
8.
^和$符号的应用
^ 表示行首,"^step"仅匹配 "step hello world"中的第一个单词
$ 表示行尾,"step$"仅匹配 "hello world step"中最后一个单词
9.
finstr "" 2.txt
任何不在字符集0-9中的字符,即存在字符不属于0-9就打印,
比如dfd41210 的对方的45 等 就会打印;而54545158将不被打印.
10.
findstr "" 2.txt
任何不在字符集a-z中的字符,即存在字符不属于a-z就打印.
比如adfdfd2225 dfdfdfdfd大富大贵 等将被打印;而dfdfdffd不被打印
11.
*号的作用
前面已经说过了 ".*"表示搜索的条件是任意字符,*号在正则表达式中的作用不是任何字符,而是表示左侧字符或者表达式的重复次数,*号表示重复的次数为零次或者多次。
12.
findstr "^*$" 2.txt
这个是匹配找到的纯数字,^ 是代表开头 代表数字 * 代表重复0或多次 $ 代表结尾.
例如 234234234234,如果是2133234kkjl234就被过滤掉了。但是单个的数字不匹配.
Findstr "^*$" 2.txt
这个是匹配找到的纯字母,例如 sdfsdfsdfsdf,如果是213sldjfkljsdlk就被过滤掉了
如果在搜索条件里没有*号,也就是说不重复左侧的搜索条件,也就是 那只能匹配字符串的第一个字符也只有这一个字符,因为有行首和行尾的限制,"^$"第一个字符如果是数字就匹配,如果不是就过滤掉,如果字符串是 9 就匹配,如果是98或者9j之类的就不可以了。
13.
"\<…\>"这个表达式的作用
这个表示精确查找一个字符串,\<sss 表示字的开始位置,sss\>表示字的结束位置
echo hello world computer|findstr "\<computer\>"这样的形式
echo hello worldcomputer|findstr "\<computer\>" 这样的形式就不成了,他要找的是 "computer"这个字符串,所以不可以。
echo hello worldcomputer|findstr ".*computer\>"这样就可以匹配了
14. "."表示任意字符.
findstr /rc:" 01...... " test.txt>>a1.txt
findstr /rc:" 02...... " test.txt>>a2.txt
15.
查找英文状态下的句号"."
在"."的前面加转义字符"\"
set abc=abc de.f
echo %abc%|findstr /c:"\."
16. 查找"\"
set abc=abc E:\123 1111
echo %abc%|findstr "E:\\"
17. /g用法
将1.txt的每行作为搜索字符串,在2.txt中搜索.(如果1.txt的某行含有空格也视为有效字符,不解释为"或")
findstr /g:1.txt 2.txt
加上/b/e 就是行首行尾完全匹配
findstr /b /e /g:1.t 2.t(或findstr /beg:1.t 2.t)
18. /o参数
((echo.%str%&echo. )|findstr /o .)|findstr /c:" "
范例:
findstr . test.txt
过滤空行打印
findstr .* test.txt
原样打印,这个不同与find /v "" 后者会给结果前加上 ---------- 目标文件全名
findstr . test.txt|findstr /v /r /c:"^ * $"
过滤空行,纯空格行打印
-------------
有一A.TXT文件,在其中找到JKLJHLL时,删除含有JKLJHLL字符串的行(不分大小写).
findstr /ivc:"JKLJHLL" a.txt >b.txt
Last edited by plp626 on 2008-4-27 at 01:55 AM ]
### <5>. Finding Commands find & findstr
#### find
Searches for a text string in a file or files.
FIND ] "string" filename]
/V Displays all lines NOT containing the specified string.
/C Displays only the count of lines containing the string.
/N Displays line numbers with the displayed lines.
/I Ignores the case of characters when searching for the string.
/OFF Do not skip files with offline attribute set.
"string" Specifies the text string to find.
filename
Specifies a file or files to search.
If a path is not specified, FIND searches the text typed at the prompt
or piped from another command.
If no path is specified, FIND searches the text typed or piped from another command.
When using find "?" file, the result will always have a line at the beginning like (indicating that the following content is from *.* this file)
---------- *.*
When the /v and /c parameters are used together, it is (n is an integer)
---------- *.*: n
Now, let's talk about the usage of parameters:
/v parameter: lines not containing "str" As the help says, it displays lines that do not contain the specified string.
find /v "?" ... displays lines that do not contain "?".
But commonly used is find /v "" ...
It prints all the content of the file because the file does not contain empty characters.
/c parameter: count the number of lines It does not display the content, only displays the number of lines containing the search string.
Here, it is necessary to understand that when find analyzes a file, it takes the carriage return and line feed characters as markers to determine the end of one line and the start of the next line.
find /c "?"... does not display lines containing "?", but displays the number of lines containing "?".
find /c /v "?" ... does not display the content of the lines, but displays the number of lines that do not contain "?".
find /c /v "" ... only displays the number of lines that do not contain empty characters, that is, the total number of lines in the file.
/n parameter: number of lines If it displays the content of the lines, it adds the line number at the beginning of the line.
The /n is always effective only when used together with /v, and is invalid when used together with the /c parameter.
/i parameter: Ignores case for "?"
If "string" contains spaces, the spaces will not be interpreted as logical OR, but as a valid string.
find parameter "?" *.txt *.c searches in all txt files in the current path according to (files can also be in enumerated format: 1.txt 2.txt 3.txt)
Common formats:
find /c "?" *.txt *.bat
find /v "?" *.txt *.bat
find /n "?" 1.txt
find /c /v "" *.*
#### findstr
FINDSTR
]
strings filename]
/B Matches pattern if at the beginning of a line.
/E Matches pattern if at the end of a line.
/L Uses search strings literally.
/R Uses search strings as regular expressions.
/S Searches for matching files in the current directory and all
subdirectories.
/I Specifies that the search is not to be case-sensitive.
/X Prints lines that match exactly.
/V Prints only lines that do not contain a match.
/N Prints the line number before each line that matches.
/M Prints only the filename if a file contains a match.
/O Prints character offset before each matching line.
/P Skip files with non-printable characters.
/OFF Do not skip files with offline attribute set.
/A:attr Specifies color attribute with two hex digits. See "color /?"
/F:file Reads file list from the specified file(/ stands for console).
/C:string Uses specified string as a literal search string.
/G:file Gets search strings from the specified file(/ stands for console).
/D:dir Search a semicolon delimited list of directories
strings Text to be searched for.
filename
Specifies a file or files to search.
Use spaces to separate multiple search strings unless the argument is prefixed
with /C. For example, 'FINDSTR "hello there" x.y' searches for "hello" or
"there" in file x.y. 'FINDSTR /C:"hello there" x.y' searches for
"hello there" in file x.y.
Regular expression quick reference:
. Wildcard: any character
* Repeat: zero or more occurances of previous character or class
^ Line position: beginning of line
$ Line position: end of line
Character class: any one character in set
Inverse class: any one character not in set
Range: any characters within the specified range
\x Escape: literal use of metacharacter x
\<xyz Word position: beginning of word
xyz\> Word position: end of word
For full information on FINDSTR regular expressions refer to the online Command
Reference.
The maximum number of bytes for the search string is 127.
Examples of general expressions:
1.
findstr . 2.txt or findstr "." 2.txt
Find any characters (including spaces) from file 2.txt, excluding empty characters or empty lines (filters empty lines). This usage is also valid for Unicode characters, the same below.
2.
findstr .* 2.txt or findstr ".*" 2.txt
Find any characters including empty lines from file 2.txt (prints as is).
3.
findstr "" 2.txt
Find strings or lines including numbers 0-9 from file 2.txt.
4.
findstr "" 2.txt
Find strings or lines including any of the 52 English letters from file 2.txt.
5.
findstr "" 2.txt
Find strings or lines including letters a, b, c, e, z, y from file 2.txt.
6.
findstr "" 2.txt
Find strings including lowercase characters a-f and l-z from file 2.txt, but not including letters g, h, I, j, k.
7.
findstr "MY" 2.txt
Can match MahY, MbiY, MahY, etc. in file 2.txt.
8.
Application of ^ and $ symbols
^ means the beginning of a line, "^step" only matches the first word in "step hello world".
$ means the end of a line, "step$" only matches the last word in "hello world step".
9.
finstr "" 2.txt
Any character not in the character set 0-9, that is, if there is a character not belonging to 0-9, it is printed.
For example, dfd41210's other 45, etc. will be printed; while 54545158 will not be printed.
10.
findstr "" 2.txt
Any character not in the character set a-z, that is, if there is a character not belonging to a-z, it is printed.
For example, adfdfd2225 dfdfdfdfd 大富大贵, etc. will be printed; while dfdfdffd will not be printed.
11.
The role of the * sign
As mentioned earlier, ".*" means the search condition is any character. The * sign in the regular expression does not mean any character, but means the number of repetitions of the previous character or expression. The * sign means zero or more repetitions.
12.
findstr "^*$" 2.txt
This matches pure numbers. ^ represents the beginning, represents numbers, * represents zero or more repetitions, and $ represents the end.
For example, 234234234234. If it is 2133234kkjl234, it will be filtered out. But a single number does not match.
Findstr "^*$" 2.txt
This matches pure letters. For example, sdfsdfsdfsdf. If it is 213sldjfkljsdlk, it will be filtered out.
If there is no * sign in the search condition, that is, the search condition is not repeated, that is, , it can only match the first character of the string, and only this one character. Because of the restrictions of the beginning and end of the line, "^$" will match if the first character is a number, and filter out if it is not. If the string is 9, it matches; if it is 98 or 9j, etc., it does not.
13.
The role of the "\<…\>" expression
This means to accurately find a string. \<sss means the start position of the word, and sss\> means the end position of the word.
echo hello world computer|findstr "\<computer\>" in this form.
echo hello worldcomputer|findstr "\<computer\>" in this form does not work. It is looking for the "computer" string, so it is not possible.
echo hello worldcomputer|findstr ".*computer\>" can match in this way.
14. "." means any character.
findstr /rc:" 01...... " test.txt>>a1.txt
findstr /rc:" 02...... " test.txt>>a2.txt
15.
Find the period "." in English state.
Add the escape character "\" in front of ".".
set abc=abc de.f
echo %abc%|findstr /c:"\."
16. Find "\"
set abc=abc E:\123 1111
echo %abc%|findstr "E:\\"
17. /g usage
Take each line of 1.txt as the search string and search in 2.txt. (If a line in 1.txt contains spaces, it is also regarded as a valid character and not interpreted as "or".)
findstr /g:1.txt 2.txt
Adding /b/e means exact match at the beginning and end of the line.
findstr /b /e /g:1.t 2.t (or findstr /beg:1.t 2.t)
18. /o parameter
((echo.%str%&echo. )|findstr /o .)|findstr /c:" "
Example:
findstr . test.txt
Filter empty lines and print.
findstr .* test.txt
Print as is. This is different from find /v "", the latter will add ---------- target file full name at the beginning of the result.
findstr . test.txt|findstr /v /r /c:"^ * $"
Filter empty lines and pure space lines and print.
-------------
There is a file A.TXT. When JKLJHLL is found in it, delete the lines containing the JKLJHLL string (case-insensitive).
findstr /ivc:"JKLJHLL" a.txt >b.txt
Last edited by plp626 on 2008-4-27 at 01:55 AM ]
|
|
2008-1-29 08:21 |
|
|
plp626
银牌会员
     钻石会员
积分 2278
发帖 1020
注册 2007-11-19
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
<6>.查看命令 echo type more sort
Last edited by plp626 on 2008-3-3 at 11:15 PM ]
<6>.Check commands echo type more sort
Last edited by plp626 on 2008-3-3 at 11:15 PM ]
|
|
2008-1-29 08:21 |
|
|
plp626
银牌会员
     钻石会员
积分 2278
发帖 1020
注册 2007-11-19
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
<7>.访问命令 dir cd
先说dir(directory)
Displays a list of files and subdirectories in a directory.
DIR attributes]]
sortorder]] timefield]]
Specifies drive, directory, and/or files to list.
/A Displays files with specified attributes.
attributes D Directories R Read-only files
H Hidden files A Files ready for archiving
S System files - Prefix meaning not
/B Uses bare format (no heading information or summary).
/C Display the thousand separator in file sizes. This is the
default. Use /-C to disable display of separator.
/D Same as wide but files are list sorted by column.
/L Uses lowercase.
/N New long list format where filenames are on the far right.
/O List by files in sorted order.
sortorder N By name (alphabetic) S By size (smallest first)
E By extension (alphabetic) D By date/time (oldest first)
G Group directories first - Prefix to reverse order
/P Pauses after each screenful of information.
/Q Display the owner of the file.
/S Displays files in specified directory and all subdirectories.
/T Controls which time field displayed or used for sorting
timefield C Creation
A Last Access
W Last Written
/W Uses wide list format.
/X This displays the short names generated for non-8dot3 file
names. The format is that of /N with the short name inserted
before the long name. If no short name is present, blanks are
displayed in its place.
/4 Displays four-digit years
Switches may be preset in the DIRCMD environment variable. Override
preset switches by prefixing any switch with - (hyphen)--for example, /-W.其中/a,/o,/t还各自有参数(只能选其一),
/a attribute(属性)
共5个参数:d(directorys),r(read-only),h(hidden),a(archiving),s(system)及否定开关"-"
/o order(顺序)
共5个参数:n(name), s(size), e(extension), d(date), g(group directorys)及一个"反序"开关"-"
/o:n
名称优先级(高者在前)一般为:空格!()-,._`+12...9aAbB...zZ 汉字转换为拼音首字母ab...z
/os
文件字节数:小者优先
/oe
扩展名优先级:空(无扩展名) 字符 数字 字母 汉字 的顺序
/od
时间先着优先
/og
优先级待定
/tc
创建日期
/ta
访问日期
/tw
写入日期
/t time(时间)
共3个参数:c(creation) a(last access) w(written)
另外:
dir |dir .|dir .\ /* 当前路径,文件,目录
dir ..|dir ..\ /* 上一路径下,...
dir \ /* 根目录下...
-----------------------------------------------------
cd .. 进入上级目录
cd\ 进入根目录
cd 显示当前目录
Last edited by plp626 on 2008-3-3 at 06:09 PM ]
### <7>. Access Commands dir cd
First, talk about dir (directory)
Displays a list of files and subdirectories in a directory.
DIR attributes]]
sortorder]] timefield]]
Specifies drive, directory, and/or files to list.
/A Displays files with specified attributes.
attributes D Directories R Read-only files
H Hidden files A Files ready for archiving
S System files - Prefix meaning not
/B Uses bare format (no heading information or summary).
/C Display the thousand separator in file sizes. This is the
default. Use /-C to disable display of separator.
/D Same as wide but files are list sorted by column.
/L Uses lowercase.
/N New long list format where filenames are on the far right.
/O List by files in sorted order.
sortorder N By name (alphabetic) S By size (smallest first)
E By extension (alphabetic) D By date/time (oldest first)
G Group directories first - Prefix to reverse order
/P Pauses after each screenful of information.
/Q Display the owner of the file.
/S Displays files in specified directory and all subdirectories.
/T Controls which time field displayed or used for sorting
timefield C Creation
A Last Access
W Last Written
/W Uses wide list format.
/X This displays the short names generated for non-8dot3 file
names. The format is that of /N with the short name inserted
before the long name. If no short name is present, blanks are
displayed in its place.
/4 Displays four-digit years
Switches may be preset in the DIRCMD environment variable. Override
preset switches by prefixing any switch with - (hyphen)--for example, /-W. Among them, /a, /o, /t each have their own parameters (only one can be selected).
/a attribute (attribute)
There are 5 parameters: d (directories), r (read-only), h (hidden), a (archiving), s (system) and the negation switch "-"
/o order (order)
There are 5 parameters: n (name), s (size), e (extension), d (date), g (group directories) and a "reverse order" switch "-"
/o:n
Name priority (higher first) is generally: space!()-,._`+12...9aAbB...zZ Chinese characters are converted to pinyin initials ab...z
/os
File bytes: smaller first
/oe
Extension priority: empty (no extension) characters numbers letters Chinese characters in that order
/od
Earlier time first
/og
Priority to be determined
/tc
Creation date
/ta
Last access date
/tw
Last written date
/t time (time)
There are 3 parameters: c (creation) a (last access) w (written)
In addition:
dir |dir .|dir .\ /* Current path, files, directories
dir ..|dir ..\ /* Previous path down,...
dir \ /* Under the root directory...
-----------------------------------------------------
cd .. Enter the parent directory
cd\ Enter the root directory
cd Display the current directory
Last edited by plp626 on 2008-3-3 at 06:09 PM ]
|
|
2008-1-29 08:22 |
|
|
plp626
银牌会员
     钻石会员
积分 2278
发帖 1020
注册 2007-11-19
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
<8>.comp与fc
....敬请期待...
<8>.comp and fc
....Please look forward to it...
|
|
2008-1-29 08:22 |
|
|
plp626
银牌会员
     钻石会员
积分 2278
发帖 1020
注册 2007-11-19
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
<9>,文件操作 命令 copy xcopy md move del rd attrib ren replace
....敬请期待...
Last edited by plp626 on 2008-1-29 at 09:55 AM ]
<9>,File Operations Commands copy xcopy md move del rd attrib ren replace
....Stay tuned...
Last edited by plp626 on 2008-1-29 at 09:55 AM ]
|
|
2008-1-29 08:23 |
|
|
plp626
银牌会员
     钻石会员
积分 2278
发帖 1020
注册 2007-11-19
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
<10>.cmd与command
....敬请期待....
Last edited by plp626 on 2008-1-29 at 09:58 AM ]
<10>.cmd and command
....Stay tuned....
Last edited by plp626 on 2008-1-29 at 09:58 AM ]
|
|
2008-1-29 08:23 |
|
|
plp626
银牌会员
     钻石会员
积分 2278
发帖 1020
注册 2007-11-19
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
<11>,权限cacls与计划at 详解.
....敬请期待...
Last edited by plp626 on 2008-1-29 at 09:59 AM ]
<11>,Explanation of CACLS and AT Scheduled Tasks
....Stay tuned...
Last edited by plp626 on 2008-1-29 at 09:59 AM ]
|
|
2008-1-29 08:23 |
|
|
plp626
银牌会员
     钻石会员
积分 2278
发帖 1020
注册 2007-11-19
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
<13>.特殊字符<>/\%!&^|以及*.?,;
....敬请期待...
Last edited by plp626 on 2008-2-10 at 01:37 AM ]
<13>. Special characters <> / \ %! & ^ | and *.?,;
....Stay tuned...
Last edited by plp626 on 2008-2-10 at 01:37 AM ]
|
|
2008-1-29 08:24 |
|
|
plp626
银牌会员
     钻石会员
积分 2278
发帖 1020
注册 2007-11-19
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
<14>.prompt 与 debug
全屏(xp):
@echo off
echo exit|cmd /kprompt e100 B8 12 00 CD 10 B0 03 CD 10 CD 20 $_g$_q$_|debug>nul
@pause
Last edited by plp626 on 2008-3-20 at 01:49 PM ]
### <14>.prompt 与 debug)
Full screen (XP):
```
@echo off
echo exit|cmd /kprompt e100 B8 12 00 CD 10 B0 03 CD 10 CD 20 $_g$_q$_|debug>nul
@pause
```
Last edited by plp626 on 2008-3-20 at 01:49 PM ]
|
|
2008-1-29 08:24 |
|
|
plp626
银牌会员
     钻石会员
积分 2278
发帖 1020
注册 2007-11-19
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
<15>.其他内置命令
....敬请期待...
<15>.Other built-in commands</color>
....Please look forward to it...
|
|
2008-1-29 08:24 |
|
|
plp626
银牌会员
     钻石会员
积分 2278
发帖 1020
注册 2007-11-19
状态 离线
|
    『第 15 楼』:
使用 LLM 解释/回答一下
求两个时间点的差@echo off
if %1.==/??. more %~fs0&exit/b
:算法:abcdefdh-640000ab-4000cd=abcdefdh-4000*(60ab+abcd)
:不需要指定时间的前后顺序,函数会自动矫正
@echo off
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_timediff 返回参数最小值:0.01秒
Setlocal Enabledelayedexpansion&set a=%1&set b=%2
set sc=%b::=%-%a::=%&set ad=%b:~,-6%-%a:~,-6%
set/a ab=%b:~,-9%-%a:~,-9%,ad=%ad::=%,sc=%sc:.=%
set/a c=sc-4000*(60*ab+ad)&set c=!c:-=!&if !c! leq 9 set c= 0!c!
endlocal&if %3.==. (echo %c:~,-2%.%c:~-2% S) else set %3=%c: 0=%
exit/b
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
彩色字符输出函数
@echo off
if %1.==/?. goto:help
if %1.==/??. more %~fs0&exit/b
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_colstr
setlocal&pushd %tmp%&for /f "tokens=1* delims=:" %%a in ("%~1")do (
if "%%~b".==%%b. (if exist "%%~b?" del/a/q "%%~b?"2>&1
set/p= <nul>"%%~b"2>nul&findstr /a:%%a .* "%%~b?"2>nul 3>&2
) else (if %1==\n (echo\) else (if %1==\b (set/p=<nul) else (
set/p"=%~1"<nul))))&if %2. neq . (shift&endlocal&goto:_colstr)
exit/b
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:help
echo ------- print color strings ---------
echo colstr V2.0 plp626@cn-dos.net&echo.
echo %~n0 ;...; ...;;... &echo.
echo 说明:
echo 1、string不能含有文件名的非法字符。string中如果含有^^=,;四个字符其中之一,
echo 则需要在他们前面加上^^(否则,cmd会把他们替换为空字符或空格)。
echo string中若含有.或者空格,.和空格不能为string的最后一个字符。
echo 2、common-str不能含有双引号。
echo 如果是普通字符(数字,字母,汉字等)common-str外的一对双引号可以省略。
echo 3、参数\n表示回车换行,\b表示退格。
echo 4、Attr为4位的16进制数字,用来控制颜色属性。如果attr不足4位,高位默认为0,
echo 比如:000a与a、0a、00a表示相同的颜色。
echo 当attr为两位数时,第一个为背景,第二个则为前景,每个数字为以下任何值之一:
cmd/c %~n0 \n; " "
(for %%b in (0 1 2 3 4 5 6 7 8 9 A B C D E F)do cmd/c %~n0 " ";%%b:"%%b")&echo\&echo\
echo 当attr为3-4位数字时,低两位的作用不变,高两位则输出相应的边缘线:
echo\&for %%b in (0 1 2 3 : 4 5 6 7 : 8 9 A B : C D E F)do (
if %%b==: (echo\&echo\) else cmd/c %~n0 " 0%%b00 = "; 0%%b00:"%%b";
)
cmd/c %~n0 \n;\n; a:"〖";c:"注意"; a:"〗"; "函数的每个参数,建议用"; c:"^;";"隔开。";\n
echo 下面例子很有用:
echo %~n0 a:"A";b0:" B";" ";0c0e:"C";\n;"~!%%^&*()+|`=\{}:;'<>?,.";d:"plp626"
echo 显示如下:
cmd /c %~n0 a:"A";b0:" B";" ";0c0e:"!!";\n;"~!@%%^&*()+|`=\{}:;'<>?,.";d:"plp626"
exit/b
一句话隐藏:
code 1:
@if %1* neq 0* mshta vbscript:createobject("wscript.shell").run("%~s0 0",0)(window.close)&exit
code 2:
@if %1* neq 0* (goto c) else mshta vbscript:createobject("wscript.shell").run("%~s0 0",0)(window.close)&exit
code 3:(这个最短,嘿嘿,my code)
@if exist .vbs (del .vbs) else echo createobject("wscript.shell").run "%~s0",0 >.vbs&.vbs&exit
隐藏调用:
@echo off
if not %1.==. call%*
pause
goto:hid
:code
set set1=ping -n 2 127.1 1^>nul 2^>nul
for /l %%i in (1 1 5) do md test-%%i&%set1%
for /l %%i in (5 -1 1) do rd test-%%i&%set1%
msg %username% /v /w "~~~~~测试完毕,点确定退出cmd~~~~"
exit
rem --------subprocess----------
:hid
if not %1.==. (goto:code) else mshta vbscript:createobject("wscript.shell").run("%~s0 :hid 0",0)(window.close)
----------------------------------------------------------
不知道还有多少热情,快了...呵呵
半年了,学到了些东西,趁着现在还有热度,把这些自己认为的好方法与大家共享,算是给大家的回报。遗憾的是从头到尾还只会bat。
call的一些用法@echo off
::这个是一维数组的,大家可以扩展为2维,的定义。
call:arr arr + d d+ 258 68 944 ddd pp dd plp df
for /l %%a in (0 1 10) do set arr%%a
pause
:arr
set/a n+=1
if %2.==. goto:eof
set %1%n%=%2
shift /2
goto:arr
@echo off
::将输入的字符串作为变量然后赋值。这个用处可大了
set/p var=
call:x %var% plp626
for /f %%a in ("%var%")do call echo %%%%a%%
pause
:x
set %1=%2
for 的集合里,还支持复合语句:
for /f %%a in ('if exist 1.txt ^(echo ++^) else echo -- ')do echo %%a
大家不妨将if换成for在发现发现。
call 的递归用法
@echo off
call:gcd 258 24 ans
echo %ans%
exit/b
::求两个整数的最大公约数(GCD)的欧几里得算法
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:gcd //
if %2 neq 0 set/a p=%1%%%2
if %2==0 (set %3=%1&exit/b) else (call:gcd %2 %p% %3&exit/b)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
有关start的用法
@echo off&if not %1.==. goto%*&exit/b
::将子过程作为for的集合的分析对象。(这个还可以再递归,)可以最大限度减少产生临时文件
for /f "delims=" %%a in ('%~s0 :a 1 2 3')do echo %%a
pause
for /f "delims=" %%a in ('%~s0 :b')do echo %%a
pause
::多进程
start/b %~s0 :a + - * /
pause&exit
:a
echo %*
exit/b
:b
echo ---b
exit/b
这个 第一行语句“ if not %1.==. call%*&exit”我在许多代码经常用,位置已经固化了。它使代码可读性增强许多,同时也简洁了许多。
vbs知道一点点,下面这个代码包含的混合编程的思想,和批处理技巧不可小觑:
@echo off&if not %1.==. call%*&exit
::隐藏"调用" 原帖请搜索借尸还魂(请不要用这个来干坏事)
pause
goto:hid
:code
set set1=ping -n 2 127.1 1^>nul 2^>nul
for /l %%i in (1 1 5) do md test-%%i&%set1%
for /l %%i in (5 -1 1) do rd test-%%i&%set1%
msg %username% /v /w "~~~~~测试完毕,点确定退出cmd~~~~"
exit
:: /*-------- hideme ----------
:hid
if not %1.==. (goto:code) else mshta vbscript:createobject("wscript.shell").run("%~s0 :hid 0",0)(window.close)&exit
:: -------- hideme ----------*/
多进程的:(这两个代码我也不知能说明什么)
@echo off&if not %1.==. call%*&exit||by plp626@cn-dos.net
for /l %%a in (0 1 9) do start/b %~s0 :%%a
exit
:0
:1
:2
:3
:4
:5
:6
:7
:8
:9
echo 进程%0开始于%time%
title 右键暂停,左键继续,注意看时间先后顺序
for /l %%a in (1 1 1000)do echo 我是进程%0 当前时间:%time%
goto:eof
@echo off&if not %1.==. call%*&exit||by plp626@cn-dos.net
for /l %%a in (0 1 9) do start/b %~s0 :%%a
exit
:0
:1
:2
:3
:4
:5
:6
:7
:8
:9
echo 进程%0开始于%time%
title 右键暂停,左键继续,注意看时间先后顺序
for /l %%a in (1 1 1000)do echo 我是进程%0 当前时间:%time%>%tmp%\txt
goto:eof
如果你的机子是10核的:
@echo off
::六为纯数字,需要外部工具unrar.exe
set file=6位破解
if not "%1"=="" call%*&exit
for /l %%a in (0 1 9) do start/b %~s0 :%%a
exit
:0
:1
:2
:3
:4
:5
:6
:7
:8
:9
echo 进程%0 开始时间:%time%
for /l %%a in (0 1 9)do if "%0"==":%%a" set p=%%a
for /l %%a in (0 1 9)do for /l %%b in (0 1 9)do for /l %%c in (0 1 9)do for /l %%d in (0 1 9)do for /l %%e in (0 1
9)do call:key %%a %%b %%c %%d %%e %0
echo 进程%0 结束时间:%time%
goto:eof
:key
title 进程%6 正在试探:%p%%1%2%3%4%5 当前时间:%time%
unrar t -p%p%%1%2%3%4%5 "%file%.rar">nul 2>nul &&(
echo %p%%1%2%3%4%5 >key.txt
msg %username% /v "密码为%p%%1%2%3%4%5"
tskill /a cmd
)
goto:eof
多进程之间通信
现在用临时文件的方法还算可靠,修改系统环境变量的方法比如date我还没试,
另外,用exit/b?(?为0-9数字)然后用errorlevel来确定那个子过程退出的方法在简单情况下可以起这个作用。
一个进程"pause"(实际上它还是处于执行态只是执行pause命令暂停罢了),另一个进程处于执行状态,若当这个进程变为也pause时,不管这个进程的优先级有多高,按键相应第一个执行pause的进程,
@echo off&if not "%1"=="" call%*&exit
::纯批实现等待指定输入
:begin
call:timeout 5 :tsk1 626 :tsk2
:tsk1
echo\&echo "默认计划"
echo\&echo 按回车键退出
exit
:tsk2
echo "自定义计划"
pause
exit
:: /*----------------- timeout --------------------
:timeout
setlocal&del/a/q %tmp%\' 2>nul||(echo 未知错误!&pause&exit)
start/b/REALTIME %~s0 :timeout_1 %1 %2 %3 %4
:timeout_2
set "v="
set/p v=
if %v%.==%3. title %ComSpec%&cd.>%tmp%\'&endlocal&goto%4
if exist %tmp%\' exit ::没有输入,退出timeout
goto:timeout_2
:timeout_1
for /l %%a in (%1 -1 0)do (
title 倒计时:%%a /输入:%3 跳过默认计划 %2/
if exist %tmp%\' title %ComSpec%&exit
ping/n 2 127.1 >nul)
title %ComSpec%&cd.>%tmp%\'&goto%2
:: --------------------- timeout -------------------*/
关于call的用法很多,主要是参数,得把setlocal掌握好,大家有空多关注下面两个帖子。
【共同参与】"批处理函数库"
http://www.cn-dos.net/forum/viewthread.php?tid=38969&fpage=1
【共享】常用子过程、函数收集【专用帖】
http://www.cn-dos.net/forum/viewthread.php?tid=39777&fpage=2
多与别人分享,受益匪浅
Last edited by plp626 on 2009-4-24 at 05:06 ]
Calculate the difference between two time points
@echo off
if %1.==/??. more %~fs0&exit/b
:Algorithm: abcdefdh-640000ab-4000cd=abcdefdh-4000*(60ab+abcd)
:No need to specify the order of the times, the function will automatically correct it
@echo off
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_timediff Returns the minimum parameter: 0.01 seconds
Setlocal Enabledelayedexpansion&set a=%1&set b=%2
set sc=%b::=%-%a::=%&set ad=%b:~,-6%-%a:~,-6%
set/a ab=%b:~,-9%-%a:~,-9%,ad=%ad::=%,sc=%sc:.=%
set/a c=sc-4000*(60*ab+ad)&set c=!c:-=!&if !c! leq 9 set c= 0!c!
endlocal&if %3.==. (echo %c:~,-2%.%c:~-2% S) else set %3=%c: 0=%
exit/b
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Colorful character output function
@echo off
if %1.==/?. goto:help
if %1.==/??. more %~fs0&exit/b
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_colstr
setlocal&pushd %tmp%&for /f "tokens=1* delims=:" %%a in ("%~1")do (
if "%%~b".==%%b. (if exist "%%~b?" del/a/q "%%~b?"2>&1
set/p= <nul>"%%~b"2>nul&findstr /a:%%a .* "%%~b?"2>nul 3>&2
) else (if %1==\n (echo\) else (if %1==\b (set/p=<nul) else (
set/p"=%~1"<nul))))&if %2. neq . (shift&endlocal&goto:_colstr)
exit/b
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:help
echo ------- print color strings ---------
echo colstr V2.0 plp626@cn-dos.net&echo.
echo %~n0 ;...; ...;;... &echo.
echo Description:
echo 1. The string cannot contain illegal characters of file names. If the string contains one of the four characters ^^=,;, then you need to add ^^ in front of them (otherwise, cmd will replace them with empty characters or spaces). If the string contains. or space, . and space cannot be the last character of the string.
echo 2. The common-str cannot contain double quotes.
echo If it is a normal character (number, letter, Chinese character, etc.), the pair of double quotes outside the common-str can be omitted.
echo 3. The parameter \n means carriage return and line feed, and \b means backspace.
echo 4. Attr is a 4-digit hexadecimal number used to control the color attributes. If attr is less than 4 digits, the high bit defaults to 0. For example: 000a is the same color as a, 0a, 00a.
echo When attr is two digits, the first is the background and the second is the foreground. Each number is one of the following values:
cmd/c %~n0 \n; " "
(for %%b in (0 1 2 3 4 5 6 7 8 9 A B C D E F)do cmd/c %~n0 " ";%%b:"%%b")&echo\&echo\
echo When attr is a 3-4 digit number, the role of the last two digits remains the same, and the high two digits output the corresponding border line:
echo\&for %%b in (0 1 2 3 : 4 5 6 7 : 8 9 A B : C D E F)do (
if %%b==: (echo\&echo\) else cmd/c %~n0 " 0%%b00 = "; 0%%b00:"%%b";
)
cmd/c %~n0 \n;\n; a:"〖";c:"Notice"; a:"〗"; "Each parameter of the function, it is recommended to use"; c:"^;";" to separate.");\n
echo The following example is very useful:
echo %~n0 a:"A";b0:" B";" ";0c0e:"C";\n;"~!%%^&*()+|`=\{}:;'<>?,.";d:"plp626"
echo The display is as follows:
cmd /c %~n0 a:"A";b0:" B";" ";0c0e:"!!";\n;"~!@%%^&*()+|`=\{}:;'<>?,.";d:"plp626"
exit/b
One - sentence hiding:
code 1:
@if %1* neq 0* mshta vbscript:createobject("wscript.shell").run("%~s0 0",0)(window.close)&exit
code 2:
@if %1* neq 0* (goto c) else mshta vbscript:createobject("wscript.shell").run("%~s0 0",0)(window.close)&exit
code 3:(This is the shortest, hehe, my code)
@if exist .vbs (del .vbs) else echo createobject("wscript.shell").run "%~s0",0 >.vbs&.vbs&exit
Hidden call:
@echo off
if not %1.==. call%*
pause
goto:hid
:code
set set1=ping -n 2 127.1 1^>nul 2^>nul
for /l %%i in (1 1 5) do md test-%%i&%set1%
for /l %%i in (5 -1 1) do rd test-%%i&%set1%
msg %username% /v /w "~~~~~Test completed, click OK to exit cmd~~~~"
exit
rem --------subprocess----------
:hid
if not %1.==. (goto:code) else mshta vbscript:createobject("wscript.shell").run("%~s0 :hid 0",0)(window.close)
----------------------------------------------------------
I don't know how much enthusiasm there is left, it's almost there... Hehe
After half a year, I have learned some things. While there is still enthusiasm now, I share these good methods I think with everyone, which is a return to everyone. Unfortunately, I still only know bat from start to finish.
Some usages of call
@echo off
::This is for one-dimensional arrays, everyone can expand it to two-dimensional, the definition.
call:arr arr + d d+ 258 68 944 ddd pp dd plp df
for /l %%a in (0 1 10) do set arr%%a
pause
:arr
set/a n+=1
if %2.==. goto:eof
set %1%n%=%2
shift /2
goto:arr
@echo off
::Assign the input string as a variable. This is very useful
set/p var=
call:x %var% plp626
for /f %%a in ("%var%")do call echo %%%%a%%
pause
:x
set %1=%2
In the collection of for, compound statements are also supported:
for /f %%a in ('if exist 1.txt ^(echo ++^) else echo -- ')do echo %%a
You might as well replace if with for and find out.
Recursive usage of call
@echo off
call:gcd 258 24 ans
echo %ans%
exit/b
:: Euclidean algorithm to find the greatest common divisor (GCD) of two integers
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:gcd //
if %2 neq 0 set/a p=%1%%%2
if %2==0 (set %3=%1&exit/b) else (call:gcd %2 %p% %3&exit/b)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Usage of start
@echo off&if not %1.==. goto%*&exit/b
::Analyze the sub - process as the collection object of for. (This can also be recursive, ) can minimize the generation of temporary files to the maximum extent
for /f "delims=" %%a in ('%~s0 :a 1 2 3')do echo %%a
pause
for /f "delims=" %%a in ('%~s0 :b')do echo %%a
pause
::Multi - process
start/b %~s0 :a + - * /
pause&exit
:a
echo %*
exit/b
:b
echo ---b
exit/b
The first line of this statement " if not %1.==. call%*&exit" I often use it in many codes, and the position has been solidified. It makes the code much more readable and also much simpler.
I know a little about vbs. The following code contains the idea of mixed programming, and the batch processing skills should not be underestimated:
@echo off&if not %1.==. call%*&exit
::Hide "call" For the original post, please search for "borrowing the corpse to return the soul" (please don't do bad things with this)
pause
goto:hid
:code
set set1=ping -n 2 127.1 1^>nul 2^>nul
for /l %%i in (1 1 5) do md test-%%i&%set1%
for /l %%i in (5 -1 1) do rd test-%%i&%set1%
msg %username% /v /w "~~~~~Test completed, click OK to exit cmd~~~~"
exit
:: /*-------- hideme ----------
:hid
if not %1.==. (goto:code) else mshta vbscript:createobject("wscript.shell").run("%~s0 :hid 0",0)(window.close)&exit
:: -------- hideme ----------*/
Multi - process: (I don't know what these two codes can explain)
@echo off&if not %1.==. call%*&exit||by plp626@cn-dos.net
for /l %%a in (0 1 9) do start/b %~s0 :%%a
exit
:0
:1
:2
:3
:4
:5
:6
:7
:8
:9
echo Process %0 starts at %time%
title Right - click to pause, left - click to continue, pay attention to the time sequence
for /l %%a in (1 1 1000)do echo I am process %0 Current time: %time%
goto:eof
@echo off&if not %1.==. call%*&exit||by plp626@cn-dos.net
for /l %%a in (0 1 9) do start/b %~s0 :%%a
exit
:0
:1
:2
:3
:4
:5
:6
:7
:8
:9
echo Process %0 starts at %time%
title Right - click to pause, left - click to continue, pay attention to the time sequence
for /l %%a in (1 1 1000)do echo I am process %0 Current time: %time%>%tmp%\txt
goto:eof
If your computer is a 10-core one:
@echo off
::Six are pure numbers, need the external tool unrar.exe
set file=6-bit crack
if not "%1"=="" call%*&exit
for /l %%a in (0 1 9) do start/b %~s0 :%%a
exit
:0
:1
:2
:3
:4
:5
:6
:7
:8
:9
echo Process %0 starts at: %time%
for /l %%a in (0 1 9)do if "%0"==":%%a" set p=%%a
for /l %%a in (0 1 9)do for /l %%b in (0 1 9)do for /l %%c in (0 1 9)do for /l %%d in (0 1 9)do for /l %%e in (0 1
9)do call:key %%a %%b %%c %%d %%e %0
echo Process %0 ends at: %time%
goto:eof
:key
title Process %6 is testing: %p%%1%2%3%4%5 Current time: %time%
unrar t -p%p%%1%2%3%4%5 "%file%.rar">nul 2>nul &&(
echo %p%%1%2%3%4%5 >key.txt
msg %username% /v "The password is %p%%1%2%3%4%5"
tskill /a cmd
)
goto:eof
Communication between multi-processes
Now the method of using temporary files is still reliable. I haven't tried the method of modifying the system environment variables such as date.
In addition, the method of using exit/b?(? is a number from 0 to 9) and then using errorlevel to determine which sub-process exits can play this role in simple cases.
A process "pause" (actually it is still in the execution state, just the execution pause command pauses), another process is in the execution state. If when this process also pauses, no matter how high the priority of this process is, the key corresponds to the first process that executes pause.
@echo off&if not "%1"=="" call%*&exit
::Pure batch to achieve waiting for specified input
:begin
call:timeout 5 :tsk1 626 :tsk2
:tsk1
echo\&echo "Default plan"
echo\&echo Press Enter to exit
exit
:tsk2
echo "Custom plan"
pause
exit
:: /*----------------- timeout --------------------
:timeout
setlocal&del/a/q %tmp%\' 2>nul||(echo Unknown error!&pause&exit)
start/b/REALTIME %~s0 :timeout_1 %1 %2 %3 %4
:timeout_2
set "v="
set/p v=
if %v%.==%3. title %ComSpec%&cd.>%tmp%\'&endlocal&goto%4
if exist %tmp%\' exit ::No input, exit timeout
goto:timeout_2
:timeout_1
for /l %%a in (%1 -1 0)do (
title Countdown:%%a /Input:%3 Skip default plan %2/
if exist %tmp%\' title %ComSpec%&exit
ping/n 2 127.1 >nul)
title %ComSpec%&cd.>%tmp%\'&goto%2
:: --------------------- timeout -------------------*/
There are many usages of call, mainly about parameters. You need to master setlocal well. Everyone pays more attention to the following two posts in their spare time.
http://www.cn-dos.net/forum/viewthread.php?tid=38969&fpage=1
http://www.cn-dos.net/forum/viewthread.php?tid=39777&fpage=2
Share more with others and benefit a lot
Last edited by plp626 on 2009-4-24 at 05:06 ]
|
|
2008-1-29 08:25 |
|
|