China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-12 16:01
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to fix “invalid number” when running a batch file View 708 Replies 2
Original Poster Posted 2010-04-09 01:01 ·  中国 广东 东莞 电信
初级用户
Credits 30
Posts 30
Joined 2008-09-19 14:07
17-year member
UID 126048
Gender Male
Status Offline
An invalid number appears when running the batch file. How can I fix it?

The code below reduces the date by two years, then runs a piece of software, and then changes the date back to the current one. Because this software has already expired, I have to use this method to start it.
It had always been running fine before, and this problem only started today. Why is that? Experts, please help fix it.
This appears when running it:




@echo off&setlocal ENABLEDELAYEDEXPANSION
set str=%date%
for /f "tokens=1 delims= " %%a in ("%date%") do set w=%%a
for /f "tokens=1,2* delims=-" %%a in ("%w%") do (set/a e=%%a-2
date !e!-%%b-%%c
start "" "d:\Progra~1\PhraseExpress\phraseexpress.exe"
call :ProcDelay 1000
date !str!
)
exit

:ProcDelay delayMSec_
setlocal enableextensions
for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set start_=%%h%%i%%j%%k
:_procwaitloop
for /f "tokens=1-4 delims=:. " %%h in ("%time%") do set now_=%%h%%i%%j%%k
set /a diff_=%now_%-%start_%
if %diff_% LSS %1 goto _procwaitloop
endlocal & goto :EOF
Floor 2 Posted 2010-04-09 02:09 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
The problem is in the time delay part: set start_=%%h%%i%%j%%k and set now_=%%h%%i%%j%%k. When the time is between 00:00 and 01:00, the variables start_ and now_ become numbers starting with 0. In set /a diff_=%now_%-%start_%, set /a determines the numeric base according to whether the number starts with 0. If it starts with 0, then it is not decimal but octal (because after 0 there cannot be x, so it cannot be hexadecimal), so if the whole number string contains a digit greater than 7, an error occurs.
This problem is caused by a flaw in the design of batch files. At the same time, this delay section also does not take crossing midnight into account. Although the chance of that happening is low, if the batch file starts running at 23:59:5x, the delay time will produce a major error.

Considering that in actual use your delay requirement is not high, just change call :ProcDelay 1000 to ping /n 10 127.1>nul. Delete everything below exit.

[ Last edited by Hanyeguxing on 2010-4-9 at 02:20 ]
Floor 3 Posted 2010-04-09 10:11 ·  中国 广东 东莞 电信
初级用户
Credits 30
Posts 30
Joined 2008-09-19 14:07
17-year member
UID 126048
Gender Male
Status Offline
Thanks, both forums replied. As expected, Brother Hanye is really helpful.
Forum Jump: