我有一个bat文件,用于检查23:00时间,并在23:00之后执行一些其他步骤。
批次在稍后启动到10:00 AM
时确实有效。
但是在09:00 AM
运行时同样的蝙蝠不起作用并抛出如下错误:
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).
我发现这个bat文件只能用于两位数小时而不是单个数字小时,例如1,2,3,4,5,6,7,8,9
。
代码:
@For /F "tokens=1,2,3,4 delims=/-: " %%A in ('time /t') do @(
Set Hour=%%A
Set Minute=%%B
)
echo current hour is %Hour%
echo current Minute is %Minute%
set /A remaining_hour=23-%Hour%
set /A remaining_minutes=60-%Minute%
echo Remaining hours to reach 23:00 is %remaining_hour%
echo Remaining Minutes to reach 23:00 is %remaining_minutes%
你能帮我摆脱错误信息吗?
根据我在尝试测试bat文件但我的系统时间更改为上午09:00时仍然无效的建议修改了下面的代码
修改后的代码1:
@echo off
@For /F "tokens=1,2,3,4 delims=/-: " %%A in ('time /t') do @(
Set Hour=%%A
for /F "tokens=* delims=0" %%N in ("%Hour%") do set "Hour=%%N" & set /A "Hour+=0"
Set Minute=%%B
)
setlocal EnableDelayedExpansion
echo current hour is %Hour%
echo current Minute is %Minute%
set /A remaining_hour=23-%Hour%
set /A remaining_minutes=60-%Minute%
echo Remaining hours to reach 23:00 is %remaining_hour%
echo Remaining Minutes to reach 23:00 is %remaining_minutes%
set /A sleep_Minutes=((remaining_minutes+(60*remaining_hour))*60)
echo Total sleep time is %sleep_Minutes%
pause
答案 0 :(得分:3)
set /A
命令将前面带零的数字解释为八进制数 - 请参阅set /?
:
数字值是十进制数,除非前缀为0x(十六进制数字),0(八进制数字)。因此0x12与18相同,与022相同。请注意,八进制表示法可能令人困惑:08和09不是有效数字,因为8和9不是有效的八进制数字。
您可以通过删除Hour
值中的前导零来轻松解决此问题:
for /F "tokens=* delims=0" %%N in ("%Hour%") do set "Hour=%%N" & set /A "Hour+=0"
for /F "tokens=* delims=0" %%N in ("%Minute%") do set "Minute=%%N" & set /A "Minute+=0"
这是上面实现的完整代码:
@for /F "tokens=1,2,3,4 delims=/-: " %%A in ("%TIME%") do @(
for /F "tokens=* delims=0" %%N in ("%%A") do set "Hour=%%N" & set /A "Hour+=0"
for /F "tokens=* delims=0" %%N in ("%%B") do set "Minute=%%N" & set /A "Minute+=0"
)
echo current hour is %Hour%
echo current Minute is %Minute%
set /A remaining_hour=23-Hour
set /A remaining_minutes=60-Minute
echo Remaining hours to reach 23:00 is %remaining_hour%
echo Remaining Minutes to reach 23:00 is %remaining_minutes%