bat脚本知识点总结
1.在bat里调用ps1
powershell -command Set-ExecutionPolicy -Scope CurrentUser "bypass"
powershell -command get-executionpolicy
powershell -command ./apk-package/pollingapk.ps1 -polling 1 -deviceid %DEVICE_SERIAL%
2. bat 函数
:test
echo "hello world"
GOTO:EOF
调用 call:test
3. if elseif else
if %EXCUTE_OPTION% EQU 1 (
echo "hello world"
) ^
else if %EXCUTE_OPTION% EQU 2 (
echo "all"
) ^
else (
echo "nothing"
)
4.批处理标签函数 不使用call调用也会执行,最后把所有函数放在最后, 在调用完执行后使用pause暂停或者EXIT退出
5.注释问题
:: hello world 或者*.bat 12 12 12 错误
带空格需引号括起来,否则会报错 :: rem 同样
::"hello world 或者*.bat 12 12 12" 正确
6.循环
for /l %%i in (1,1,100) do (
echo "now circle num:" %%i
timeout /T 2 /NOBREAK
)
7.计算耗时
set /a StartS=%time:~6,2%
set /a StartM=%time:~3,2%
set /a startH=%time:~0,2%
echo 开始时间:%time%
call:your.bat
set /a EndS=%time:~6,2%
set /a EndM=%time:~3,2%
set /a EndH=%time:~0,2%
echo 结束时间:%time%
set /a diffS_=%EndS%-%StartS%
set /a diffM_=%EndM%-%StartM%
set /a diffH_=%EndH%-%StartH%
set /a cost=%diffH_%*3600 + %diffM_%*60 + %diffs_%
echo 总共花费时间 : %cost% 秒
8. 批处理字符串,变量延时扩展,字符串截取中含有变量时处理办法
for /l %%n in (1,1,%num%) do (
set /a startNum=%%n-1
call set opt_current=%%EXCUTE_OPTION:~!startNum!,%%n%%
)
9. if 多个条件时,只能临时这样处理
if %%n == 1 if "!opt_current!" EQU "1" set fsctoryFlag=1
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效