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

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