所有文本处理为UTF8:
REM 声明采用UTF-8编码
chcp 65001
设置整个窗口背景和字体颜色:
color 背景色+字体色
color help //查看
例如:
color 07
表示黑底白字,0表示黑 7表示白
关闭语句执行本身的打印:
方法1:文件开始时写上:@echo off
方法2:每个语句前面加上@,或>nul或>nul 2>nul
延迟变量:用户定义变量
setlocal enabledelayedexpansion
窗口大小:
mode con cols=30 lines=20
注意:这里会修改缓冲区大小变为20,因此要使用修改缓冲区方法来处理一次
缓冲区大小:
powershell.exe -command "& {$pshost = Get-Host;$pswindow = $pshost.UI.RawUI;$newsize = $pswindow.BufferSize;$newsize.height = 1500;$pswindow.buffersize = $newsize;}"
注意:这里的大小为1500,可以调整此值实现;bat命令本身没有可以单独设置缓冲区大小的命令,因此只能用powershell方式;也可以使用其它手动设置方法;
设置窗口名称:
title 窗口名称
注意:
1.所有路径最好使用双引号,否则会有很多奇怪的报错
2.所有路径最好用'\'而不要用'/',因为最后一个是文件的话会索引不到
查看端口连接数:
netstat -an |find /c ":80"
查看端口占用的程序:
1.netstat -aon|findstr "49157" //观察最后一个ID,如2720
2.tasklist|findstr "2720"
例:
@echo off
setlocal enabledelayedexpansion
set findport=80
for /f "tokens=4,5" %%i in ('netstat -aon^|findstr "%findport%"') do (
if %%i neq 0 (
for /f %%k in ('tasklist^|findstr "%%i"') do echo %%k
)
if %%j neq 0 (
for /f %%k in ('tasklist^|findstr "%%j"') do echo %%k
)
)
打开到当前执行(bat、exe)路径目录:
cd /d %~dp0
其中:/d表示会转换盘符
获取当前路径:
1.%cd%:表示当前cd到的目录位置
2.%~dp0表示当前bat脚本或exe所在的目录位置
for参数说明:
例1:for /f "tokens=4,5" %%i in ('netstat -aon^|findstr "%findport%"') do ()
例2:for /F "usebackq skip=4 tokens=2,4" %%i in (`"netstat -ano -p UDP"`) do ()
/f:表示得到字符串
eol:忽略以某字符开头的行
skip:忽略前N行
usebackq:in()中出现:双引号(当文件路径或名称中有空格时,用双引号括起来,如果不用usebackq则表示字符串),单引号(表示字符串,即'string',如果不用usebackq则表示执行命令),后引号(表示命令执行,即`command`)
delims:切分符,相当于split
tokens:取得切分后的某一项
in ():里面可以是字符串、文件名、命令
注意:
1.in ()内部把指令用单引号括起来,如果里面有|等特殊指令,前面加转义符^,如:in ('tasklist^|findstr "%%i"')
比较符:
== 等于
EQU - 等于
NEQ - 不等于
LSS - 小于
LEQ - 小于或等于
GTR - 大于
GEQ - 大于或等于
返回值:
%errorlevel%
例子1:
go build -tags lua54 %~dp0\cmd\gamemgr\
if %errorlevel% neq 0 (
pause
)
例2:
a.bat
exit /b 123
b.bat
call a.bat
echo %errorlevel%
注意:exit中的参数 /b :指定要退出当前批处理脚本而不是 CMD.EXE。
例子3:
(注意,for后面的语句里如果要使用变量则用!!来访问,如:!tmp!)
@set b2=""
@for /f "delims=" %%i in ('call bat2.bat') do @(
@set "b2=%%i"
echo !b2!
)
echo %b2%
bat2.bat:
@echo off
set b1=abcd bacsd
echo %b1%
保留崩溃堆栈:
run_battleserver.bat:
title battleserver
call battleserver.exe
pause
runbat.bat:
start /I run_battleserver.bat
延时:
方法一:具有倒计时提示
timeout /nobreak /t 2
方法二:
choice /t 1 /d y /n >nul
其中:1表示1秒
启动服务器:
方法一:
一定要有两个脚本才可以:
脚本1:start /I %root_dir%\server\run.bat GameCenter.exe
脚本2(run.bat):call %root_dir%\%1
方法2:(其中,start /B 为后台模式)
cmd /C start /I gamemgr.exe
判断文件夹是否存在:
if not exist c:\folder mkdir c:\folder
如果不存在c:\folder,创建它。
判断文件是否存在:
if not exist ./gamemgr_cmd.txt (cd . >gamemgr_cmd.txt)
如果不存在gamemgr_cmd.txt,创建它。
删除目录:
if exist c:\folder rd c:\folder /s/q
如果存在c:\folder,删除它。
删除文件:
del /s /q ./test.txt
del ./test.txt /a/f/q
字符串判断:
if "%two%"=="" (echo null&&goto start) else (echo %2)
拷贝文件夹(所有文件和文件夹):
xcopy /S "%path1%" "%path2%"
拷贝文件:
方法一(有时候不行):copy /y "%path1%" "%path2%" > nul
方法二:xcopy /f/y "../../src/cmd/battleserver/*.exe" "./"
拷贝文件夹下所有文件(不包括文件夹):
copy /y "%path1%/" "%path2%/"
遍历文件夹下所有文件:
:: %%~nxi只显示文件名,%%i显示带路径的文件信息,%%~ni去掉后缀只有文件名,/a-d:遍历文件和文件夹,/a:a:只遍历文件, /s:一次性显示所有文件和文件g
for /f "delims=" %%i in ('dir /b /a-d /s "%input%"') do echo %%i
查找包含某个关键字的文件名:
set curpath=.
set name=product64
for /f "delims=" %%i in ('dir /a-d /b /on %curpath%\*%name%*') do ( echo %%i )
查找以点(".")开头的文件或文件夹:
文件夹:for /f "delims=" %%i in ('dir .* /ad/b/s') do echo %%i
文件:for /f "delims=" %%i in ('dir .* /a-d/b/s') do echo %%i
注意:上面的/s是表示循环遍历子文件夹
显示当前(.)文件夹下最新文件名:
for /f "delims=" %%i in ('dir /o-d /tc /b .') do (
echo %%i
goto end
)
:end
比较文件内容是否相同:
type castlevania.sql > tmp1.txt
type castlevania2.sql > tmp2.txt
fc tmp1.txt tmp2.txt >nul
if %errorlevel% equ 0 (
echo 相同
) else (
echo 不同
)
杀进程:
根据进程名杀进程:taskkill /f /im "ServerMaintain.exe"
根据关键字杀进程:for /f "tokens=2 delims=," %%a in ('tasklist /v /fo:csv /nh ^| findstr /r "battleserver"') do taskkill /pid %%a
根据关键字杀CMD进程:for /f "tokens=2 delims=," %%a in ('tasklist /fi "imagename eq cmd.exe" /v /fo:csv /nh ^| findstr /r "battleserver"') do taskkill /f /t /pid %%a
根据title杀进程(比如有脚本执行过title runserver):taskkill /f /t /fi "imagename eq cmd.exe" /fi "windowtitle eq runserver"
判断进程是否存在:
例一:
tasklist|findstr "redis" > nul
if %errorlevel% == 0 (
redis-cli -p 6379 -a 123456 shutdown
echo has shutdown redis!
)
例二:循环判断
set /a nTag=0
:check
if %nTag%==1 (
set /a nTag=0
timeout /nobreak /t 2
)
set /a nTag=1
echo check...
for /f "tokens=2 delims=," %%a in ('tasklist /fi "imagename eq cmd.exe" /v /fo:csv /nh ^| findstr /r "battleserver"') do (echo wait battleserver && goto check)
将SQL语句执行的结果输出到文件:
mysql -h10.10.10.10 -uroot -p123456 -P3306 -Ne "use test; select * from tb_test;" > /tmp/rs.txt
输入:
set /p input=please input:
echo %input%
逐行读取文件内容并修改(必须要有延迟变量):
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('type A.txt') do ( set "str=%%a"
rem 222替换为444 set "str=!str:222=444!" echo !str! >>a.txt.change )
打印特殊字符:
开启延时变量后,打印特殊字符会打印不了,比如!号,可在特殊符号前加^^来解决
例:
echo start^^!^^!^^!^^!^^!
for循环:
@echo off
setlocal ENABLEDELAYEDEXPANSION
set /a sum=0
set /a ii=0
for /l %%i in (1,1,100) do (
echo %%i
set /a ii+=1
set /a sum+=!ii!
rem 或者:set /a sum+=%%i
)
echo !sum!
pause
变量累加(必须要有延迟变量):
setlocal enabledelayedexpansion
for /f "skip=1 delims=, tokens=1*" %%a in ('type A.txt') do (
set "str=%%a" set /a j+=1
if !j! equ 1 (set keys=!str!) else (goto end) )
:end
替换显示本行内容:
@echo off
rem // Enable delayed expansion which is necessary to output carriage-return characters later:
setlocal EnableDelayedExpansion
echo start^^!^^!^^!^^!^^!
rem // Retrieve a carriage-return character:
for /F %%C in ('copy /Z "%~f0" nul') do set "CR=%%C"
rem // Retrieve a back-space character:
for /F %%C in ('echo prompt $H ^| cmd') do set "BS=%%C"
for /l %%i in (1,1,5) do (
set /a x+=1
rem // Prepare message text and a string of spaces:
set "STRING=!x! Press any key to continue . . ."
set "SPACES=
rem // Use `set /P` to print message text without trailing line-break (carriage-return + line-feed): "
set /P ="!STRING!" < nul
rem // Do `pause` but suppress its message text:
pause > nul
rem /* Use `set /P` to first print an invisible back-space character; this avoids a white-space
rem character to appear first to `set /P` as it would not output such leading characters;
rem then print a single carriage-return character to move the cursor to the beginning of the
rem current line; then a string of spaces onto the previous message text to overwrite and so to
rem clear it; append a single carriage-return character, so any other text is printed onto it;
rem since there is no trailing line-break, any further text is printed at the same line: */
set /P ="!BS!!CR!!SPACES!!CR!" < nul
)
echo %STRING%
endlocal
echo done
pause
分割字符串:
@echo off
setlocal ENABLEDELAYEDEXPANSION
set teststring=The;rain;in;spain
:stringloop
if "!teststring!" EQU "" goto END
for /f "delims=;" %%a in ("!teststring!") do (
set substring=%%a
echo !substring!
)
:striploop
set stripchar=!teststring:~0,1!
set teststring=!teststring:~1!
if "!teststring!" EQU "" goto stringloop
if "!stripchar!" NEQ ";" goto striploop
goto stringloop
:END
endlocal
pause
例2:
set aa=asdfasdf asdfad asdfd
for %%a in (%aa%) do (
echo %%a
)
判断语句的写法:
@echo off setlocal EnableDelayedExpansion set option=2 set sum=-1 if %option% == 3 ( echo three set /a sum=%option%*%option%*%option% ) else if %option% == 2 ( echo two set /a sum=2*%option% ) ^ else ( echo zero set /a sum=0 ) echo sum = !sum! pause
netsh命令:
查看网络设置状态:netsh interface ip show config
设置动态IP(DHCP自动获取IP):
netsh interface ip set address "本地连接" dhcp 或 netsh interface ip set address name="本地连接" source=dhcp
设置指定的IP:(此处以设置本机IP为10.16.15.226,子网掩码为255.255.255.0,网关IP为10.16.15.1为例)
netsh interface ip set address "本地连接" static 10.16.15.226 255.255.255.0 10.16.15.1 或 netsh interface ip set address name="本地连接" source=static addr=10.16.15.226 mask=255.255.255.0 gateway=192.168.10.1
设置动态DNS:(DHCP自动获取DNS)
netsh interface ip set dns "本地连接" dhcp
设置指定的DNS(此处以设置DNS为210.45.240.10为例)
netsh interface ip set dns "本地连接" static 210.45.240.10
查看防火墙设置状态:netsh firewall show state
禁用系统防火墙:netsh firewall set opmode disable
启用防火墙:netsh firewall set opmode enable
设置防火墙规则示例:netsh advfirewall firewall add rule ?
添加一条防火墙规则:netsh firewall add allowedprogram C: \ MyApp \ MyApp.exe "My Application" ENABLE