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