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 /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. :: Convert date to Julian
  166. :: Arguments : YYYY MM DD
  167. :: Returns   : Julian date
  168. ::
  169. :: Algorithm based on Fliegel-Van Flandern
  170. :: algorithm from the Astronomical Almanac,
  171. :: provided by Doctor Fenton on the Math Forum
  172. :: (http://mathforum.org/library/drmath/view/51907.html),
  173. :: and converted to batch code by Ron Bakowski.
  174. ::
  175. SET /A Month1 = ( %2 - 14 ) / 12
  176. SET /A Year1  = %1 + 4800
  177. SET /A JDate  = 1461 * ( %Year1% + %Month1% ) / 4 + 367 * ( %2 - 2 -12 * %Month1% ) / 12 - ( 3 * ( ( %Year1% + %Month1% + 100 ) / 100 ) ) / 4 + %3 - 32075
  178. FOR %%A IN (Month1 Year1) DO SET %%A=
  179. GOTO:EOF
  180.  
  181.  
  182. :UpCase
  183. SET UpCase=%1
  184. SET UpCase=%UpCase:a=A%
  185. SET UpCase=%UpCase:b=B%
  186. SET UpCase=%UpCase:c=C%
  187. SET UpCase=%UpCase:d=D%
  188. SET UpCase=%UpCase:e=E%
  189. SET UpCase=%UpCase:f=F%
  190. SET UpCase=%UpCase:g=G%
  191. SET UpCase=%UpCase:h=H%
  192. SET UpCase=%UpCase:i=I%
  193. SET UpCase=%UpCase:j=J%
  194. SET UpCase=%UpCase:k=K%
  195. SET UpCase=%UpCase:l=L%
  196. SET UpCase=%UpCase:m=M%
  197. SET UpCase=%UpCase:n=N%
  198. SET UpCase=%UpCase:o=O%
  199. SET UpCase=%UpCase:p=P%
  200. SET UpCase=%UpCase:q=Q%
  201. SET UpCase=%UpCase:r=R%
  202. SET UpCase=%UpCase:s=S%
  203. SET UpCase=%UpCase:t=T%
  204. SET UpCase=%UpCase:u=U%
  205. SET UpCase=%UpCase:v=V%
  206. SET UpCase=%UpCase:w=W%
  207. SET UpCase=%UpCase:x=X%
  208. SET UpCase=%UpCase:y=Y%
  209. SET UpCase=%UpCase:z=Z%
  210. GOTO:EOF
  211.  
  212.  
  213. :Syntax
  214. ECHO.
  215. ECHO Yesterday.bat,  Version 3.11 for Windows NT and later
  216. ECHO Display yesterday's, today's and tomorrow's dates in sorted and local format
  217. ECHO.
  218. IF     "%OS%"=="Windows_NT" CALL :UpCase %~n0
  219. IF     "%OS%"=="Windows_NT" ECHO Usage:  %UpCase%  [ /DEBUG ]
  220. IF NOT "%OS%"=="Windows_NT" ECHO Usage:  %0
  221. ECHO.
  222. ECHO Where:  /DEBUG    disables @ECHO OFF, thus displaying every command line
  223. ECHO.
  224. ECHO Notes:  Environment variables Yesterday, Today and Tomorrow are set to the
  225. ECHO         sorted date values found.
  226. ECHO         Adapted for Windows XP with help from Kailash Chanduka.
  227. ECHO         Local date code by Frederic Guigand and Rob van der Woude.
  228. ECHO         Julian date math algorithms based on Fliegel-Van Flandern algorithm
  229. ECHO         from the Astronomical Almanac, provided by Doctor Fenton on the Math
  230. ECHO         Forum (http://mathforum.org/library/drmath/view/51907.html), and
  231. ECHO         converted to batch code by Ron Bakowski.
  232. ECHO.
  233. ECHO Written by Rob van der Woude
  234. ECHO http://www.robvanderwoude.com
  235.  
  236. IF "%OS%"=="Windows_NT" COLOR 00
  237.  

page last uploaded: 2021-01-27, 16:12