失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 批处理命令——bat文件创建和基本命令语法

批处理命令——bat文件创建和基本命令语法

时间:2022-12-22 06:56:23

相关推荐

批处理命令——bat文件创建和基本命令语法

批处理文件创建:

1. 使用copy con命令创建批处理文件

con(console)代表计算机屏幕,该命令是将计算机屏幕上的内容输入到指定的文件中,适用于创建较小的bat文件,不能用于已有的bat文件编辑

2. 用记事本创建批处理文件

2. 使用edit命令创建和编辑批处理文件,比较古老,很少使用。

基本语法:

1. echo命令和@符号 ,一般开头就会使用@echo off

echo [{on|off}] [messgae], echo on后面执行的命令都会显示在屏幕上,echo off 会静静地执行(包括当前行和之后的所有行);

@会限定当前执行命令的回显(只针对当前行),且不受echo命令的限制;

@echo on @type a.txt@echo switch off the echoecho offtype a.txt

2. pause 用于暂停批处理的执行,显示“请按任意键继续...”

3. call命令 call [[Drive] [Path] filename [Parameter]] [[:label] [arguments]]

call a.bat //调用意外一个bat文件call :xx //调用xx标识符处的命令:xxecho here is xx cmd!

4. rem命令,用于注释,执行时会被忽视

5. set命令,用于设置和显示变量

6. goto命令,流程控制转向命令, goto label, 去执行:label处的程序,label最多8ge字符,超过8个则只识别前8个。

7. start命令,重新启动一个新的窗口,执行指定的命令,start ["title"] [/dpath] [/i:] [/min] [/max] [wait](等待新窗口结束才继续执行)

8. if命令,条件判断

if exits a.txt (echo find the file) else (echo don't find the file)if [not] errorLevel number [else expression] if [not] string1 == string2 [else expression] if [not] exist file [else expression] if [/i] string1 compareOp string2 [else expression] compareOp:EQUNEQLSSLEQGTRGEQ

9. for语句

@echo offrem /a is to make the string expression to value expressionset /a num = 0for %%x in (*.txt) do{echo get file %%x, and content as follow:type %%xrem echo. is to change lineecho.set /a num = num + 1}echo totally we get %x% files.for [%var|%%var] in (set) do command

10. setlocal 语句,批处理过程中,设置局部的环境变量,不会影响系统环境变量,需使用endlocal来匹配结束

@echo offsetlocalpath = d:\echo the local path variable is:set pathendlocalecho the system path variable is:set pathsetlocal {enableextension|diasbleextension} {enabledelayedextension|diabledelayedextension}endlocal

11.shift 命令,更改批处理命令处理参数的方式

shift [/n]rem test this by shift.bat a.txt b.txt@echo off:roundif "%1" == "" goto endecho %1 's content as follow:type %1echo.shiftgoto round:end

12. 通配符 * 和 ?

13. 重定向符号< ,<<和>

@echo offecho type a.txt > b.batecho dir /b /w >> b.batecho the content of the b.bat is:type b.batecho the b.bat after sorting:sort < b.bat

14. 管道符|,左边的内容输出到右侧

@echo off find "key" in a.txt | sort > b.txtecho the result is:type b.txt

如果觉得《批处理命令——bat文件创建和基本命令语法》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。