Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for week.bat

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

  1. @ECHO OFF
  2. :: Week.bat, Version 1.01 for Windows NT
  3. :: Display the number of the current week
  4. ::
  5. :: This batch file assumes English DATE command output with weekday, e.g. "Mon 10-10-2014"
  6. ::
  7. :: Credits: Error in DoY function fixed by Marcin Ryczywolski
  8. ::
  9. :: Written by Rob van der Woude
  10. :: http://www.robvanderwoude.com
  11.  
  12.  
  13. :: Use local copy of environment and enable command extensions
  14. VERIFY OTHER 2>nul
  15. SETLOCAL ENABLEEXTENSIONS
  16. IF ERRORLEVEL 1 (
  17. 	ECHO Unable to enable command extensions
  18. 	GOTO End
  19. )
  20.  
  21. :: Export registry settings for date format to a temporary file
  22. START /W REGEDIT /E %TEMP%.\_TEMP.REG "HKEY_CURRENT_USER\Control Panel\International"
  23. :: Read date format from the exported data
  24. FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND /I "iDate"') DO SET iDate=%%B
  25. FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND /I "sDate"') DO SET sDate=%%B
  26. DEL %TEMP%.\_TEMP.REG
  27. :: Remove quotes
  28. SET iDate=%iDate:"=%
  29. SET sDate=%sDate:"=%
  30.  
  31. :: Convert current date to number of days passed this year,
  32. :: taking into account the date format read from the registry
  33. IF %iDate%==0 FOR /F "tokens=2-4* delims=%sDate% " %%A IN ('DATE/T') DO CALL :DoY %%B %%A %%C
  34. IF %iDate%==1 FOR /F "tokens=2-4* delims=%sDate% " %%A IN ('DATE/T') DO CALL :DoY %%A %%B %%C
  35. IF %iDate%==2 FOR /F "tokens=2-4* delims=%sDate% " %%A IN ('DATE/T') DO CALL :DoY %%C %%B %%A
  36.  
  37. :: Convert day of week to number
  38. FOR /F "tokens=1 delims= " %%A IN ('DATE/T') DO CALL :DoW %%A
  39.  
  40. :: Calculate number of full weeks passed this year
  41. SET /A Week = %DoY% - %DoW% + 7
  42. SET /A Week /= 7
  43.  
  44. SET Week
  45. GOTO End
  46.  
  47.  
  48. :DoW
  49. SETLOCAL
  50. :: Convert day of week to number (language dependent, modify if necessary!)
  51. IF "%1"=="Mon" SET DoW=1
  52. IF "%1"=="Tue" SET DoW=2
  53. IF "%1"=="Wed" SET DoW=3
  54. IF "%1"=="Thu" SET DoW=4
  55. IF "%1"=="Fri" SET DoW=5
  56. IF "%1"=="Sat" SET DoW=6
  57. IF "%1"=="Sun" SET DoW=7
  58. ENDLOCAL & SET DoW=%DoW%
  59. GOTO:EOF
  60.  
  61.  
  62. :DoY
  63. SETLOCAL
  64. SET DoM=%1
  65. :: Remove leading zero
  66. IF "%DoM:~0,1%"=="0" IF %DoM% GTR 0 SET /A DoM = 1%DoM% - 100
  67. SET Month=%2
  68. :: Remove leading zero
  69. IF "%Month:~0,1%"=="0" IF %Month% GTR 0 SET /A Month = 1%Month% - 100
  70. SET Year=%3
  71.  
  72. :: Determine leap day
  73. SET LeapYear=0
  74. SET /A Leap = %Year% %% 4
  75. IF %Leap% EQU 0 SET LeapYear=1
  76. SET /A Leap = %Year% %% 100
  77. IF %Leap% EQU 0 SET LeapYear=0
  78. SET /A Leap = %Year% %% 400
  79. IF %Leap% EQU 0 SET LeapYear=1
  80.  
  81. :: Add the days of each full month passed
  82. IF %Month% GEQ  1 SET /A Days2Add  =  0
  83. IF %Month% GEQ  2 SET /A Days2Add += 31
  84. IF %Month% GEQ  3 SET /A Days2Add += 28
  85. IF %Month% GEQ  3 SET /A Days2Add += %LeapYear%
  86. IF %Month% GEQ  4 SET /A Days2Add += 31
  87. IF %Month% GEQ  5 SET /A Days2Add += 30
  88. IF %Month% GEQ  6 SET /A Days2Add += 31
  89. IF %Month% GEQ  7 SET /A Days2Add += 30
  90. IF %Month% GEQ  8 SET /A Days2Add += 31
  91. IF %Month% GEQ  9 SET /A Days2Add += 31
  92. IF %Month% GEQ 10 SET /A Days2Add += 30
  93. IF %Month% GEQ 11 SET /A Days2Add += 31
  94. IF %Month% GEQ 12 SET /A Days2Add += 30
  95.  
  96. :: Add it all up and return the calculated value
  97. SET /A DoY = %Days2Add% + %DoM%
  98. ENDLOCAL & SET DoY=%DoY%
  99. GOTO:EOF
  100.  
  101.  
  102. :End
  103. ENDLOCAL
  104.  

page last uploaded: 2017-07-06, 12:37