1. 变量的定义与删除
定义变量:
set var = abc
set /a var = expression
set /p var = string
删除变量:
set var =
字符串连接:
%var%%var%
如何将命令的执行结果赋值给变量?
for /f %i in ( 'ipconfig /all ^| find /i "ipv4" ') do set yourvar=%i
for /f "tokens=*" %i in ('ipconfig /all^|find /i "ipv4" ') do set yourvar=%i
for /f "tokens=2 delims=:(" %i in ('ipconfig /all^|find /i "ipv4" ') do set yourvar=%i
for /f "delims=" %i in ( 'ipconfig /all ^| find /i "ipv4" ') do set yourvar=%i
又如,
for /f "tokens=3 delims= " %i in ('dir desktop ^|find /i "emmcboot.mbn"') do set yourvar=%i
将desktop目录下的emmcboot.mbn文件大小赋值给变量yourval
2. 截取date time 显示年月日时分
set ThisDay=%date:~0,4%%date:~5,2%%date:~8,2%
rem 获取时间
if "%time:~0,2%" lss "10" (
set ThisTime=0%time:~1,1%%time:~3,2%
) else (
set ThisTime=%time:~0,2%%time:~3,2%
)
set Now=%ThisDay%%ThisTime%