基本语法
删除目录下所有文件
@ECHO OFF DEL . DR
列出文件列表输入到文件
@echo off dir "C:Program Files" >b.txt pause
设置变量SET
@echo off set message=Hello World echo %message% pause
局部变量&全局变量,SETLOCAL 和ENDLOCAL之间包裹着局部变量
@echo off set globalvar=5 SETLOCAL set var=13145 set /A var=%var% + 5 /A 用在声明数字时 echo %var% echo %globalvar% ENDLOCAL
系统环境变量
@echo off echo %JAVA_HOME%
字符串
set message=Hello World 创建字符串 Set a= 创建空字符串 检验字符串是否为空 @echo off SET a= SET b=Hello if [%a%]==[] echo "String A is empty" if [%b%]==[] echo "String B is empty " 字符串拼接 @echo off SET a=Hello SET b=World SET /A d=50 SET c=%a% and %b% %d% echo %c% 转变为数字类型 @echo off set var=13145 set /A var=%var% + 5 echo %var% 取前五个字符 @echo off set str=Helloworld echo %str% Helloworld set str=%str:~0,5% Hello echo %str% 字符串替换 @echo off set str=Batch scripts is easy. It is really easy. echo %str% set str=%str:is=% echo %str% 去掉空格 @echo off set str=This string has a lot of spaces echo %str% set str=%str:=% echo %str% Thisstringhasalotofspaces