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

0

(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?

Herb

Posted 2017-05-29T14:26:51.293

Reputation: 673

Question was closed 2017-05-29T17:43:14.150

Agreed. What is considered good practice? To delete this question? – Herb – 2017-05-31T11:24:26.567

Answers

1

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.

LPChip

Posted 2017-05-29T14:26:51.293

Reputation: 42 190

echo %date% just outputs the date, but no information on the day of week (Sunday, Monday...). – Herb – 2017-05-29T14:56:50.323

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 – 2017-05-29T15:01:04.920

if you run date /t does it shows it then? – LPChip – 2017-05-29T15:02:05.103

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? – Herb – 2017-05-29T15:11:09.980

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 – 2017-05-29T15:43:21.153

See also: https://www.sevenforums.com/tutorials/3529-date-format-change.html

– LPChip – 2017-05-29T15:49:05.170

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.

– Herb – 2017-05-29T15:50:04.620

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. – LPChip – 2017-05-29T15:50:56.353

Let us continue this discussion in chat.

– Herb – 2017-05-29T16:26:52.813