Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for setdate.vbs

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

  1. Option Explicit
  2.  
  3. Dim dtmNow
  4. Dim strDay, strDoW, strDoY, strMonth, strQuarter, strWeek, strYear
  5. Dim wshShell, wshSystemEnv
  6.  
  7. Set wshShell = CreateObject( "WScript.Shell" )
  8.  
  9. If WScript.Arguments.Count > 0 Then Syntax
  10.  
  11. Set wshSystemEnv = wshShell.Environment( "SYSTEM" )
  12.  
  13. ' Take a "snapshot" of the current date and time,
  14. ' because setting the variables may take quite a while
  15. dtmNow = Now
  16.  
  17. ' Set the "components" in environment variables
  18. strDay     = Right( "0" & DatePart( "d", dtmNow ), 2 )
  19. strDoW     = DatePart( "w", dtmNow )
  20. strDoY     = Right( "00" & DatePart( "y", dtmNow ), 3 )
  21. strMonth   = Right( "0" & DatePart( "m", dtmNow ), 2 )
  22. strQuarter = DatePart( "q", dtmNow )
  23. strWeek    = Right( "0" & DatePart( "ww", dtmNow ), 2 )
  24. strYear    = DatePart( "yyyy", dtmNow )
  25.  
  26. wshSystemEnv( "Date.Day"     ) = strDay
  27. wshSystemEnv( "Date.DoW"     ) = strDoW
  28. wshSystemEnv( "Date.DoY"     ) = strDoY
  29. wshSystemEnv( "Date.Month"   ) = strMonth
  30. wshSystemEnv( "Date.Quarter" ) = strQuarter
  31. wshSystemEnv( "Date.Week"    ) = strWeek
  32. wshSystemEnv( "Date.Year"    ) = strYear
  33.  
  34. Set wshSystemEnv = Nothing
  35. Set wshShell     = Nothing
  36.  
  37.  
  38. Sub Syntax
  39. 	WScript.Echo vbCrLf _
  40. 	           & "SetDate.vbs,  Version 1.10" _
  41. 	           & vbCrLf _
  42. 	           & "Set the current date in a set of environment variables" _
  43. 	           & vbCrLf & vbCrLf _
  44. 	           & "Usage:  CSCRIPT  SETDATE.VBS" _
  45. 	           & vbCrLf & vbCrLf _
  46. 	           & "Notes:  Schedule this command every day at midnight AND at system startup." _
  47. 	           & vbCrLf _
  48. 	           & "        From then on, all batch files and programs will have a set of" _
  49. 	           & vbCrLf _
  50. 	           & "        environment variables available that can be used in log files names" _
  51. 	           & vbCrLf _
  52. 	           & "        etcetera." _
  53. 	           & vbCrLf _
  54. 	           & "        The following variables will be available (with the number of digits" _
  55. 	           & vbCrLf _
  56. 	           & "        between parentheses):" _
  57. 	           & vbCrLf _
  58. 	           & "        Date.Day     (2)    Date.DoW  (1)    Date.DoY  (3)    Date.Month (2)" _
  59. 	           & vbCrLf _
  60. 	           & "        Date.Quarter (1)    Date.Week (2)    Date.Year (4)" _
  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 uploaded: 2017-07-06, 12:37