命令列表
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
第一个批处理脚本
@echo off Rem This is for listing down all the files in the directory Program files dir "C:Program Files" > C:lists.txt echo "The program has completed"
@echo off echo %1 echo %2 echo %3
执行效果如下:
语法
set [/A] variable-name=value
- variable-name
- value
- /A
字符串
@echo off set message=Hello World echo %message%
数字
@echo off SET /A a=5 SET /A b=10 SET /A c=%a% + %b% echo %c%
@echo off set globalvar=5 SETLOCAL set var=13145 set /A var=%var% + 5 echo %var% echo %globalvar% ENDLOCAL echo %var% echo %globalvar%
执行效果如下:
可以看出局部变量在执行
@echo off rem 这是一个注释 ::这也是一个注释
@echo off set abc=hello world, this string come from bat echo 原字符串为:%abc% echo 截取前5个字符:%abc:~0,5% echo 截取最后5个字符:%abc:~-5% echo 截取第一个到倒数第6个字符:%abc:~1,-5% echo 从第4个字符开始截取5个字符:%abc:~3,5% echo 从倒数第14个字符开始截取5个字符:%abc:~-14,5%
@echo off SET /A a=5 SET /A b=10 SET /A c=%a% + %b% if %c%==15 echo "The value of variable c is 15" if %c%==10 echo "The value of variable c is 10"
@echo off SET /A a=5 SET /A b=10 SET /A c=%a% + %b% if %c%==15 (echo "The value of variable c is 15") else (echo "Unknown value") if %c%==10 (echo "The value of variable c is 10") else (echo "Unknown value")
if defined
@echo off SET str1=String1 SET str2=String2 if defined str1 echo "Variable str1 is defined" if defined str3 (echo "Variable str3 is defined") else (echo "Variable str3 is not defined")
if exists
@echo off if exist C:set2.txt echo "File exists" if exist C:set3.txt (echo "File exists") else (echo "File does not exist")
@echo off SET /A a=5 SET /A b=10 if %a%==5 if %b%==10 echo "The value of the variables are correct"
if errorlevel
@echo off ping baidu.com if errorlevel 0 echo "ping baidu.com ok" if errorlevel 1 echo "ping baidu.com not ok" ping dsadsadasdsadsadqgt3432.com if errorlevel 0 echo "ping dsadsadasdsadsadqgt3432.com ok" if errorlevel 1 echo "ping dsadsadasdsadsadqgt3432.com not ok"
@echo off SET /A a=5 if %a%==5 goto :labela if %a%==10 goto :labelb :labela echo "The value of a is 5" exit /b 0 :labelb echo "The value of a is
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
% |
|
|
@echo off SET /A a=5 SET /A b=10 SET /A c=%a%+%b% echo %c% SET /A c=%a%-%b% echo %c% SET /A c=%b%*%a% echo %c% SET /A c=%b%/%a% echo %c% rem 备注:发现模运算符在批处理文件里面是%% SET /A c=%b% %% %a% echo %c%
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@echo off set /A a=5 set /A b=6 if not %a% EQU %b% (echo "yes") else (echo "no") if %a% NEQ %b% (echo "yes") else (echo "no") rem 其他的也类似..
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@echo off SET /A a=5 SET /A a+=5 echo %a% SET /A a-=5 echo %a% SET /A a*=5 echo %a% SET /A a/=5 echo %a% SET /A a%=5 echo %a%