Gus*_*avo 3 windows command-line
在 Windows 命令行中,我可以通过执行以下操作来打印当前日期的年份:
echo %date:~10,4%
Run Code Online (Sandbox Code Playgroud)
结果:2016
当然,根据您的语言中日期的显示方式,您需要更改10
或4
以获得正确的子字符串。但没关系。
我想要它打印的是2015
, 即2016 - 1
. 那么,如何从刚刚捕获的子字符串中减去 1 呢?使用辅助变量没有问题。
正如您所说,使用另一个变量没有问题,您可以按如下方式执行此操作(我的环境设置使用不同的子字符串)
\nset /a "yearminus1=%date:~6,10%-1"\n
Run Code Online (Sandbox Code Playgroud)\n\n对于你来说,我认为这将是set /a "yearminus1=%date:~10,4%-1"
,但我无法测试这一点。
使用/a
开关 withset
指定您将给出一个要计算的表达式,而不是仅将表达式按原样存储在变量中。
可以使用以下运算符:
\n + Add set /a "_num=_num+5"\n += Add variable set /a "_num+=5"\n - Subtract (or unary)set /a "_num=_num-5"\n -= Subtract variable set /a "_num-=5"\n * Multiply set /a "_num=_num*5"\n *= Multiply variable set /a "_num*=5"\n / Divide set /a "_num=_num/5"\n /= Divide variable set /a "_num/=5"\n %% Modulus set /a "_num=5%%2"\n %%= Modulus set /a "_num%%=5" \n ! Logical negation 0 (FALSE) \xe2\x87\xa8 1 (TRUE) and any non-zero value (TRUE) \xe2\x87\xa8 0 (FALSE)\n ~ One\'s complement (bitwise negation) \n & AND set /a "_num=5&3" 0101 AND 0011 = 0001 (decimal 1)\n &= AND variable set /a "_num&=3"\n | OR set /a "_num=5|3" 0101 OR 0011 = 0111 (decimal 7)\n |= OR variable set /a "_num|=3"\n ^ XOR set /a "_num=5^3" 0101 XOR 0011 = 0110 (decimal 6)\n ^= XOR variable set /a "_num=^3"\n << Left Shift. (sign bit \xe2\x87\xa8 0)\n >> Right Shift. (Fills in the sign bit such that a negative number always remains negative.)\n Neither ShiftRight nor ShiftLeft will detect overflow.\n <<= Left Shift variable set /a "_num<<=2"\n >>= Right Shift variable set /a "_num>>=2"\n\n ( ) Parenthesis group expressions set /a "_num=(2+3)*5"\n , Commas separate expressions set /a "_num=2,_result=_num*5"\n
Run Code Online (Sandbox Code Playgroud)\n从这里。
\n