CMD:无法使用+ =运算符将1加到08 [英] CMD: Can't add 1 to 08 using += operator

查看:100
本文介绍了CMD:无法使用+ =运算符将1加到08的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CMD命令来处理某些变量.我可以为变量添加1,但是如果该变量为08或09,则结果为1,而不是9或10.这是代码:

I am using CMD commands to work on some variables. I can add 1 to a variable but if that variable is 08 or 09, the result comes out to be 1,rather than 9 or 10. Here's the code:

setlocal EnableDelayedExpansion
set day=08
echo Day:%day%
set /a day+=1
echo Day:% day%

这适用于数字1-7和10之后的数字.我需要在小于10的数字上添加前导零,因为否则尽管显示了正确的值,但由于某种原因,该变量仍未写入文本文件(稍后将执行)在代码的其他部分.

This works for numbers 1-7, and after 10. I need to add a leading zero to digits less than 10 because otherwise somehow the variable is not written to a text file (which I perform later) despite showing the correct value in other parts of the code.

推荐答案

命令 SET 的帮助可以通过在命令提示符窗口set /?中运行来阅读.它说明以0开头的数字被解释为八进制数字,请参见函数 strtol Windows命令解释器内部使用的 base 0(自动检测).

Help for command SET can be read by running in a command prompt window set /?. It explains that numbers with a leading 0 are interpreted as octal numbers, see function strtol with base being 0 (automatic detection) as used internally by Windows command interpreter.

0809在八进制系统中无效,因此将其解释为值0.

08 and 09 are invalid in octal number system and are therefore interpreted with value 0.

这种情况的一种解决方案是,在预处理状态期间首先使用字符串连接,将字符串08更改为108,然后将99从转换为整数的108中减去,以获得08以及0131递增1.

One solution for this case is using first a string concatenation during preprocessing state to change the string 08 to 108 and subtract 99 from 108 converted to an integer number to get 08 and 09 as well as 01 to 31 incremented by 1.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "day=08"
echo Day: %day%
set /A day=1%day% - 99
echo Day: %day%
endlocal

上述批处理文件的输出为:

The output of above batch file is:

Day: 08
Day: 9

这里不需要延迟的环境变量扩展,并且通常在算术表达式中根本不需要扩展,也可以通过命令 SET 进行解释.当然也有例外,例如必须先执行字符串连接,然后才能在命令块中的环境变量已在其上定义或修改的命令块中执行算术表达式之前执行.但是这些用例确实很少见.

Delayed environment variable expansion is not needed here and in general not needed at all in an arithmetic expression as also explained by help of command SET. There are of course exceptions like a string concatenation must be done first before executing the arithmetic expression within a command block on which the environment variable was defined or modified before in the command block. But those use cases are really rare.

对于小于两位数的数字,如果结果应再次以前导0开头,则最好不要减去99,而应加1并在下一步中仅将最后两位分配给环境变量算术表达式求值后,字符串中始终有3位数字.

When the result should be again with a leading 0 for numbers with less than two digits, it is best not subtracting 99, but adding 1 and assign to the environment variable in next step just the last two digits from the string with always 3 digits after evaluation of arithmetic expression.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "day=08"
echo Day: %day%
set /A day=1%day% + 1
set "day=%day:~-2%"
echo Day: %day%
endlocal

上述批处理文件的输出为:

The output of above batch file is:

Day: 08
Day: 09

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • set /?
  • setlocal /?
  • echo /?
  • endlocal /?
  • set /?
  • setlocal /?

这篇关于CMD:无法使用+ =运算符将1加到08的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆