批处理命令——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 echo
echo off
type a.txt

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

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

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

:xx
echo 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:
EQU
NEQ
LSS
LEQ
GTR
GEQ

  9. for语句

@echo off
rem /a is to make the string expression to value expression
set /a num = 0
for %%x in (*.txt) do{
echo get file %%x, and content as follow:
type %%x
rem echo. is to change line
echo.
set /a num = num + 1
}
echo totally we get %x% files.


for [%var|%%var] in (set) do command 

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

@echo off
setlocal
path = d:\
echo the local path variable is:
set path
endlocal

echo the system path variable is:
set path



setlocal {enableextension|diasbleextension} {enabledelayedextension|diabledelayedextension}

endlocal

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

shift [/n]

rem test this by shift.bat a.txt b.txt
@echo off

:round
if "%1" == "" goto end
echo %1 's content as follow:
type %1
echo.
shift

goto round

:end


  12. 通配符 * 和 ?

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

@echo off
echo type a.txt > b.bat
echo dir /b /w >> b.bat

echo the content of the b.bat is:
type b.bat

echo the b.bat after sorting:
sort < b.bat

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

@echo off 

find "key" in a.txt | sort > b.txt

echo the result is:
type b.txt

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值