Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for yesterday.bat

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

  1. @ECHO OFF
  2. :: Windows NT 4 or later only
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4. :: No command line arguments required
  5. IF NOT [%1]==[] GOTO Syntax
  6.  
  7. :: Keep variables local
  8. SETLOCAL ENABLEDELAYEDEXPANSION
  9.  
  10. :: Query the registry for the date format and delimiter
  11. CALL :DateFormat
  12.  
  13. :: Parse today's date depending on registry's local date format settings
  14. IF %iDate%==0 FOR /F "TOKENS=1-4* DELIMS=%sDate%" %%A IN ('DATE/T') DO (
  15. 	SET LocalFormat=MM%sDate%DD%sDate%YYYY
  16. 	SET tLocal=%%tMonth%%%sDate%%%tDay%%%sDate%%%tYear%%
  17. 	SET yLocal=%%yMonth%%%sDate%%%yDay%%%sDate%%%yYear%%
  18. 	SET Year=%%C
  19. 	SET Month=%%A
  20. 	SET Day=%%B
  21. )
  22. IF %iDate%==1 FOR /F "TOKENS=1-4* DELIMS=%sDate%" %%A IN ('DATE/T') DO (
  23. 	SET LocalFormat=DD%sDate%MM%sDate%YYYY
  24. 	SET tLocal=%%tDay%%%sDate%%%tMonth%%%sDate%%%tYear%%
  25. 	SET yLocal=%%yDay%%%sDate%%%yMonth%%%sDate%%%yYear%%
  26. 	SET Year=%%C
  27. 	SET Month=%%B
  28. 	SET Day=%%A
  29. )
  30. IF %iDate%==2 FOR /F "TOKENS=1-4* DELIMS=%sDate%" %%A IN ('DATE/T') DO (
  31. 	SET LocalFormat=YYYY%sDate%MM%sDate%DD
  32. 	SET tLocal=%%tYear%%%sDate%%%tMonth%%%sDate%%%tDay%%
  33. 	SET yLocal=%%yYear%%%sDate%%%yMonth%%%sDate%%%yDay%%
  34. 	SET Year=%%A
  35. 	SET Month=%%B
  36. 	SET Day=%%C
  37. )
  38.  
  39. :: Remove the day of week if applicable
  40. FOR %%A IN (%Year%)  DO SET Year=%%A
  41. FOR %%A IN (%Month%) DO SET Month=%%A
  42. FOR %%A IN (%Day%)   DO SET Day=%%A
  43.  
  44. :: Today's date in YYYYMMDD format
  45. SET SortDate=%Year%%Month%%Day%
  46.  
  47. :: Today's date in local format
  48. FOR %%A IN (%Date%) DO SET Today=%%A
  49.  
  50. :: Remove leading zeroes
  51. IF   "%Day:~0,1%"=="0" SET   Day=%Day:~1%
  52. IF "%Month:~0,1%"=="0" SET Month=%Month:~1%
  53.  
  54. :: Calculate yesterday's date
  55. CALL :JDate %Year% %Month% %Day%
  56. SET /A JDate -= 1
  57. CALL :GDate %JDate%
  58. FOR /F "tokens=1-3" %%A IN ('ECHO %GDate%') DO (
  59. 	SET yYear=%%A
  60. 	SET yMonth=%%B
  61. 	SET yDay=%%C
  62. )
  63. :: Calculate tomorrow's date
  64. SET /A JDate += 2
  65. CALL :GDate %JDate%
  66. FOR /F "tokens=1-3" %%A IN ('ECHO %GDate%') DO (
  67. 	SET tYear=%%A
  68. 	SET tMonth=%%B
  69. 	SET tDay=%%C
  70. )
  71.  
  72. :: Add leading zero to tDay, yDay, tMonth and/or yMonth if necessary
  73. IF 1%tDay%   LSS 20 SET tDay=0%tDay%
  74. IF 1%yDay%   LSS 20 SET yDay=0%yDay%
  75. IF 1%tMonth% LSS 20 SET tMonth=0%tMonth%
  76. IF 1%yMonth% LSS 20 SET yMonth=0%yMonth%
  77.  
  78. :: Yesterday's and tomorrow's date in YYYYMMDD format
  79. SET SortTom=%tYear%%tMonth%%tDay%
  80. SET SortYest=%yYear%%yMonth%%yDay%
  81.  
  82. :: Display the results
  83. ECHO Format:     YYYYMMDD  (%LocalFormat%)
  84. ECHO.==================================
  85. CALL ECHO Yesterday:  %SortYest%  (%yLocal%)
  86. ECHO Today:      %SortDate%  (%Today%)
  87. CALL ECHO Tomorrow:   %SortTom%  (%tLocal%)
  88.  
  89. :: Done
  90. ENDLOCAL&SET Yesterday=%SortYest%&SET Today=%SortDate%&SET Tomorrow=%SortTom%
  91. GOTO:EOF
  92.  
  93. :: * * * * * * * *  Subroutines  * * * * * * * *
  94.  
  95.  
  96. :DateFormat
  97. REG.EXE /? 2>&1 | FIND "REG QUERY" >NUL
  98. IF ERRORLEVEL 1 (
  99. 	CALL :DateFormatRegEdit
  100. ) ELSE (
  101. 	CALL :DateFormatReg
  102. )
  103. GOTO:EOF
  104.  
  105.  
  106. :DateFormatReg
  107. FOR /F "tokens=1-3" %%A IN ('REG Query "HKCU\Control Panel\International" ^| FINDSTR /R /C:"[is]Date"') DO (
  108. 	IF "%%~A"=="REG_SZ" (
  109. 		SET %%~B=%%~C
  110. 	) ELSE (
  111. 		SET %%~A=%%~C
  112. 	)
  113. )
  114. GOTO:EOF
  115.  
  116.  
  117. :DateFormatRegEdit
  118. :: Export registry's date format settings to a temporary file, in case REG.EXE is not available
  119. START /W REGEDIT /E %TEMP%.\_TEMP.REG "HKEY_CURRENT_USER\Control Panel\International"
  120. :: Read the exported data
  121. FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND /I "iDate"') DO SET iDate=%%B
  122. FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND /I "sDate"') DO SET sDate=%%B
  123. DEL %TEMP%.\_TEMP.REG
  124. :: Remove quotes from exported values
  125. SET iDate=%iDate:"=%
  126. SET sDate=%sDate:"=%
  127. GOTO:EOF
  128.  
  129.  
  130. :GDate
  131. :: Convert Julian date back to "normal" Gregorian date
  132. :: Argument : Julian date
  133. :: Returns  : YYYY MM DD
  134. ::
  135. :: Algorithm based on Fliegel-Van Flandern
  136. :: algorithm from the Astronomical Almanac,
  137. :: provided by Doctor Fenton on the Math Forum
  138. :: (http://mathforum.org/library/drmath/view/51907.html),
  139. :: and converted to batch code by Ron Bakowski.
  140. ::
  141. SET /A P      = %1 + 68569
  142. SET /A Q      = 4 * %P% / 146097
  143. SET /A R      = %P% - ( 146097 * %Q% +3 ) / 4
  144. SET /A S      = 4000 * ( %R% + 1 ) / 1461001
  145. SET /A T      = %R% - 1461 * %S% / 4 + 31
  146. SET /A U      = 80 * %T% / 2447
  147. SET /A V      = %U% / 11
  148. SET /A GYear  = 100 * ( %Q% - 49 ) + %S% + %V%
  149. SET /A GMonth = %U% + 2 - 12 * %V%
  150. SET /A GDay   = %T% - 2447 * %U% / 80
  151. :: Clean up the mess
  152. FOR %%A IN (P Q R S T U V) DO SET %%A=
  153. :: Add leading zeroes
  154. IF 1%GMonth% LSS 20 SET GMonth=0%GMonth%
  155. IF 1%GDay%   LSS 20 SET GDay=0%GDay%
  156. :: Return value
  157. SET GDate=%GYear% %GMonth% %GDay%
  158. GOTO:EOF
  159.  
  160.  
  161. :JDate
  162. :: Convert date to Julian
  163. :: Arguments : YYYY MM DD
  164. :: Returns   : Julian date
  165. ::
  166. :: Algorithm based on Fliegel-Van Flandern
  167. :: algorithm from the Astronomical Almanac,
  168. :: provided by Doctor Fenton on the Math Forum
  169. :: (http://mathforum.org/library/drmath/view/51907.html),
  170. :: and converted to batch code by Ron Bakowski.
  171. ::
  172. SET /A Month1 = ( %2 - 14 ) / 12
  173. SET /A Year1  = %1 + 4800
  174. SET /A JDate  = 1461 * ( %Year1% + %Month1% ) / 4 + 367 * ( %2 - 2 -12 * %Month1% ) / 12 - ( 3 * ( ( %Year1% + %Month1% + 100 ) / 100 ) ) / 4 + %3 - 32075
  175. FOR %%A IN (Month1 Year1) DO SET %%A=
  176. GOTO:EOF
  177.  
  178.  
  179. :UpCase
  180. SET UpCase=%1
  181. SET UpCase=%UpCase:a=A%
  182. SET UpCase=%UpCase:b=B%
  183. SET UpCase=%UpCase:c=C%
  184. SET UpCase=%UpCase:d=D%
  185. SET UpCase=%UpCase:e=E%
  186. SET UpCase=%UpCase:f=F%
  187. SET UpCase=%UpCase:g=G%
  188. SET UpCase=%UpCase:h=H%
  189. SET UpCase=%UpCase:i=I%
  190. SET UpCase=%UpCase:j=J%
  191. SET UpCase=%UpCase:k=K%
  192. SET UpCase=%UpCase:l=L%
  193. SET UpCase=%UpCase:m=M%
  194. SET UpCase=%UpCase:n=N%
  195. SET UpCase=%UpCase:o=O%
  196. SET UpCase=%UpCase:p=P%
  197. SET UpCase=%UpCase:q=Q%
  198. SET UpCase=%UpCase:r=R%
  199. SET UpCase=%UpCase:s=S%
  200. SET UpCase=%UpCase:t=T%
  201. SET UpCase=%UpCase:u=U%
  202. SET UpCase=%UpCase:v=V%
  203. SET UpCase=%UpCase:w=W%
  204. SET UpCase=%UpCase:x=X%
  205. SET UpCase=%UpCase:y=Y%
  206. SET UpCase=%UpCase:z=Z%
  207. GOTO:EOF
  208.  
  209.  
  210. :Syntax
  211. ECHO.
  212. ECHO Yesterday.bat,  Version 3.10 for Windows NT
  213. ECHO Display yesterday's, today's and tomorrow's dates in sorted and local format
  214. ECHO.
  215. IF     "%OS%"=="Windows_NT" CALL :UpCase %~n0
  216. IF     "%OS%"=="Windows_NT" ECHO Usage:  %UpCase%
  217. IF NOT "%OS%"=="Windows_NT" ECHO Usage:  %0
  218. ECHO.
  219. ECHO Notes:  Environment variables Yesterday, Today and Tomorrow are set to the
  220. ECHO         sorted date values found.
  221. ECHO         Adapted for Windows XP with help from Kailash Chanduka.
  222. ECHO         Local date code by Frederic Guigand and Rob van der Woude.
  223. ECHO         Julian date math algorithms based on Fliegel-Van Flandern algorithm
  224. ECHO         from the Astronomical Almanac, provided by Doctor Fenton on the Math
  225. ECHO         Forum (http://mathforum.org/library/drmath/view/51907.html), and
  226. ECHO         converted to batch code by Ron Bakowski.
  227. ECHO.
  228. ECHO Written by Rob van der Woude
  229. ECHO http://www.robvanderwoude.com
  230.  
  231. IF "%OS%"=="Windows_NT" COLOR 00
  232.  

page last uploaded: 2017-08-21, 14:26