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. ECHO.
  3.  
  4. :: Check Windows version (XP Pro or later)
  5. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  6.  
  7. :: Check command line argument (one mandatory)
  8. IF NOT "%~1"=="" GOTO Syntax
  9.  
  10. :: Check if WMIC is available
  11. WMIC.EXE Alias /? >NUL 2>&1 || GOTO Syntax
  12.  
  13. :: Localize variables
  14. SETLOCAL ENABLEDELAYEDEXPANSION
  15.  
  16. :: Reset/initialize the variables used
  17. SET DatePart=
  18. SET Day=
  19. SET Month=
  20. SET Year=
  21. SET Error=0
  22.  
  23. :: Get current day, month, year
  24. CALL :Today
  25.  
  26. :: Determine if we have a leap year
  27. CALL :LeapYear
  28.  
  29. :: Calculate the day of the year
  30. CALL :DayOfYear
  31.  
  32. :: Calculate the ISO week number
  33. CALL :WeekOfYear
  34.  
  35. :: Display result
  36. ECHO.%Week%
  37.  
  38. :: Check for errors trapped by WMIC
  39. IF "%Error%"=="1" (
  40. 	ENDLOCAL
  41. 	GOTO Syntax
  42. )
  43.  
  44. :: Done
  45. ENDLOCAL & SET Week=%Week%
  46. EXIT /B %Week%
  47.  
  48.  
  49. :DayOfYear
  50. :: Fill array with cumulative number of days of past months
  51. SET /A DaysPast.1  = 0
  52. SET /A DaysPast.2  = %DaysPast.1%  + 31
  53. SET /A DaysPast.3  = %DaysPast.2%  + 28 + %LeapYear%
  54. SET /A DaysPast.4  = %DaysPast.3%  + 31
  55. SET /A DaysPast.5  = %DaysPast.4%  + 30
  56. SET /A DaysPast.6  = %DaysPast.5%  + 31
  57. SET /A DaysPast.7  = %DaysPast.6%  + 30
  58. SET /A DaysPast.8  = %DaysPast.7%  + 31
  59. SET /A DaysPast.9  = %DaysPast.8%  + 31
  60. SET /A DaysPast.10 = %DaysPast.9%  + 30
  61. SET /A DaysPast.11 = %DaysPast.10% + 31
  62. SET /A DaysPast.12 = %DaysPast.11% + 30
  63. SET /A DayOfYear   = !DaysPast.%Month%! + %Day%
  64. GOTO:EOF
  65.  
  66.  
  67. :LeapYear
  68. SET LeapYear=0
  69. SET /A "Test = %Year% %% 4"
  70. IF %Test% EQU 0 SET LeapYear=1
  71. SET /A "Test = %Year% %% 100"
  72. IF %Test% EQU 0 SET LeapYear=0
  73. SET /A "Test = %Year% %% 400"
  74. IF %Test% EQU 0 SET LeapYear=1
  75. GOTO:EOF
  76.  
  77.  
  78. :Today
  79. FOR /F "skip=1 tokens=1-4" %%A IN ('WMIC Path Win32_LocalTime Get Day^,DayOfWeek^,Month^,Year /Format:table 2^>NUL ^|^| SET Error=1') DO (
  80. 	IF "!Day!"==""       SET Day=%%A
  81. 	IF "!DayOfWeek!"=="" SET DayOfWeek=%%B
  82. 	IF "!Month!"==""     SET Month=%%C
  83. 	IF "!Year!"==""      SET Year=%%D
  84. )
  85. IF %DayOfWeek% EQU 0 SET DayOfWeek=7
  86. GOTO:EOF
  87.  
  88.  
  89. :WeekOfYear
  90. SET /A ThisWeeksSunday = %DayOfYear% - %DayOfWeek% + 7
  91. SET /A Week = %ThisWeeksSunday% / 7
  92. SET /A FirstThursday = %ThisWeeksSunday% - 7 * %Week% + 4
  93. IF %FirstThursday% GTR 7 SET /A Week -= 1
  94. GOTO:EOF
  95.  
  96.  
  97. :Syntax
  98. ECHO WeekXP.bat, Version 1.01 for Windows XP Professional and later
  99. ECHO Returns the ISO week number for the current date
  100. ECHO.
  101. ECHO Usage:   WEEK
  102. ECHO.
  103. ECHO Notes:   The value returned is numeric, without leading zeros.
  104. ECHO          The value is displayed on screen, returned as "errorlevel"
  105. ECHO          and stored in environment variable %%Week%%.
  106. ECHO          Week 0 means the last week (52 or 53) of the previous year.
  107. ECHO.
  108. ECHO Written by Rob van der Woude
  109. ECHO http://www.robvanderwoude.com
  110.  

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