How do I test for a particular day of week in a .bat script?

7,667

Yes, you can.

The following command can be used to get the day:

set day=%date:~0,2%

Now %day% contains the date.

Depending on your locale, it may be possible that the date returns a 3 letter day instead of a 2, in which case you would use set day=%date:~0,3%

Explaination: %date% contains a small string that starts with the day, followed by the date in the language used by your OS. The command creates a new variable called day and takes the first 2 characters of the variable %date% as result.

Share:
7,667

Related videos on Youtube

Herb
Author by

Herb

Updated on September 18, 2022

Comments

  • Herb
    Herb over 1 year

    (Win 8.1 Pro 64b on Intel i7)

    All right, I have this fully-automated backup script running daily. However, it turns out, that one of the saved directories (consuming about 25% of the space, and consequently, about the same portion of the total backup time) is modified quite rarely.

    I'd now opt to save the directory in question just once weekly. Only that I am not able to figure out how to obtain the day of week in a .bat file.

    (Of course, I could do what was done on a HP41C back in olden times and do calculations involving day of month, month of year, and year, but well, I think of it as plan B.)

    Is there a way to load the day of week into a .bat variable in a somehow more efficient manner?

    • Admin
      Admin almost 7 years
      Agreed. What is considered good practice? To delete this question?
  • Herb
    Herb almost 7 years
    echo %date% just outputs the date, but no information on the day of week (Sunday, Monday...).
  • LPChip
    LPChip almost 7 years
    Hmm, it does for me. Seems that is part of the locale too then. Bummer. I'll have to figure out how to change the locale to include the day in the %date% variable then.
  • LPChip
    LPChip almost 7 years
    if you run date /t does it shows it then?
  • Herb
    Herb almost 7 years
    No, just the date. (The locale is de-ch, shown as "dd.mm.yyyy".) However, it's hard to believe, that MS would make cmd statements' functionality dependent on locales, when on the same OS. - Speaking about: are you using Win 8.1 as well?
  • LPChip
    LPChip almost 7 years
    I'm using windows 10, but I know from experience that this worked in windows 7, xp, 2000 and earlier even. It shows as ma 29-5-2017 on my computer (dutch), which is identical to the short date setting in my control panel. I've build quite a few scripts around this behavior, and yes I know for sure its based on locale. But strangely, changing that setting and then opening a command prompt does not alter it.
  • LPChip
    LPChip almost 7 years
  • Herb
    Herb almost 7 years
    OffT: So the Dutch get days of week, and the Swiss don't? That's a case for the ECHR! Anyway, thanks for trying. - OnT: Is there possibly a WMIC solution? Not yet familiar enough with it.
  • LPChip
    LPChip almost 7 years
    Actually I think it does work to change the regional settings, it probably just needs a reboot. I can't reboot right now, but will try out tonight when I can.
  • Herb
    Herb almost 7 years