(view source code of repeatevery.bat as plain text)
@ECHO OFF
:: Prepare display of command line before starting delayed variable expansion
FOR /F "tokens=2*" %%A IN ("%*") DO SET CommandLine=%%B
SET CommandLine=%CommandLine:!=$?$#$%
:: Start delayed variable expansion
SETLOCAL ENABLEDELAYEDEXPANSION
:: Finish preparing display of command line after starting delayed variable expansion
SET CommandLine=!CommandLine:$?$#$=%%!
:: Check command line
IF "%~3"=="" GOTO Syntax
SET Interval=0
SET /A Interval=%1 2>NUL || GOTO Syntax
IF "%Interval%"=="0" GOTO Syntax
ECHO.%~2 | FINDSTR /R /I /B /C:"sec" >NUL
IF ERRORLEVEL 1 (
ECHO.%~2 | FINDSTR /R /I /B /C:"min" >NUL
IF ERRORLEVEL 1 (
ECHO.%~2 | FINDSTR /R /I /B /C:"hou" >NUL
IF ERRORLEVEL 1 (
ECHO.%~2 | FINDSTR /R /I /B /C:"day" >NUL
IF ERRORLEVEL 1 (
GOTO Syntax
) ELSE (
SET Steps=86400
)
) ELSE (
SET Steps=3600
)
) ELSE (
SET Steps=60
)
) ELSE (
SET Steps=1
)
CLS
ECHO.
ECHO Repating the following command until a key is pressed:
ECHO.
ECHO.Command ^& Arguments: %CommandLine%
ECHO.
ECHO Working Directory: "%CD%"
ECHO.
ECHO Iterations: 0
ECHO.
ECHO Press any key to abort . . .
:: Endless loop
SET Break=0
SET Count=0
FOR /L %%A IN (0,0,1) DO (
REM Wait
FOR /L %%B IN (1,1,%Steps%) DO (
IF !Break!==0 (
TIMEOUT %Interval% | FINDSTR /R /E /C:"[^0-9]0" >NUL
IF ERRORLEVEL 1 (
SET Break=1
EXIT
)
)
IF !Break!==1 EXIT
SET /A Count += 1
FOR /F "tokens=2*" %%A IN ("%*") DO SET CommandLine=%%B
CLS
ECHO.
ECHO Repating the following command until a key is pressed:
ECHO.
ECHO.Command ^& Arguments: %CommandLine%
ECHO.
ECHO Working Directory: "%CD%"
ECHO.
ECHO Iterations: !Count!
ECHO.
ECHO Press any key to abort . . .
ECHO.
REM Execute the command
!CommandLine!
)
IF !Break!==1 EXIT
)
ENDLOCAL
SET CommandLine=
EXIT /B 0
:Syntax
ENDLOCAL
SET CommandLine=
ECHO.
ECHO RepeatEvery.bat, Version 1.02
ECHO Repeat a command forever, with a specified interval, until a key is pressed
ECHO.
ECHO Usage: RepeatEvery steps stepsize command [ arguments ]
ECHO.
ECHO Where: steps number of seconds, minutes, hours or days ^(integer^)
ECHO stepsize either "seconds", "minutes", "hours" or "days"
ECHO ^(only the first 3 characters are required^)
ECHO command command to be exetuted repeatedly
ECHO arguments optional command line arguments for command ^(no double
ECHO quotes, ampersands, pipes nor redirection allowed^)
ECHO.
ECHO Example: RepeatEvery 5 sec ECHO Test 1 2 3 !Time!
ECHO.
ECHO Notes: Note the use of !Time! instead of %%Time%% in the example: delayed
ECHO variable expansion is required for non-static variables.
ECHO !Count! may be used on the command line for the iterations count.
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
EXIT /B 1
page last modified: 2024-04-16; loaded in 0.0070 seconds