在批处理脚本中处理八进制变量 [英] dealing with octal variables in batch script

查看:106
本文介绍了在批处理脚本中处理八进制变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个批处理脚本来获取当前日期并对其执行数字运算.

Hi I am running a batch script to get a current date and perform numerical operations on it.

我使用以下命令获取日期,然后对其进行操作(加,减等).

I get the date by using the following command and then do operations on it (Add, Subtract etc).

但是如果日期返回的值小于10(例如09、08),则操作会出错.

But If the date returns a value less than 10 (eg. 09, 08) the operation gives an error.

set dd=%date:~7,2%   
set /a dd1=08-1 
Invalid number.  Numeric constants are either decimal (17),hexadecimal (0x11), or octal (021).

请帮助

推荐答案

您可以使用一些技巧来删除零:

you can remove the zero with a little trick:

set /a dd1=(1%dd: =0%-100)-1

这会将字符串"1"添加到"08"(仍然是字符串),结果为"108".然后减去"100"(/a将其视为数字),结果为"8".如果在您的语言环境中日期没有前导零,而是空格,那么%%d: =0%会将其替换为零

This adds the string "1" to "08" (which is also still a string), resulting in "108". Then subtract "100" (/a treating them as numbers), which results in "8". If in your locale the date has no leading zero but a space instead, %%d: =0% replaces it with a zero

如果您需要前导零的结果,只需再次添加即可:

If you need the result with leading zero, just add it again:

set dd1=0%dd1%
set dd1=%dd1:~-2%

这会将字符串"0"添加到字符串"7"(从之前的结果开始)之前,结果为"07",并从字符串"07"中获取最后两位数字(如果之前的结果为"24,->添加" 0"=" 024->最后两个=" 24)

This adds the string "0" in front of the string "7" (result from before), resulting in "07" and takes the last two digits from it "07" (in case, the result from before is "24", -> add "0" = "024" -> last two = "24")

已编辑,以便在日期中有空格而不是前导零的区域设置中也可以使用. (感谢LưuVĩnhPhúc发现了它)

edited to work also in locales, where the date has a space instead of a leading zero. (thanks to Lưu Vĩnh Phúc for spotting it)

这篇关于在批处理脚本中处理八进制变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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