Reputation: 11
I am currently working on a batch file that will compare the current time to a specified time before starting a program. i.e: The batch file checks if it's past 10 am. If it is it start the program. If not, it sends a message telling that the program can't be open before 10 am. If it's past 6 pm, the program won't start and a message appears.
This is my code, for now:
@echo off
Set _hourAM=
Set _minAM=
Set _hourPM=
Set _hourPM=
If %_nowh% GEQ %_hourAM% If %_nowm% GEQ %_minAM% (
If %_nowh% LSS %_hourPM% If %_nowm% LSS %_minPM% Goto _approved
) ELSE (
Upvotes: 1
Views: 1062
Reputation: 70923
This uses a subroutine to transform current time to 24h format just in case current time is in 12h format.
@echo off
setlocal enableextensions
call :getTime24h now
if "%now%" geq "18:00:00,00" goto noRun
if "%now%" lss "10:00:00,00" goto noRun
echo Here the program should be called
goto endProcess
:noRun
echo ERROR: Program can not be run now
pause
:endProcess
endlocal
exit /b
:getTime24h returnVariable
setlocal enableextensions
set "ts=%time%"
if "%ts:pm=%"=="%ts%" ( set "adjust=0" ) else ( set "adjust=12" )
for /f "tokens=1-4 delims=:-.,apm " %%a in ("%ts%") do ( set /a "h=100%%a+adjust", "m=100%%b", "s=100%%c", "c=100%%d" )
endlocal & set "%~1=%h:~-2%:%m:~-2%:%s:~-2%,%c:~-2%" & exit /b
Upvotes: 0
Reputation: 77657
You could try a completely different approach.
Allocate a "flag folder" somewhere.
Make your batch file check for existence of a flag (an empty file by a specific name) in that folder. If it exists, run the program, otherwise deny running the program. (Or you could reverse the logic, if you like.)
Create two scheduled tasks, one running at 10:00 AM and creating the flag file and the other running at 6:00 PM deleting it. (It could be another or same batch file accepting a parameter to specify whether to create or delete the flag file.)
Batch files aren't very good at date/time calculation, so, in my opinion, it would be better (and really easy too) to delegate that job to the system component that can do it very well, and just co-operate with it.
Upvotes: 0
Reputation: 9545
You can do like this :
@echo off&cls
set "$AM=0950"
set "$PM=1800"
set "$Local_T=%time:~0,2%%time:~3,2%"
set "$Sw_T=True"
if %$Local_T% lss %$AM% set "$Sw_T=False"
if %$Local_T% gtr %$PM% set "$Sw_T=False"
If "%$Sw_T%"=="True" (
echo Path\to\your\program.exe
exit /b)
cls&echo "YOU CAN RUN THIS PROGRAM ONLY BETWEEN 09:50 AND 18:00"
pause
exit
To start your program you have to replace the line :
echo Path\to\your\program.exe
Bye the path of your program.
Upvotes: 2