Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for repeatevery.bat

(view source code of repeatevery.bat as plain text)

  1. @ECHO OFF
  2. :: Prepare display of command line before starting delayed variable expansion
  3. FOR /F "tokens=2*" %%A IN ("%*") DO SET CommandLine=%%B
  4. SET CommandLine=%CommandLine:!=$?$#$%
  5.  
  6. :: Start delayed variable expansion
  7. SETLOCAL ENABLEDELAYEDEXPANSION
  8.  
  9. :: Finish preparing display of command line after starting delayed variable expansion
  10. SET CommandLine=!CommandLine:$?$#$=%%!
  11.  
  12. :: Check command line
  13. IF "%~3"=="" GOTO Syntax
  14.  
  15. SET Interval=0
  16. SET /A Interval=%1 2>NUL || GOTO Syntax
  17. IF "%Interval%"=="0" GOTO Syntax
  18.  
  19. ECHO.%~2 | FINDSTR /R /I /B /C:"sec" >NUL
  20. IF ERRORLEVEL 1 (
  21. 	ECHO.%~2 | FINDSTR /R /I /B /C:"min" >NUL
  22. 	IF ERRORLEVEL 1 (
  23. 		ECHO.%~2 | FINDSTR /R /I /B /C:"hou" >NUL
  24. 		IF ERRORLEVEL 1 (
  25. 			ECHO.%~2 | FINDSTR /R /I /B /C:"day" >NUL
  26. 			IF ERRORLEVEL 1 (
  27. 				GOTO Syntax
  28. 			) ELSE (
  29. 				SET Steps=86400
  30. 			)
  31. 		) ELSE (
  32. 			SET Steps=3600
  33. 		)
  34. 	) ELSE (
  35. 		SET Steps=60
  36. 	)
  37. ) ELSE (
  38. 	SET Steps=1
  39. )
  40.  
  41. CLS
  42. ECHO.
  43. ECHO Repating the following command until a key is pressed:
  44. ECHO.
  45. ECHO.Command ^& Arguments:    %CommandLine%
  46. ECHO.
  47. ECHO Working Directory:      "%CD%"
  48. ECHO.
  49. ECHO Iterations:             0
  50. ECHO.
  51. ECHO Press any key to abort . . .
  52.  
  53. :: Endless loop
  54. SET Break=0
  55. SET Count=0
  56. FOR /L %%A IN (0,0,1) DO (
  57. 	REM Wait
  58. 	FOR /L %%B IN (1,1,%Steps%) DO (
  59. 		IF !Break!==0 (
  60. 			TIMEOUT %Interval% | FINDSTR /R /E /C:"[^0-9]0" >NUL
  61. 			IF ERRORLEVEL 1 (
  62. 				SET Break=1
  63. 				EXIT
  64. 			)
  65. 		)
  66. 		IF !Break!==1 EXIT
  67. 		SET /A Count += 1
  68. 		FOR /F "tokens=2*" %%A IN ("%*") DO SET CommandLine=%%B
  69. 		CLS
  70. 		ECHO.
  71. 		ECHO Repating the following command until a key is pressed:
  72. 		ECHO.
  73. 		ECHO.Command ^& Arguments:    %CommandLine%
  74. 		ECHO.
  75. 		ECHO Working Directory:      "%CD%"
  76. 		ECHO.
  77. 		ECHO Iterations:             !Count!
  78. 		ECHO.
  79. 		ECHO Press any key to abort . . .
  80. 		ECHO.
  81. 		REM Execute the command
  82. 		!CommandLine!
  83. 	)
  84. 	IF !Break!==1 EXIT
  85. )
  86.  
  87. ENDLOCAL
  88. SET CommandLine=
  89. EXIT /B 0
  90.  
  91.  
  92. :Syntax
  93. ENDLOCAL
  94. SET CommandLine=
  95.  
  96. ECHO.
  97. ECHO RepeatEvery.bat,  Version 1.02
  98. ECHO Repeat a command forever, with a specified interval, until a key is pressed
  99. ECHO.
  100. ECHO Usage:    RepeatEvery  steps  stepsize  command  [ arguments ]
  101. ECHO.
  102. ECHO Where:    steps        number of seconds, minutes, hours or days ^(integer^)
  103. ECHO           stepsize     either "seconds", "minutes", "hours" or "days"
  104. ECHO                        ^(only the first 3 characters are required^)
  105. ECHO           command      command to be exetuted repeatedly
  106. ECHO           arguments    optional command line arguments for command ^(no double
  107. ECHO                        quotes, ampersands, pipes nor redirection allowed^)
  108. ECHO.
  109. ECHO Example:  RepeatEvery 5 sec ECHO Test 1 2 3 !Time!
  110. ECHO.
  111. ECHO Notes:    Note the use of !Time! instead of %%Time%% in the example: delayed
  112. ECHO           variable expansion is required for non-static variables.
  113. ECHO           !Count! may be used on the command line for the iterations count.
  114. ECHO.
  115. ECHO Written by Rob van der Woude
  116. ECHO http://www.robvanderwoude.com
  117.  
  118. EXIT /B 1
  119.  

page last modified: 2024-04-16; loaded in 0.0199 seconds