Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for atfuture.bat

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

  1. @ECHO OFF
  2. SETLOCAL
  3.  
  4. :: Get command line arguments
  5. :: first argument   -> fAmt = Amount of time in the future to schedual
  6. :: second argument  -> fCmd = Command to be executed
  7. :: [third argument] -> fOpt = "none" as third option disables
  8. ::                            /INTERACTIVE on the job
  9.  
  10. :: Set to TRUE for troubleshooting
  11. SET DEBUG=FALSE
  12.  
  13. :: Check command line arguments
  14. IF "%2"=="" goto Usage
  15.  
  16. SET fAmt=%1  
  17. SET fCmd=%2
  18.  
  19. :: Check if specified time is within limits
  20. IF %fAmt% GTR 60 GOTO Usage
  21. IF %fAmt% LSS  2 GOTO Usage
  22.  
  23. ::Default value
  24. IF "%3"=="" (SET fOpt=/INTERACTIVE) ELSE (SET fOpt=%3)
  25. IF /I "%fOpt:~0,1%"=="N" SET fOpt=
  26.  
  27. :: For troubleshooting purposes
  28. IF "%DEBUG%"=="TRUE" ECHO fAmt %fAmt%  fCmd %fCmd%  fOpt %fOpt%
  29.  
  30. :: Date and time functions for NT, independent of International settings
  31. :: Export registry settings to a temporary .REG file
  32. START /W REGEDIT /E %TEMP%.\_TEMP.REG "HKEY_CURRENT_USER\Control Panel\International"
  33.  
  34. :: Read the exported data, filtering out keys added by Windows 2000
  35. FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND /I "iDate"') DO SET iDate=%%B
  36. FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND /I "sDate"') DO SET sDate=%%B
  37. FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND /I "iTime" ^| FIND /I /V "iTimePrefix"') DO SET iTime=%%B
  38. FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND /I "sTime" ^| FIND /I /V "sTimeFormat" ^| FIND /I /V "sTimePrefix"')  DO SET sTime=%%B
  39.  
  40. :: Remove quotes
  41. SET iDate=%iDate:"=%
  42. SET sDate=%sDate:"=%
  43. SET iTime=%iTime:"=%
  44. SET sTime=%sTime:"=%
  45.  
  46. :: Delete temporary .REG file
  47. IF EXIST %TEMP%.\_TEMP.REG DEL %TEMP%.\_TEMP.REG
  48.  
  49. :: Parse current date, using International settings from the registry
  50. IF "%iDate%"=="0" FOR /F "TOKENS=1-4* DELIMS=%sDate% " %%A IN ('DATE/T') DO (
  51. 	SET CYear=%%D
  52. 	SET CMonth=%%B
  53. 	SET CDay=%%C
  54. 	SET SortDate=%%D%%B%%C
  55. )
  56. IF "%iDate%"=="1" FOR /F "TOKENS=1-4* DELIMS=%sDate% " %%A IN ('DATE/T') DO (
  57. 	SET CYear=%%D
  58. 	SET CMonth=%%C
  59. 	SET CDay=%%B
  60. 	SET SortDate=%%D%%C%%B
  61. )
  62. IF "%iDate%"=="2" FOR /F "TOKENS=1-4* DELIMS=%sDate% " %%A IN ('DATE/T') DO (
  63. 	SET CYear=%%B
  64. 	SET CMonth=%%C
  65. 	SET CDay=%%D
  66. 	SET SortDate=%%B%%C%%D
  67. )
  68.  
  69. :: Remove leading zeroes
  70. SET /A CMonth = 1%CMonth% - 100
  71. SET /A CDay   = 1%CDay%   - 100
  72.  
  73. :: Parse current time, using International settings from the registry
  74. FOR /F "TOKENS=1,2* DELIMS=%sTime% " %%A IN ('TIME/T') DO (
  75. 	SET CHour=%%A
  76. 	SET CMins=%%B
  77. 	SET SortTime=%%A%%B
  78. )
  79. IF "%iTime%"=="1" GOTO SkipAmPm
  80.  
  81. :: Process AM/PM
  82. SET AMPM=%CMins:~2,1%
  83. SET CMins=%CMins:~0,2%
  84.  
  85. :: Remove leading zero from hours (only if necessary)
  86. IF "%CHour:~0,1%"=="0" IF NOT "%CHour%"=="0" SET /A CHour = 1%CHour% - 100
  87.  
  88. :: Convert from 12 hour AM/PM format to 24 hour format
  89. IF %CHour% LSS 12 IF /I "%AMPM%"=="P" SET /A CHour = %CHour% + 12
  90. IF %CHour% EQU 12 IF /I "%AMPM%"=="A" SET CHour=0
  91. :SkipAmPm
  92.  
  93. :: Remove leading zeroes (CMins always has 2 digits,
  94. :: with CHours we have to check if 1 or 2 digits are
  95. :: used, we may have removed the leading zero ourselves)
  96. SET /A CMins = 1%CMins% - 100
  97. IF "%CHour:~0,1%"=="0" IF NOT "%CHour%"=="0" SET /A CHour = 1%CHour% - 100
  98.  
  99. :: Display Time Variables when troubleshooting
  100. IF /I "%DEBUG%"=="TRUE" SET C | FIND /I /V "COM"
  101. IF /I "%DEBUG%"=="TRUE" SET SORT
  102.  
  103. ::Temporary variable for deciding when to roll to next hour
  104. SET /A tMax = 59 - %fAmt%
  105. IF %CMins% GTR %tMax% (CALL :RollHour) ELSE (SET /A CMins = %CMins% + %fAmt%)
  106.  
  107. :: Restore leading zeroes
  108. IF %CMins% LSS 10 SET CMins = 0%CMins%
  109. IF %CHour% LSS 10 SET CHour = 0%CHour%
  110.  
  111. :: ** Here is the actual AT job set
  112. AT %CHour%:%CMins% %fOpt% cmd /c %fCmd%
  113.  
  114. :: Done
  115. GOTO End
  116.  
  117.  
  118. ::****************************  SUB ROUTINES
  119.  
  120. :RollHour
  121. :: CMins + fAmt is greater than 60.  so we need to increment CHour
  122. SET /A CMins = %CMins% - 60 + %fAmt%
  123. :: If its 23 hundred hours and we're going to add one
  124. :: hour we need to goto 00 but rather than using another goto
  125. :: we'll just set to -1 then increment by 1
  126. IF %CHour% EQU 23 SET CHour=-1
  127. SET /A CHour = %CHour% + 1
  128. GOTO:EOF
  129.  
  130.  
  131. :Usage
  132. ECHO.
  133. ECHO ATFUTURE.BAT,  Version 1.20 for Windows NT
  134. ECHO Schedule a command a specified number of minutes in the near future
  135. ECHO.
  136. ECHO Usage:  %~n0  minutes  command  [opt]
  137. ECHO.
  138. ECHO Where:  minutes = number of minutes in future to schedule command ^(2-60^)
  139. ECHO         command = command to be executed ^(executed as CMD /C command^);
  140. ECHO                   for long file names or extra command line parameters,
  141. ECHO                   enclose the entire command in double quotes
  142. ECHO         opt     = options for at:
  143. ECHO                   None will set none
  144. ECHO                   /INTERACTIVE by default
  145. ECHO.
  146. ECHO Written by Rob Fuller
  147. ECHO rob_s_fuller@yahoo.com
  148. ECHO and Rob van der Woude
  149. ECHO http://www.robvanderwoude.com
  150. GOTO End
  151.  
  152.  
  153. :End
  154. ENDLOCAL
  155.  

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