bat 批处理(NASI 编码格式)
1 @echo off
2 chcp 65001 3 echo 开始测试 4 echo 开始 ping 192.168.1.133 5 call p1.133.bat 6 echo 开始 ping 192.168.1.161 7 call p1.161.bat 8 echo 完成 9 pause
@echo off :以下内容默认不显示
chcp :切换编码格式;65001==utf-8 ; 936 == 默认GPK ; 437 == 美式英语
call :调用另外一个bat文件(直接写另外一个bat文件,回不来)
pause:按任意键结束(暂停)
1 @echo off 2 3 chcp 936 4 echo start NO.1-2 ping 192.168.1.133 5 set startime=%DATE%%time% 6 echo start=%startime% >> d:/pinglog/ptest01.txt 7 ping 192.168.1.133 >> d:/pinglog/ptest01.txt 8 set endtime=%DATE%%time% 9 echo end=%endtime% >> d:/pinglog/ptest01.txt 10 echo /n >> d:/pinglog/ptest01.txt 11 echo -------------------------------------------- >> d:/pinglog/ptest01.txt 12 chcp 65001
chcp 936 : 切换为默认GPK格式
set :设置环境变量
%DATE%日期
%time%时间
@echo off dir c:\*.* >d:a.txt echo 你好 pause
dir c:\*.* >d:a.txt : 获取c盘文件列表
set name=%time:~0,8% echo %name% >> ./test.txt
0 == 舍弃前零位;8 == 取前8位
----------------------------------------------------------------------
字符串操作:
截取字符串:
@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%
————————————————
版权声明:本文为CSDN博主「Walker_fire」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qwe15954250805/article/details/80290062
替换字符串:
@echo off
set aa=你好!世界
echo 替换前:%aa%
echo 替换后:%aa:世界=中国%
echo aa=%aa%
echo %aa%
set aa=%aa:世界=中国%
echo aa=%aa%
合并字符串
@echo off
set aa=你好!
set bb=世界
echo %aa%%bb%
echo aa=%aa%
echo bb=%bb%
set "aa=%aa%%bb%"
echo aa=%aa%
----------------------------------------------------------------------
字符串操作参看:
https://blog.csdn.net/qwe15954250805/article/details/80290062?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164022976316780261910570%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=164022976316780261910570&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~sobaiduend~default-1-80290062.nonecase&utm_term=bat%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%93%8D%E4%BD%9C&spm=1018.2226.3001.4450
调用另一个bat文件参看:
https://blog.csdn.net/u013250071/article/details/115180312?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164022287816780274132266%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=164022287816780274132266&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-2-115180312.pc_search_result_cache&utm_term=bat%E5%BE%AA%E7%8E%AF%E6%89%A7%E8%A1%8C%E5%8F%A6%E4%B8%80%E4%B8%AAbat&spm=1018.2226.3001.4187
乱码参看:
https://blog.csdn.net/u013948858/article/details/81036079?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164022322316780271938504%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=164022322316780271938504&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~sobaiduend~default-2-81036079.nonecase&utm_term=bat%E8%84%9A%E6%9C%AC%E4%B8%AD%E6%96%87%E6%98%BE%E7%A4%BA%E4%B9%B1%E7%A0%81&spm=1018.2226.3001.4450