<http://www.netikka.net/tsneti/info/tscmd149.htm>
Copyright © 2003-2010 by Prof. Timo Salmi  
Last modified Mon 10-May-2010 14:57:56

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



149} How to calculate a date's week number with a pure cmd script?

  @echo off & setlocal enableextensions disabledelayedexpansion
  if "%~3"=="" (
    echo Usage: %~0 DD MM YYYY
    echo No leading zeros!
    goto :EOF)
  ::
  :: Get the date
  set day=%~1
  set month=%~2
  set year=%~3
  ::
  :: Call the day ordinal number subroutine
  call :JDweekNumber %day% %month% %year% WeekNumber
  ::
  :: Display the result
  echo %day%.%month%.%year% Week number %WeekNumber%
  endlocal & goto :EOF
  ::
  :: =====================================
  :: Subroutine: Calculate the week number
  :: The weeks in the algorithm start on Mondays
  :JDweekNumber day month year return_
  setlocal enableextensions enabledelayedexpansion
  if %2 LEQ 2 (
    set /a a=%3-1
    set /a b=!a!/4-!a!/100+!a!/400
    set /a c=^(!a!-1^)/4-^(!a!-1^)/100+^(!a!-1^)/400
    set /a s=!b!-!c!
    set /a e=0
    set /a f=%1-1+31*^(%2-1^)
    ) else (
    set /a a=%3
    set /a b=!a!/4-!a!/100+!a!/400
    set /a c=^(!a!-1^)/4-^(!a!-1^)/100+^(!a!-1^)/400
    set /a s=!b!-!c!
    set /a e=!s!+1
    set /a f=%1+^(153*^(%2-3^)+2^)/5+58+!s!
    )
  set /a g=(%a%+%b%) %% 7
  set /a d=(%f%+%g%-%e%) %% 7
  set /a n=%f%+3-%d%
  set return_=
  if %n% LSS 0 set /a return_=53
  if %n% GTR 364+%s% set /a return_=1
  if not defined return_ set /a return_=%n%/7+1
  endlocal & set "%4=%return_%" & goto :EOF

The output might be e.g.
  C:\_D\TEST>cmdfaq 17 9 2006
  17.9.2006 Week number 37
  C:\_D\TEST>cmdfaq 18 9 2006
  18.9.2006 Week number 38
Note that 17.9.2006 is a Sunday and 18.9.2006 a Monday.

If a Visual Basic Script (VBScript) aided command line script is acceptable then
  @echo off & setlocal enableextensions
  if "%~3"=="" (
    echo Usage: %~0 DD MM YYYY
    goto :EOF)
  ::
  set day=%~1
  set month=%~2
  set year=%~3
  ::
  set vbs_=%temp%\tmp$$$.vbs
  echo Wscript.Echo DatePart("ww","%year%.%month%.%day%",vbMonday,vbFirstFourDays)>"%vbs_%"
  for /f %%w in ('cscript //nologo "%vbs_%"') do set wk_=%%w
  echo wk_=%wk_%
  for %%f in ("%vbs_%") do if exist %%f del %%f
  endlocal & goto :EOF