Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for week.vbs

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

  1. Option Explicit
  2.  
  3. Dim intWeek, wshShell, wshUserEnv
  4.  
  5. ' Check command line argument(s)
  6. If WScript.Arguments.Named.Count > 0 Then Syntax
  7. With WScript.Arguments.Unnamed
  8. 	If .Count = 1 Then
  9. 		' Check if valid date is specified on the command line
  10. 		If IsDate( .Item(0) ) Then
  11. 			intWeek = DatePart( "ww", .Item(0), vbMonday, vbFirstFourDays )
  12. 		Else
  13. 			Syntax
  14. 		End If
  15. 	ElseIf .Count = 0 Then
  16. 		' Calculate today's week number if no date was specified
  17. 		intWeek = DatePart( "ww", Date, vbMonday, vbFirstFourDays )
  18. 	Else
  19. 		Syntax
  20. 	End If
  21. End With
  22.  
  23. ' Set the result in an environment variable "Week"
  24. Set wshShell = CreateObject( "WScript.Shell" )
  25. Set wshUserEnv = wshShell.Environment( "USER" )
  26. wshUserEnv( "Week" ) = intWeek
  27. Set wshUserEnv = Nothing
  28. Set wshShell   = Nothing
  29.  
  30. ' Display the result
  31. WScript.Echo intWeek
  32.  
  33. ' Exit with the resulting week number as return code
  34. WScript.Quit intWeek
  35.  
  36.  
  37. Sub Syntax
  38. 	WScript.Echo "Week.vbs,  Version 2.00 for Windows Script Host 2.00" _
  39. 	           & vbCrLf _
  40. 	           & "Return the ISO week number for the current or specified date." _
  41. 	           & vbCrLf & vbCrLf _
  42. 	           & "Usage:  CSCRIPT.EXE  //NoLogo  WEEK.VBS  [ date ]" _
  43. 	           & vbCrLf & vbCrLf _
  44. 	           & "Where:  ""date""       is an optional date to calculate the week number for" _
  45. 	           & vbCrLf _
  46. 	           & "                     (default is today)" _
  47. 	           & vbCrLf & vbCrLf _
  48. 	           & "Notes:  The ISO week number will be displayed on screen, returned as return" _
  49. 	           & vbCrLf _
  50. 	           & "        code, and stored in an environment variable %Week%." _
  51. 	           & vbCrLf _
  52. 	           & "        0 means the last week (52 or 53) of the previous year." _
  53. 	           & vbCrLf _
  54. 	           & "        If ambiguous, the date is interpretated according to the computer's" _
  55. 	           & vbCrLf _
  56. 	           & "        regional/international settings." _
  57. 	           & vbCrLf _
  58. 	           & "        To make the date unambiguous, use the ISO format (yyyy-mm-dd)." _
  59. 	           & vbCrLf _
  60. 	           & "        To distinguish between errors and week numbers, -1 is used for errors." _
  61. 	           & vbCrLf & vbCrLf _
  62. 	           & "Written by Rob van der Woude" _
  63. 	           & vbCrLf _
  64. 	           & "http://www.robvanderwoude.com"
  65. 	WScript.Quit -1
  66. End Sub
  67.  

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