简单的批处理命令(二)

传送门

简单的批处理命令(一):.


5. rem

语法: rem [message]

说明: rem是注释命令,其后的message将不会被执行,只是起注释的作用,便于自己和别人阅读。需要注意的是,rem后面的message在执行命令过程中会被忽略掉,而@以及@echo off是不回显,请注意它们之间的区别。
另外,rem命令常常用两个冒号即" :: "代替。

6. pause

语法: pause

说明: pause是暂停命令,即执行到pause命令后会暂停,此时屏幕上会显示“请按任意键继续…” or “Press any key to continue…”

7. call

语法: call [Path][filename] [params] [:label [args]]

说明: 从一个批处理程序调用另一个批处理程序(包括自身),并且不会终止父批处理程序。如果直接调用批处理程序而不是使用call命令,那么执行完被调用的批处理程序后,不会返回当前程序的后续命令。
call命令的参数必须要指明被调用的批处理程序的位置和文件名,且文件名必须要包含扩展名。
call命令也支持直接调用目标程序的标签(label)。
最后需要注意的是,如果call命令在脚本或批处理文件之外使用,它将不会在命令行里起作用。

8. choice

语法: CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]

说明: choice命令是DOS或Windows的外部命令,使用时可以在命令提示行里输入 choice /? 来查看当前版本的语法规则。以下是我电脑版本下CHOICE的用法,仅供参考:
Description:
This tool allows users to select one item from a list of choices and returns the index of the selected choice.
Parameter List:
/C choices:Specifies the list of choices to be created. Default list is “YN”.
/N:Hides the list of choices in the prompt. The message before the prompt is displayed and the choices are still enabled.
/CS:Enables case-sensitive choices to be selected. By default, the utility is case-insensitive.
/T timeout:The number of seconds to pause before a default choice is made. Acceptable values are from 0 to 9999. If 0 is specified, there will be no pause and the default choice is selected.
/D choice:Specifies the default choice after nnnn seconds. Character must be in the set of choices specified by /C option and must also specify nnnn with /T.
/M text:Specifies the message to be displayed before the prompt. If not specified, the utility displays only a prompt.
/?:Displays this help message.

NOTE:
The ERRORLEVEL environment variable is set to the index of the key that was selected from the set of choices. The first choice listed returns a value of 1, the second a value of 2, and so on. If the user presses a key that is not a valid choice, the tool sounds a warning beep. If tool detects an error condition, it returns an ERRORLEVEL value of 255.If the user presses ^BREAK or ^C, the tool returns an ERRORLEVEL value of 0. When you use ERRORLEVEL parameters in a batch program, list them in decreasing order.

再次提醒一下,choice命令执行后返回的errorlevel的值是 从1开始 的,除非终止了命令的继续执行。^BREAK or ^C的意思是CTRL+Break or CTRL+C(下同)。
下面举个例子:

@echo off
echo ---------------------------------------------------------------------------------------------
echo 1.Your content is very helpful!
echo 2.Your content is helpful, but I still have some problems. Will  leave a massage or comments.
echo 3.What did you say? It's totally terrible!
echo 4.Will choose next time, let me go for now!
echo ---------------------------------------------------------------------------------------------

echo Choose a number to express yourself now!
::/C:give choice 1234, /N: to hide them, /T 15: pause 15s to let you make your choice, 
/D 1: if you didn't choose one of them after 15s, 1 will be selected, /M: a message for users.
choice /C 1234 /N /T 15 /D 1 /M "Give your choice:"

::we can use errorlevel directly. 
if %errorlevel%==4 goto quit
if %errorlevel%==3 goto angry
if %errorlevel%==2 goto doubt
if %errorlevel%==1 goto praise

:: Define some labels.
:quit
echo Bye~
goto end

:angry
echo Sorry to do less help for you.
goto end

:doubt
echo Don't be shy of connecting with me to discuss more!
goto end

:praise
echo Congratulations!
goto end

:end
pause
9. start

语法: START [“title”] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
[/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
[/NODE ] [/AFFINITY ] [/WAIT] [/B]
[command/program] [parameters]

说明: start 启动命令提示符来执行命令,如果没有参数,则会启动一个新的命令提示符窗口。下面记录一下各个参数及意义。
"title":Title to display in window title bar.
path : Starting directory.
B:Start application without creating a new window. The application has ^C handling ignored. Unless the application enables ^C processing, ^Break is the only way to interrupt the application.
I:The new environment will be the original environment passed to the cmd.exe and not the current environment.
MIN:Start window minimized.
MAX:Start window maximized.
SEPARATE:Start 16-bit Windows program in separate memory space.
SHARED:Start 16-bit Windows program in shared memory space.
LOW:Start application in the IDLE priority class.
NORMAL:Start application in the NORMAL priority class.
HIGH: Start application in the HIGH priority class.
REALTIME:Start application in the REALTIME priority class.
ABOVENORMAL:Start application in the ABOVENORMAL priority class.
BELOWNORMAL: Start application in the BELOWNORMAL priority class.
NODE:Specifies the preferred Non-Uniform Memory Architecture (NUMA) node as a decimal integer.
AFFINITY:Specifies the processor affinity mask as a hexadecimal number. The process is restricted to running on these processors.
The affinity mask is interpreted differently when /AFFINITY and /NODE are combined. Specify the affinity mask as if the NUMA node’s processor mask is right shifted to begin at bit zero. The process is restricted to running on those processors in common between the specified affinity mask and the NUMA node. If no processors are in common, the process is restricted to running on the specified NUMA node.
WAIT:Start application and wait for it to terminate.
command/program:If it is an internal cmd command or a batch file then the command processor is run with the /K switch to cmd.exe. This means that the window will remain after the command has been run. If it is not an internal cmd command or batch file then it is a program and will run as either a windowed application or a console application.
parameters:These are the parameters passed to the command/program.

NOTE: The SEPARATE and SHARED options are not supported on 64-bit platforms.

这里很多参数都还没有用过,下面举几个例子仅供参考:

  1. start "TestBatShowing" /max /belownormal test.bat
    以低于常规优先级启动test.bat并最大化,同时且启动后的命令提示窗口的title是TestBatShowing。
  1. @echo off
    start /wait test.bat
    ::打开D盘根目录
    start explorer d:\\
    pause
    等待test.bat程序运行结束,回到终端之后再决定是否要继续执行下面的命令。关闭test.bat之后会出现 ^CTerminate batch job (Y/N)?,意思是是否终止程序的继续,输入N之后,就会执行下面的命令了。
10. for

说在前头: 如果在命令提示符里面输入"for /?", 界面上会显示出很长的内容,筛选点有用的信息并举简单的例子如下:
语法: FOR %%variable IN (set) DO command [command-parameters]
说明: 虽然 for /?之后这个命令里面用的是一个%,但在批处理程序中使用for时,应当用%%variable(下同)。
%%variable :指定一个单一字母可替换的参数,需要注意的是,这个字母是大小写敏感的。
set :指定一个或一组文件,可以使用通配符。
command:对每个文件要执行的命令。
parameters:为特定命令指定参数或命令行开关。
举例:
for %%i in (D:\*.bat) do @echo %%i :: 显示D盘下扩展名为.bat的文件全名

如果启用了命令扩展,则for命令可以扩展出一下格式的命令(4个参数 /D /R /L /F ):
10.1 语法: FOR /D %%variable IN (set) DO command [command-parameters]
说明: /D这个命令主要用于目录搜索,不会搜索文件,故如果set中包含了通配符,则指定与目录名匹配,而不是文件名。
举例:
for /d %%i in (D:\???) do @echo %%i :: 显示D盘下名字只有1-3个字母的目录

10.2 语法: FOR /R [[drive:]path] %%variable IN (set) DO command [command-parameters]
说明: /R搜索指定路径及所有子目录中与set相符合的所有文件,如果不包含,则不会列举。如果set中没有通配符,即一个具体的文件,则会列举指定drive:path及其所有子目录,在列举时,会将具体的文件名加在后面,并且不论set这个文件是否真是存在
举例:
for /r D:\KnowledgeLearning\ %%i in (123.txt) do @echo %%i :: 列举for /r D:\KnowledgeLearning\ %%i in (123.txt) do @echo %%i所有的123.txt文件。
(这里这个文件是不存在的,但实际上显示的结果是:D:\KnowledgeLearning\123.txt)
for /r D:\ %%i in (123.txt) do if exist %%i @echo %%i :: 这个例子实现文件的搜索,在D盘下搜索123.txt文件存在的目录。
(不过,当D盘里存放了五花八门的文件夹,而且有的路径还比较深,使用这个命令时可能效率也会降低,所以在用这个命令时,建议缩小范围。忽然想到Windows窗口的搜索功能,不知道哪个会更快呢?)
10.3 语法: FOR /L %%variable IN (start,step,end) DO command [command-parameters]
说明: 这个命令中的set表示的是一个从start到end的有序数字,其中step表示增量,注意,这个增量可以是负的
举例:
for /L %%i in (5,-1,1) do @echo %%i :: 显示的结果是5,4,3,2,1
for /l %%i in (1,1,5) do md %%i :: 建立从1~5共5个文件夹
for /l %%i in (1,1,5) do rd /q %%i :: 删除从1~5共5个文件夹
10.4 语法:
FOR /F [“options”] %%variable IN (file-set) DO command [command-parameters]
FOR /F [“options”] %%variable IN (“string”) DO command [command-parameters]
FOR /F [“options”] %%variable IN (‘command’) DO command [command-parameters]

or, if usebackq option present:
FOR /F [“options”] %%variable IN (file-set) DO command [command-parameters]
FOR /F [“options”] %%variable IN (‘string’) DO command [command-parameters]
FOR /F [“options”] %%variable IN (`command`) DO command [command-parameters]
说明: 使用文件解析来处理文件、字符串以及命令。option提供了解析修改解析的方式(file-set可以是一个或多个文件名,对每个文件的操作会在跳转至下一个文件之前完成;这些操作包括读取、将文件分分行,再将每一行解析成0或更多的符号)。然后将此符号赋值给变量,供循环体使用。
options introduction:
eol=c: 指定注释符结束符(以指定符号开头的的行将被忽略,注意,c只能有一个)
skip=n : 指定从文件的开头忽略的行数
delims=xxx:指定分隔符集,默认的是空格键和tab键
tokens=x,y,m-n: 指定在每一次迭代过程中,每一行的哪个符号将会被传递到循环体中,注意此行为会造成额外变量的分配。m-n表示从m到n这个范围。如果tokens= string的string中的最后一个符号是*,额外变量将会在最后一个符号解析之后分配,并接受行上剩余的文本。此时额外的变量是通过tokens隐式声明的。
usebackq: 指定新语法生效,其中,反引号字符串 ``(键盘上1左边的键)作为命令执行,单引号字符串 ‘’ 是字面字符串命令,并允许使用双引号在file-set中引用文件名。
需要注意的是:
1. 使用usebackq处理文件时,当文件名有空格的时候,需要将文件名用双引号括起来
2. 使用usebackq处理字符串时,字符串应该用单引号
3. 使用usebackq处理命令时,字符串用的是``.
举例:
:: 处理test1.txt时,忽略以;开头的行。
for /F "eol=;" %%i in (test1.txt) do echo %%i
:: 使用了usebackq来处理带有空格的文件名。如果没有双引号,文件是找不到的。
for /F "usebackq eol=;" %%i in ("test Test2.txt") do echo %%i
:: 处理指定字符串回显第二个、第五个和剩余的符号。(%%i接受第二个符号,%%j接受第五个符号,%%k即额外变量,接受剩余的符号。)
for /F "tokens=2,5*" %%i in ("1 2 3 4 5 6 7 8 9") do echo tokens1=%%i tokens2=%%j tokens3=%%k
:: 使用了usebackq处理字符串时必须使用单引号,否则会被认为处理文件。
for /F "usebackq tokens=2,5*" %%i in ('1 2 3 4 5 6 7 8 9') do echo tokens1=%%i tokens2=%%j
:: hello.bat的内容是@echo hello~,处理命令时,将命令传递给子CMD.exe,并对当作文件进行分析。
for /F %%i in ('hello.bat') do echo %%i
:: 使用了usebackq,处理命令时必须用``
for /F "usebackq" %%i in (`hello.bat`) do echo %%i
:: 修改默认的分隔符,一下则会显示除空白行及注释行之外的所有内容。
:: 注意用delims= string去替换默认的分隔符时,如果string有空格个,空格应当放在最后。
for /F "delims=, " %%i in (test1.txt) do echo %%i

for的变量增强
在for的使用过程中,变量可以带有指定的修饰符,以此来实现变量的增强。它的语法是%~I,其中I就是命令中定义的变量,比如命令中用字母k来代替变量,那么这里就是%~k. 再次提醒,变量名是区分发小写的。

带有修饰符的变量及说明:
%%~I 展开 %%I 可删除任何周围引号。
::如果变量指代的元素前后的引号是成对出现的,则这对引号都会被删除,如果只是前面有引号,也会被删除,但如果只是末尾有引号,以所指代的元素中见有引号,是不会被删的。比如一个元素是aa"bb",那这两个引号都不会被删除。总结成一句话就是有首删首尾,无首不用删

%%~fI 扩展 %%I 到完全限定的路径名称。

%%~dI %%I仅扩展到驱动盘符。

%%~pI %%I仅扩展一个路径。

%%~nI %%I仅扩展到文件名。

%%~xI %%I仅扩展到文件扩展名。

%%~sI 将路径展开为仅包含短名称。

%%~aI 扩展 %%I 到文件的文件属性。

%%~tI 扩展 %%I 到文件的日期和时间。

%%~zI 扩展 %%I 到文件的大小。

%%~$PATH:I 搜索 PATH 环境变量中列出的目录,并将其扩展 %I 到找到的第一个目录的完全限定名称。 如果未定义环境变量名称或搜索找不到该文件,此修饰符将扩展为空字符串。

如下也列出了可用于获取复合结果的修饰符组合:
%%~dpI %%I 仅扩展到驱动器号和路径。
%%~nxI %%I 仅扩展到文件名和扩展名。
%%~fsI %%I 将扩展为仅带有短名称的完整路径名称。
%%~dp$PATH:I 搜索 PATH 环境变量中列出的目录,并将其 %I 扩展到找到的第一个驱动器号和路径。
%%~ftzaI 扩展%%I 为类似于 dir 的输出行。
下面将这些扩展列举了一下例子,你只需要创建一个.bat文件,并将下面的代码复制进去,然后执行一下就可以很清晰的明白上面这些拓展究竟做了什么。
需要说明两点
1.组合命令只列举了%%~ftzaI,类似于dir的输出行,直观的感受一下组合命令。其他组合命令相信学完单一命令之后,也很好理解。当然,也可以自己自由组合。
2.%%~$PATH:I 中,可以在自己的环境变量中找一个已定义的文件来试试,set那里记得要用双引号。

@echo off
echo Here are usages for enhanced variable:
echo %%~i - expands %%i removing any surrounding quotes (")
FOR /F "delims==" %%i IN ('dir *.bat /b') DO echo %%~i

echo %%~fi - expands %%i to a fully qualified path name
FOR /F "delims==" %%i IN ('dir *.bat /b') DO echo %%~fi

echo %%~di - expands %%i to a drive letter only
FOR /F "delims==" %%i IN ('dir *.bat /b') DO echo %%~di

echo %%~pi - expands %%i to a path only
FOR /F "delims==" %%i IN ('dir *.bat /b') DO echo %%~pi

echo %%~ni - expands %%i to a file name only
FOR /F "delims==" %%i IN ('dir *.bat /b') DO echo %%~ni

echo %%~xi - expands %%i to a file extension only
FOR /F "delims==" %%i IN ('dir *.bat /b') DO echo %%~xi

echo %%~si - expanded path contains short names only
FOR /F "delims==" %%i IN ('dir *.bat /b') DO echo %%~si

echo %%~ai - expands %%i to file attributes of file
FOR /F "delims==" %%i IN ('dir *.bat /b') DO echo %%~ai

echo %%~ti - expands %%i to date/time of file
FOR /F "delims==" %%i IN ('dir *.bat /b') DO echo %%~ti

echo %%~zi - expands %%i to size of file
FOR /F "delims==" %%i IN ('dir *.bat /b') DO echo %%~zi

echo %%~$PATH:i - searches the directories listed in the PATH environment variable and expands %%I to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string.
FOR /F "delims==" %%i IN ("Perl.exe") DO echo %%~$PATH:i

echo %%~ftzai - expands %%I to a DIR like output line
FOR /F "delims==" %%i IN ('dir *.bat /b') DO echo %%~ftzai
pause

至此,简单的介绍了10个批处理命令,其中for命令相对来说稍微有点复杂。但耐下心来学习,还是很好理解的(请忽略上面这段代码的排版,实在是太难了~,比for /f … 这个命令还难以理解~)。

写在最后

打开windows的命令提示符,把你想了解的命令输进去,然后输入 " /?"(注意/前面有一个空格),回车之后就可以看到这个命令的用法了。比如for /?,就可以看到for的用法了。你可以输入color /?, 然后试着给你的命令提示行换个背景色吧~
for /?
shutdown -s -t 0 我用了很久的关机方式~
参考:
https://docs.microsoft.com/zh-cn/windows-server/administration/windows-commands/windows-commands.
https://www.jb51.net/article/41322.htm
https://www.jb51.net/article/140920.htm

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值