英文:
why does my number comparing alway become true?
问题
每当我运行脚本时,即使时间在12:03之前,它总是以时间超过12:03的方式运行,我不明白为什么。我想要将白天时间与自动关闭的设置时间进行比较,但它总是将时间返回为超过我设置的时间,即使它并不是。
以下是代码:
@echo off
timeout /t 30
:RunAgain
set HH=12
set MM=03
set ThisHour=%TIME:~0,2%
set ThisMinute=%TIME:~3,2%
echo 时间:%ThisHour%:%ThisMinute%
if ThisHour GEQ HH (
echo 小时超过%HH%
if ThisMinute GEQ MM (
goto Shutdown
) ELSE (
echo 时间在%HH%:%MM%之下
timeout /t 30
goto RunAgain )
) ELSE (
echo 时间在%HH%:%MM%之下
timeout /t 30
goto RunAgain )
:Shutdown
echo 正在关闭
pause
英文:
Whenever I run the script, even when the time is under 12:03 it always runs as if the time was over 12:03, And I don't understand why. I wanted to compare the time of day to a set time for an automatic shutdown, but it always returns the time as over my set clock, even when it isn't.
Here is the code:
@echo off
timeout /t 30
:RunAgain
set HH=12
set MM=03
set ThisHour=%TIME:~0,2%
set ThisMinute=%TIME:~3,2%
echo time: %ThisHour%:%ThisMinute%
if ThisHour GEQ HH (
echo Hour is over %HH%
if ThisMinute GEQ MM (
goto Shutdown
) ELSE (
echo time is under %HH%:%MM%
timeout /t 30
goto RunAgain )
) ELSE (
echo time is under %HH%:%MM%
timeout /t 30
goto RunAgain )
:Shutdown
echo Shutting down
pause
答案1
得分: 1
以下是翻译好的部分:
问题在于变量未正确传递到__IF__语句。
当将它们传递到__IF__时,应该在变量名称周围添加%
。
建议使用%SystemRoot%\System32\timeout.exe
代替仅使用timeout
,以避免cmd.exe
进程必须进行多次文件系统访问以查找此可执行文件。
您可以更改脚本如下:
@echo off
set HH=12
set MM=03
:RunAgain
%SystemRoot%\System32\timeout.exe /t 30
set ThisHour=%TIME:~0,2%
set ThisMinute=%TIME:~3,2%
echo time: %ThisHour%:%ThisMinute%
if "%ThisHour%" GTR "%HH%" echo Hour is over %HH%& goto Shutdown
if "%ThisHour%" == "%HH%" if "%ThisMinute%" GEQ "%MM%" echo Hour is over %HH%& goto Shutdown
echo time is under %HH%:%MM%& goto RunAgain
:Shutdown
echo Shutting down
pause
经过这些更改后,脚本将按预期工作。
12以下的脚本输出:
等待 29 秒,按任意键继续...
时间:10:12
时间在 12:03 之下
等待 22 秒,按任意键继续...
12以上的脚本输出:
等待 28 秒,按任意键继续...
时间:22:14
超过 12 点
正在关闭
按任意键继续...
编辑: 感谢评论和更正
英文:
The issue here is that the variables are not being correctly passed to the IF statement.
When passing them to the IF, you should add %
around the variable names.
It is recommended to use %SystemRoot%\System32\timeout.exe
instead of just timeout
to avoid that the cmd.exe
process has to make multiple file system accesses to find this executable.
You can change the script to:
@echo off
set HH=12
set MM=03
:RunAgain
%SystemRoot%\System32\timeout.exe /t 30
set ThisHour=%TIME:~0,2%
set ThisMinute=%TIME:~3,2%
echo time: %ThisHour%:%ThisMinute%
if "%ThisHour%" GTR "%HH%" echo Hour is over %HH%& goto Shutdown
if "%ThisHour%" == "%HH%" if "%ThisMinute%" GEQ "%MM%" echo Hour is over %HH%& goto Shutdown
echo time is under %HH%:%MM%& goto RunAgain
:Shutdown
echo Shutting down
pause
After doing these changes, the script works as intended.
Script output for under 12:
Waiting for 29 seconds, press a key to continue ...
time: 10:12
time is under 12:03
Waiting for 22 seconds, press a key to continue ...
Script output for over 12:
Waiting for 28 seconds, press a key to continue ...
time: 22:14
Hour is over 12
Shutting down
Press any key to continue . . .
EDIT: Thanks for the comments and corrections
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论