How to fire a command at HH:29:55 and HH:59:55 in a windows batch file

  • 1 year ago
Comment

ChatGPTChatGPT
1 / 1

To run a command at HH:29:55 and HH:59:55 in a Windows batch file, you can use a combination of the timeout command and a loop. Here's an example:

batch
@echo off

:LOOP
REM Get the current time
for /f "tokens=1-3 delims=:." %%a in ("%time%") do (
set HH=%%a
set MM=%%b
set SS=%%c
)

REM Check if the current time matches the desired trigger time
if "%MM%"=="29" (
if "%SS%"=="55" (
REM Run your command here
echo Running command at HH:29:55
REM Replace the following line with your actual command
echo Your command goes here
)
) else if "%MM%"=="59" (
if "%SS%"=="55" (
REM Run your command here
echo Running command at HH:59:55
REM Replace the following line with your actual command
echo Your command goes here
)
)

REM Wait for 5 seconds
timeout /t 5 >nul

REM Jump back to the beginning of the loop
goto :LOOP

In the above example, the batch file uses a loop to continuously check the current time. It extracts the hour, minute, and second components from the current time using the for /f loop. It then checks if the minute is 29 or 59 and the second is 55. If the condition is met, it executes the desired command.

The timeout /t 5 >nul command pauses the script for 5 seconds before checking the time again. This ensures that the loop doesn't run continuously and allows the script to execute the command at the specified trigger times.

Note: The script assumes that it will be running continuously and may run indefinitely until interrupted manually. Make sure to consider the implications of running such a script continuously and adjust it as needed to fit your specific requirements.

Replace the placeholder echo Your command goes here line with your actual command that you want to run at the specified trigger times.

By using this approach, you can run a command at HH:29:55 and HH:59:55 in a Windows batch file.