Batch: During my code loop it stops setting a variable. All Help Welcome

与世无争的帅哥 提交于 2019-12-02 12:48:58

问题


goto time

:time
set tm=%time%
set hh=%tm:~0,2%
set mm=%tm:~3,2%
set ss=%tm:~6,2%
set ms=%tm:~7,2%
goto date

:date
set dt=%date%
set wd=%dt:~0,3%
set mh=%dt:~4,2%
set dy=%dt:~6,2%
set yr=%dt:~8,4%
goto scheduletimes

:scheduletimes
goto hour1
:hour1
for /f "tokens=1*delims=0" %%a in ("$0%hh%") do set /a "HH"="%%b"
if %HH% equ 6 goto minutes1
pause
goto time
:minutes1
for /f "tokens=1*delims=0" %%a in ("$0%mm%") do set /a "MM"="%%b"
if "%MM%"=="00" goto seconds1
pause
goto time
:seconds1
for /f "tokens=1*delims=0" %%a in ("$0%ss%") do set /a "SS"="%%b"
if %SS% lss 10 goto day1
pause
goto time

:day1
echo success
echo %hh%
echo %mm%
echo %ss%
pause 
goto time

As you guys can see this is a time check loop. Seeing if the time is 6:10:>10. Basically what my problem is though is that it runs fine until it executes the command when it hits that time. As soon as it becomes <10 seconds in this section:

:minutes1
for /f "tokens=1*delims=0" %%a in ("$0%mm%") do set /a "MM"="%%b"
if %MM% equ 0 goto seconds1

it stops setting MM to %mm% and it gives me a Missing Operand error.

Any help would be appreciated to get it to continue working after the first run.

(Yes I do know there are pauses and no @echo off, This is for debugging purposes. It does it with the @echo off and without the pauses too)

Edit: Log

C:\Users\krato_000\Desktop>goto scheduletimes

C:\Users\krato_000\Desktop>goto hour1

C:\Users\krato_000\Desktop>for /F "tokens=1*delims=0" %a in ("$0 6") do set /a "
HH"="%b"

C:\Users\krato_000\Desktop>set /a "HH"=" 6"

C:\Users\krato_000\Desktop>if 6 EQU 6 goto minutes1

C:\Users\krato_000\Desktop>for /F "tokens=1*delims=0" %a in ("$000") do set /a "
MM"="%b"

C:\Users\krato_000\Desktop>set /a "MM"=""
Missing operand.

C:\Users\krato_000\Desktop>if "00" == "00" goto seconds1

C:\Users\krato_000\Desktop>for /F "tokens=1*delims=0" %a in ("$019") do set /a "
SS"="%b"

C:\Users\krato_000\Desktop>set /a "SS"="19"

C:\Users\krato_000\Desktop>if 19 LSS 10 goto day1

C:\Users\krato_000\Desktop>pause
Press any key to continue . . .

回答1:


Please note, batch does not differentiate between %mm%and %MM%. Try this:

:minutes1
set /a min=0
for /f "tokens=1*delims=0" %%a in ("$0%mm%") do set /a min=%%b 2>nul
echo %min%
if %min% equ 0 goto seconds1



回答2:


Perhaps you have a fundamental problem with delims=0. If the value is 00 you will get nothing. So you need to quote some variables

if "%HH%"=="6" goto minutes1

and

if "%MM%"=="" set MM=0 & goto seconds1

and add the first line below

if "%SS%"=="" set SS=0
if %SS% lss 10 goto day1


来源:https://stackoverflow.com/questions/17621363/batch-during-my-code-loop-it-stops-setting-a-variable-all-help-welcome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!