none
Day of week in cmd batch file

    Question

  • I have a 'DOS' batch file which makes overnight backups. I don't want to rewrite it in Powershell unless I have to.

    I want to keep backups for each day of the week, but I can't see how to do that on Server 2008 R2 - the DATE command no longer returns the day, just the date. I have seen how to do it in Windows 2000 etc, but that doesn't work any more.

    Is there any way to do this please?

    Tuesday, March 08, 2011 4:14 PM

Answers

  • Hi,

    I wrote a utility called DateX.exe a while back that works regardless of locale: http://www.westmesatech.com/sst.html

    You can use code like this in a script:

    for /f %%d in ('datex -f ddd') do set DOW=%%d

    In the above example, the DOW environment variable will get set to 'Mon', 'Tue', etc.

    HTH,

    Bill

    • Marked as answer by troy99 Tuesday, March 15, 2011 12:28 PM
    Friday, March 11, 2011 3:28 PM
  • As MichalGajda said :

    In Windows XP if you go to Control Panel - Regional and Language Option - Customize - Date - Short Date

    Change to "ddd, MM-dd-yyyy" (three letter day name) or "dddd, MM-dd-yyyy" (full day name) and you get day of week after using date /t

    This works in English UK too. I have tested on XP:xp screenshot

    In Windows 7 it is the Additional Settings button.

    On Windows 7:Windows 7 screenshot

    If you want the day of week as a number you could scan an array of Mon, Tue, Wed, etc.... to get the position of the returned value, Tue in this case being 1.

    Hope this helps.


    Tuesday, October 09, 2012 9:36 AM

All replies

  • Can you be specific? Day of week as in "Mon", "Tue", etc. as a string, or the day number of the month (1-31), or...?

    What do you mean by "that doesn't work any more"? What, specifically, doesn't work? What does your existing script look like?

    Bill

    Thursday, March 10, 2011 11:02 PM
  • Hi Bill,

    I mean that I can't get Mon, Tue etc. Server 2008 R2 just returns 11/03/2011.

    Older versions of Windows could return the day as well as part of the same "date /t" command.

    Any cmd.exe batch file method to get the day of the week would be great.

    Friday, March 11, 2011 9:38 AM
  • Date works the same like in older system.
    But maybe you have other customize regional setting.

    Check this Control Panel - Regional and Language Option - Customize - Date - Short Date
    Change to "ddd, MMMM-dd-yyyy" or "dddd, MMMM-dd-yyyy" and you get day of week after use date /t

    If you have core system then change it in registry: HKCU\Control Panel\International

    Friday, March 11, 2011 11:15 AM
  • Hi Michal,

    Many thanks for your reply.

    I didn't mention that I am in the UK, with UK set as the Locale and using UK format for the Date & Time.

    Unfortunately, Microsoft in their wisdom don't offer the 'dddd' option unless I change the date format to US which I don't want to do.

    Friday, March 11, 2011 1:59 PM
  • Hi,

    I wrote a utility called DateX.exe a while back that works regardless of locale: http://www.westmesatech.com/sst.html

    You can use code like this in a script:

    for /f %%d in ('datex -f ddd') do set DOW=%%d

    In the above example, the DOW environment variable will get set to 'Mon', 'Tue', etc.

    HTH,

    Bill

    • Marked as answer by troy99 Tuesday, March 15, 2011 12:28 PM
    Friday, March 11, 2011 3:28 PM
  • Hi Bill,

    Many thanks.

    Cheers,

    Gordon

    Tuesday, March 15, 2011 12:28 PM
  • Just for you to know, that you can use the same principal 'For /F ' followed by your command, and to gain in flexibility of Date format that you wish. Here is the full code of DateCalc.VBS that I am using since 4 years. The beauty of this is you can change the way you return date as you wish. Another feature, the one that I wanted most, is date calculation offset.

    Let see you wish to generate a Log file of yesterday Events, you can use CScript DateCalc.VBS /D:<Format>YYYY.MMMM.DD.DOW /O:-1<for yesterday> //NoLogo. So let say that we are Tuesday March 15 2011, and want to assign a FileName Variable with yesterday Date, you will do:
    For /F %A In ('CScript DateCalc.VBS /D:<Format>YYYY.MMMM.DD.DOW /O:-1<for yesterday> //NoLogo') Do Set fName=%A don't forget in a batch '%%' Then fName would be equal to 2011.March.14.mon.  Check all the options you wish, and you can modify the Date Language as you wish, add options to support something else. So far it works with all release of windows that I support. No hidden code....  secure....

    Wish it be a good alternative for you.

    Here is DateCalc.VBS Code
     '
    ' Author........: Andre Desnoyers
    ' File..........: DateCalc.VBS
    ' Description...: Return the date of desired offset date, with the requested format
    '                 And Time if requested by /T:12 or 24 format
    ' Parameters....:
    ' 1. Mandatory, /D:<Date Format>
    '         YYYY, YY, AAAA, AA...: Year format (2008, or 08)
    '         MMMM, MMM, MM, M.....: Month format (MARCH, MARS, MAR, 03, 3)
    '         DD, D, JJ, J.........: Day format (01, 1)
    '         DOW, DOWL, JDS, JDSL.: Day of Week (sat, saturday, sam, samedi)
    '
    ' 2. Option, /O:<Offset>, is a number precedes by + or -
    '         We add this Offset to the actual date, if it is a -, it becomes a
    '         substraction
    '
    ' 3. Option. /T:<12 or 24>, will return time in a 12 hour format (AM/PM) or a
    '         24 hour format
    '
    ' Pre-requisite.: None
    '
    ' Version.......: 1.0
    '
    ' ----------------------
    ' =============================================================================
    OptionExplicit
    'On Error Resume Next
    ' Array Variables
    Dim aDOW, aMonth, aOutDate( 4 ), aOrder( 4 ), aLongDOW, aLongMonth
    ' Boolean variables
    Dim bLang
    ' Character variables
    Dim cDateFormat, cDay, cDOW, cFileName, cMonth, cOptions, cOutDate, cParamValue, _
        cSep, cSwitch, cTime, cTimePeriod, cTmp, cYear
    ' Objects variables
    Dim oArgs
    Set oArgs = WScript.Arguments
    ' Numeric variables
    Dim i, j, nCalcDAte, nDayPosi, nDOWPosi, nFileSize, nHour, nMinute, nMonthPosi, _
        nOffset, nSecond, nSwap, nTimeFormat, nYearPosi
    ' Constant vaariables
    ' =============================================================================
    For i = 0To4
      aOrder( i ) = 0
    Next
    bLang = 9
    cSep = ""
    cTime = ""
    cTimePeriod = ""
    nYearPosi = 0
    nMonthPosi = 0
    nTimeFormat = 24
    nDayPosi = 0
    nDOWPosi = 0
    nCalcDate = 0
    ' BEGIN_Processing Parameters
    For i = 0to oArgs.Count -1
      cOptions = ""
      cSwitch = ""
      cParamValue = Trim( oArgs( i ))
      IfInStr( 1, cParamValue, ":" )>0Then
        cOptions = Mid( cParamValue, InStr( 1, cParamValue, ":" ) +1, _
                   Len( cParamValue )-InStr( 1, cParamValue, ":" ) )
        cSwitch = UCase( Mid( cParamValue, 1, InStr( 1, cParamValue, ":" ) -1))
       Else
        cSwitch = UCase( cParamValue )
      EndIf
      SelectCase cSwitch
        ' /D : DateFormat
        Case"/D"
          cDateFormat = cOptions
          IfInStr( 1, cDateFormat, "-", 1 ) >0Then cSep = "-"
          IfInStr( 1, cDateFormat, "_", 1 ) >0Then cSep = "_"
          IfInStr( 1, cDateFormat, ".", 1 ) >0Then cSep = "."
          IfInStr( 1, cDateFormat, "/", 1 ) >0Then cSep = "/"

        ' /O : Offset
        Case"/O"
          nOffset = cOptions
          nCalcDate = Now + nOffset

        ' /L : Langage (0 = English, 1 = French)
        Case"/L"
          If ( cOptions = "F"OR cOptions = "1" ) Then
            bLang = 1
           Else
            bLang = 0
          EndIf

        ' /T : Show Time as a second output on same line
        Case"/T"
          If cOptions <> ""Then
            nTimeFormat = cOptions
          EndIf
          cTime=Now()
          nHour=Hour( cTime )
          If nHour > 11And nTimeFormat = 12Then
            nHour = nHour -12
            cTimePeriod="PM"
           Else
            cTimePeriod="AM"
          EndIf
          IfLen(nHour)=1Then nHour="0" & nHour
          nMinute=Minute( cTime )
          IfLen(nMinute)=1Then nMinute="0" & nMinute
          nSecond=Second( cTime )
          IfLen(nSecond)=1Then nSecond="0" & nSecond
          cTime = ""
          If nTimeFormat = "12"Then
            cTime=Trim( nHour & cSep & nMinute & cSep & nSecond ) & cSep & cTimePeriod
           Else
            cTime=nHour & cSep & nMinute & cSep & nSecond
          EndIf

        ' Else
        CaseElse
          WScript.Echo"NOT A SUPPORTED PARAMETERS: " & cParamValue

      EndSelect
    Next
    ' END_Processing Parameters
    If nCalcDate = 0Then
      nCalcDate = Now
    EndIf
    '----------------------------------------------------
    aDOW = Array( "sun", "mon", "tue", "wed", "thu", "fri", "sat" )
    aLongDOW = Array( "sunday", "monday", "tuesday", "wednesday", "thursday", _
                      "friday", "saturday" )
    aMonth = Array( "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", _
                    "AUG", "SEP", "OCT", "NOV", "DEC" )
    aLongMonth = Array( "January", "February", "March", "April", "May", "June", _
                        "July", "August", "September", "October", "November", "December" )
    '----------------------------------------------------
    ' Day of Week Treatment
    cDOW = Weekday( nCalcDate, 1 )
    IfInStr( 1, cDateFormat, "DOWL", 1 ) > 0Then
      cDOW = aLongDOW( cDOW - 1 )
      nDOWPosi = InStr( 1, cDateFormat, "DOWL", 1 )
      cDateFormat = Replace( cDateFormat, "DOWL", "    " )
    Else
      IfInStr( 1, cDateFormat, "DOW", 1 ) > 0Then
        cDOW = aDOW( cDOW - 1 )
        nDOWPosi = InStr( 1, cDateFormat, "DOW", 1 )
        cDateFormat = Replace( cDateFormat, "DOW", "   " )
       Else
        cDOW = ""
      EndIf
    EndIf
    aOutDate( 3 ) = cDOW
    aOrder( 3 ) = nDOWPosi
    '----------------------------------------------------
    ' Year Treatment
    cYear = Year( nCalcDate )
    IfInStr( 1, cDateFormat, "YYYY", 1 ) > 0Then
      nYearPosi = InStr( 1, cDateFormat, "YYYY", 1 )
    Else
      IfInStr( 1, cDateFormat, "YY", 1 ) > 0Then
        cYear = Mid( cYear, 3, 2 )
        nYearPosi = InStr( 1, cDateFormat, "YY", 1 )
       Else
        cYear = ""
      EndIf
    EndIf
    aOutDate( 0 ) = cYear
    aOrder( 0 ) = nYearPosi
    '----------------------------------------------------
    ' Day Treatment
    cDay = Day( nCalcDate )
    IfInStr( 1, cDateFormat, "DD", 1 ) > 0Then
      IfLen( cDay ) = 1Then
        cDay = "0" & cDay
      EndIf
      nDayPosi = InStr( 1, cDateFormat, "DD", 1 )
    Else
      IfInStr( 1, cDateFormat, "D", 1 ) > 0Then
        nDayPosi = InStr( 1, cDateFormat, "D", 1 )
       Else
        cDay = ""
      EndIf
    EndIf
    aOutDate( 2 ) = cDay
    aOrder( 2 ) = nDayPosi
    '----------------------------------------------------
    ' Month Treatment
    cMonth = Month( nCalcDate )
    IfInStr( 1, cDateFormat, "MMMM", 1 ) > 0Then
      cMonth = aLongMonth( cMonth - 1 )
      nMonthPosi = InStr( 1, cDateFormat, "MMMM", 1 )
    Else
      IfInStr( 1, cDateFormat, "MMM", 1 ) > 0Then
        cMonth = aMonth( cMonth - 1 )
        nMonthPosi = InStr( 1, cDateFormat, "MMM", 1 )
       Else
        IfInStr( 1, cDateFormat, "MM", 1 ) > 0Then
          IfLen( cMonth ) = 1Then
            cMonth = "0" & cMonth
          EndIf
          nMonthPosi = InStr( 1, cDateFormat, "MM", 1 )
         Else
          IfInStr( 1, cDateFormat, "M", 1 ) > 0Then
            nMonthPosi = InStr( 1, cDateFormat, "M", 1 )
           Else
            cMonth = ""
          EndIf
        EndIf
      EndIf
    EndIf
    aOutDate( 1 ) = cMonth
    aOrder( 1 ) = nMonthPosi
    '----------------------------------------------------
    ' BUBBLE SORT (Put the fields in the requests order)
    nSwap = 0
    i = 0
    While i<4
      j = i +1
      While j<4
        If aOrder( i ) > aOrder( j ) Then
          cTmp = aOrder( j )
          aOrder( j ) = aOrder( i )
          aOrder( i ) = cTmp
          cTmp = aOutDate( j )
          aOutDate( j ) = aOutDate( i )
          aOutDate( i ) = cTmp
          nSwap = nSwap +1
        EndIf
        j = j +1
      WEnd
      i = i +1
    WEnd
    '----------------------------------------------------
    ' OutPut String Construction
    cOutDate = ""
    i = 0
    DoWhile i < 4
      IfNot aOrder( i ) = 0Then
        If cOutDate <> ""Then
          cOutDate = cOutDate & cSep & aOutDate( i )
         Else
          cOutDate = aOutDate( i )
        EndIf
      EndIf
      i = i +1
    Loop
    If cTime <> ""Then
      WScript.Echo cOutDate & cSep & cTime
    Else
      WScript.Echo cOutDate
    EndIf

    • Edited by ADesnoyers Tuesday, March 15, 2011 3:48 PM error in code
    Tuesday, March 15, 2011 3:35 PM
  • As MichalGajda said :

    In Windows XP if you go to Control Panel - Regional and Language Option - Customize - Date - Short Date

    Change to "ddd, MM-dd-yyyy" (three letter day name) or "dddd, MM-dd-yyyy" (full day name) and you get day of week after using date /t

    This works in English UK too. I have tested on XP:xp screenshot

    In Windows 7 it is the Additional Settings button.

    On Windows 7:Windows 7 screenshot

    If you want the day of week as a number you could scan an array of Mon, Tue, Wed, etc.... to get the position of the returned value, Tue in this case being 1.

    Hope this helps.


    Tuesday, October 09, 2012 9:36 AM
  • Many thanks Webstuff.

    I was puzzled lokking at your screenshots - I didn't have the 'ddd dd/MM/yyyy' option. It had never ocurred to me that I could type in 'ddd ', but when I did - it worked. Thanks again.

    PS It also works on Server 2008 R2.

    Tuesday, October 09, 2012 10:05 AM
  • Be careful not to use a global solution to solve a local problem. Changing the regional settings will affect all programs that use those settings. Changing them for all programs will likely have unintended side effects.

    Bill

    Tuesday, October 09, 2012 9:20 PM