Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

batch-file

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
like image 912
justchil Avatar asked Feb 16 '14 08:02

justchil


People also ask

How do I loop a batch script only a certain amount of times?

There is an example: @echo off set loop=0 :loop echo hello world set /a loop=%loop%+1 if "%loop%"=="2" goto next goto loop :next echo This text will appear after repeating "hello world" for 2 times. Output: hello world hello world This text will appear after repeating "hello world" for 2 times.

Is there an else if in batch?

However, you can't use else if in batch scripting. Instead, simply add a series of if statements: if %x%==5 if %y%==5 (echo "Both x and y equal 5.") if %x%==10 if %y%==10 (echo "Both x and y equal 10.") else (echo "Invalid input.")

How do you sequentially execute commands in batch file?

Instead of scheduling multiple Windows Tasks that may overlap, use the "start /wait" command a batch file (. bat) to automatically run multiple commands in sequential order.


1 Answers

@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
like image 105
MC ND Avatar answered Oct 10 '22 19:10

MC ND