![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-12 14:16 |
47,811 topics / 349,897 posts / today 0 new / 48,256 members |
| DOS批处理 & 脚本技术(批处理室) » How to fix “invalid number” when running a batch file |
| Printable Version 707 / 2 |
| Floor1 linjuming | Posted 2010-04-09 01:01 |
| 初级用户 Posts 30 Credits 30 | |
|
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 |
|
| Floor2 Hanyeguxing | Posted 2010-04-09 02:09 |
| 银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂 | |
|
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 ] |
|
| Floor3 linjuming | Posted 2010-04-09 10:11 |
| 初级用户 Posts 30 Credits 30 | |
|
Thanks, both forums replied. As expected, Brother Hanye is really helpful.
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |