/a 在批处理文件中的重要性

Importance of /a in a batch file

首先,如果问题很简单,我想道歉,但我很难理解批处理文件中的这段代码。

下面代码中的 /a 是什么,它的重要性和作用是什么?

if %first%==1 set /a id=(%3-1) * 3

这里我理解 id 是存储值的变量,%first% 是我从这个变量中获取值,但我无法理解 /a.

感谢您的宝贵时间

您可以通过在命令提示符下键入以下内容来获得有关大多数命令的帮助:commandName /?

例如

set /?
for /?

http://www.computerhope.com/msdos.htm是一个很好的dos命令在线帮助。

set /a ...表示算术表达式。

参考 Set:

Arithmetic expressions (SET /a)

The expression to be evaluated can include the following operators:

   +   Add                set /a "_num=_num+5"
   +=  Add variable       set /a "_num+=5"
   -   Subtract (or unary)set /a "_num=_num-5"
   -=  Subtract variable  set /a "_num-=5"
   *   Multiply           set /a "_num=_num*5"
   *=  Multiply variable  set /a "_num*=5"
   /   Divide             set /a "_num=_num/5"
   /=  Divide variable    set /a "_num/=5"
   %   Modulus            set /a "_num=5%%2"
   %%= Modulus            set /a "_num%%=5" 
   !   Logical negation  0 (FALSE) ⇨ 1 (TRUE) and any non-zero value (TRUE) ⇨ 0 (FALSE)
   ~   One's complement (bitwise negation) 
   &   AND                set /a "_num=5&3"    0101 AND 0011 = 0001 (decimal 1)
   &=  AND variable       set /a "_num&=3"
   |   OR                 set /a "_num=5|3"    0101 OR 0011 = 0111 (decimal 7)
   |=  OR variable        set /a "_num|=3"
   ^   XOR                set /a "_num=5^3"    0101 XOR 0011 = 0110 (decimal 6)
   ^=  XOR variable       set /a "_num=^3"
   <<  Left Shift.    (sign bit ⇨ 0)
   >>  Right Shift.   (Fills in the sign bit such that a negative number always remains negative.)
                       Neither ShiftRight nor ShiftLeft will detect overflow.
   <<= Left Shift variable     set /a _num<<=2
   >>= Right Shift variable    set /a _num>>=2

  ( )  Parenthesis group expressions  set /a "_num=(2+3)*5"
   ,   Commas separate expressions    set /a "_num=2,_result=_num*5"

...

Arithmetic expressions (SET /a)

Placing expressions in "quotes" is optional for simple arithmetic but required for any expression using logical operators.

Any SET /A calculation that returns a fractional result will be rounded down to the nearest whole integer.

Examples:

SET /A "_result=2+4" (=6)

SET /A "_result=5" (=5)
SET /A "_result+=5" (=10)

SET /A "_result=2<<3" (=16) { 2 Lsh 3 = binary 10 Lsh 3 = binary 10000 = decimal 16 }

SET /A "_result=5%%2" (=1) { 5/2 = 2 + 2 remainder 1 = 1 }

In a batch script, the Modulus operator (%) must be doubled up to (%%).

SET /A will treat any character string in the expression as an environment variable name. This allows you to do arithmetic with environment variables without having to type any % signs to get the values.

SET /A _result=5 + _MyVar

Multiple calculations can be performed in one line, by separating each calculation with commas, for example:

_year=1999 Set /a _century=_year/100, _next=_century+1

The numbers must all be within the range of 32 bit signed integer numbers (-2,147,483,648 through 2,147,483,647) to handle larger numbers use PowerShell or VBScript. Leading Zero will specify Octal

Numeric values are decimal numbers, unless prefixed by 0x for hexadecimal numbers, 0 for octal numbers.

So 0x10 = 020 = 16 decimal

The octal notation can be confusing - all numeric values that start with zeros are treated as octal but 08 and 09 are not valid octal digits. For example SET /a _month=07 will return the value 7, but SET /a _month=09 will return an error.

有关其他 Windows 命令行命令的信息,请参阅 An A-Z Index of the Windows CMD command line