批處理基礎

基本語法
刪除目錄下全部文件spa

 

@ECHO OFF 
DEL . 
DR

 

列出文件列表輸入到文件code

 

@echo off
dir "C:\Program Files" >b.txt
pause

設置變量SETblog

@echo off 
set message=Hello World 
echo %message%
pause

局部變量&全局變量,SETLOCAL 和ENDLOCAL之間包裹着局部變量ip

@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%

字符串string

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
相關文章
相關標籤/搜索