justchil
justchil

Reputation: 85

Batch file run cmd1 if time 10pm-4am else run cmd2

I have a batch file and within that batch file I need to run one of two commands depending on time of my server.

If the time is between 22:00:00 and 03:30:00 -- xcopy /Y a\1.txt c\1.txt

If the time is before or after this range -- -- xcopy /Y b\1.txt c\1.txt

This will use xcopy to switch a file back and forth depending on the time.

I know this is easy but my brain just won't work atm lol

Edit:

Went with 22:00 and 4:00... this is what I came up with but it doesn't seem like the best way...

set current_time=%time:~0,5% 

if "%current_time%" lss "22:00" goto daycycle 

if "%current_time%" gtr " 4:00" goto daycycle 

echo Do this between 10pm and 4am

goto continue 

:daycycle 

echo Do this before 10pm and after 4am

:continue

Upvotes: 7

Views: 25934

Answers (3)

NOAH BRANDON
NOAH BRANDON

Reputation: 1

What should work:

@echo off
:loop
set hour=%time:~0,2%
set min=%time:~3,2%

cls
ECHO %hour%
ECHO %min%
ECHO This %I%

IF %hour% == 14 GOTO Test2
goto loop

:Test2
IF %min% == 58 GOTO YUP
IF %min% == 59 GOTO LATE
Goto loop
:YUP
SET I=0
GOTO loop

:LATE
SET I=NOPE
GOTO loop 

Upvotes: -1

MC ND
MC ND

Reputation: 70923

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "now=%time: =0%"

    set "task=day"
    if "%now%" lss "03:30:00,00" ( set "task=night" ) 
    if "%now%" geq "22:00:00,00" ( set "task=night" )

    call :task_%task%

    endlocal
    exit /b

:task_day
    :: do daily task
    goto :eof

:task_night
    :: do nightly task
    goto :eof

EDITED - The previous code should work under the conditions in the original question. But will fail in different time configurations. This should solve the usual problems

@echo off
    setlocal enableextensions disabledelayedexpansion

    call :getTime now

    set "task=day"
    if "%now%" lss "03:30:00,00" ( set "task=night" ) 
    if "%now%" geq "22:00:00,00" ( set "task=night" )

    call :task_%task%

    echo %now%

    endlocal
    exit /b

:task_day
    :: do daily task
    goto :eof

:task_night
    :: do nightly task
    goto :eof

:: getTime
::    This routine returns the current (or passed as argument) time
::    in the form hh:mm:ss,cc in 24h format, with two digits in each
::    of the segments, 0 prefixed where needed.
:getTime returnVar [time]
    setlocal enableextensions disabledelayedexpansion

    :: Retrieve parameters if present. Else take current time
    if "%~2"=="" ( set "t=%time%" ) else ( set "t=%~2" )

    :: Test if time contains "correct" (usual) data. Else try something else
    echo(%t%|findstr /i /r /x /c:"[0-9:,.apm -]*" >nul || ( 
        set "t="
        for /f "tokens=2" %%a in ('2^>nul robocopy "|" . /njh') do (
            if not defined t set "t=%%a,00"
        )
        rem If we do not have a valid time string, leave
        if not defined t exit /b
    )

    :: Check if 24h time adjust is needed
    if not "%t:pm=%"=="%t%" (set "p=12" ) else (set "p=0")

    :: Separate the elements of the time string
    for /f "tokens=1-5 delims=:.,-PpAaMm " %%a in ("%t%") do (
        set "h=%%a" & set "m=00%%b" & set "s=00%%c" & set "c=00%%d" 
    )

    :: Adjust the hour part of the time string
    set /a "h=100%h%+%p%"

    :: Clean up and return the new time string
    endlocal & if not "%~1"=="" set "%~1=%h:~-2%:%m:~-2%:%s:~-2%,%c:~-2%" & exit /b

Upvotes: 11

Monacraft
Monacraft

Reputation: 6630

Try this:

@echo off
set hour=%time:~0,2%
set min=%time:~3,2%

if %hour% GEQ 22 (
    xcopy /Y a\1.txt c\1.txt
) ELSE (
   if %hour% LEQ 3 (
       if %hour% EQU 3 if %min% GTR 30 (
           xcopy /Y b\1.txt c\1.txt
           goto :END
       )
       xcopy /Y a\1.txt c\1.txt
   ) ELSE (
       xcopy /Y b\1.txt c\1.txt
       goto :END
   )
)

:END

And I'm rather sure that will do what you want. Manual if statements for the win! Note it would be a lot easier if it was 22:00 to 4:00 or 3:00. I had to incorperate the :30 minute checker.

But yea, it might not work, so just check it before you put it up on your server.

Monacraft.

Upvotes: 0

Related Questions