Search code examples
batch-filecommandoperand

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


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 . . .

Solution

  • 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