我有一个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 AM
修改代码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
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%