Tom Del Rosso
2021-09-02 07:34:52 UTC
This might not be useful to anyone else, but it's worth mentioning. I
numbered the lines this time because a lot of them will wrap.
I have an event that happens every Friday, and I have a batch to make a
folder with a date-name and put weekly files in the folder.
If I run the batch on another day, I want it to refer to the folder
whose name is the date of the last Friday, so this routine provides the
date.
It's easy to change to Monday by shifting the numbers on line 12.
[01] :get_last_friday_date
[02] rem returns variable _last_friday_date in the form YYYY-MM-DD
[03] rem if run on Friday, returns today's date [if Fri=0 is changed to
Fri=7 then it would return last week's]
[04] rem month lengths from Dec to Nov days-28
[05] set "_MonthLengths=x330323233232"
[06] for /f "tokens=1-4 delims=/ " %%a in ("%date%") do (
[07] set "_weekday=%%a"
[08] set /a _month=1%%b - 100
[09] set /a _day=1%%c - 100
[10] set /a _year=%%d
[11] )
[12] set /a Sun=2, Mon=3, Tue=4, Wed=5, Thu=6, Fri=0, Sat=1
[13] call set /a _weekdaynumber=%%%_weekday%%%
[14] set /a _last_friday_day = _day - _weekdaynumber
[15] set /a _last_friday_month = _month
[16] set /a _last_friday_year = _year
[17] if %_last_friday_day% LSS 1 (
[18] call set /a "_last_friday_day += 28 +
%%_MonthLengths:~%_month%,1%% + !(_year %%%% 4) * !(_month-3)"
[19] set /a "_last_friday_month = (_month + 10) %% 12 + 1"
[20] )
[21] if %_last_friday_month% EQU 12 if not %_month% EQU 12 set /a
_last_friday_year -= 1
[22] set "_last_friday_month=0%_last_friday_month%"
[23] set "_last_friday_month=%_last_friday_month:~-2,2%"
[24] set "_last_friday_day=0%_last_friday_day%"
[25] set "_last_friday_day=%_last_friday_day:~-2,2%"
[26] set
"_last_friday_date=%_last_friday_year%-%_last_friday_month%-%_last_friday_day%"
[27] goto :eof
numbered the lines this time because a lot of them will wrap.
I have an event that happens every Friday, and I have a batch to make a
folder with a date-name and put weekly files in the folder.
If I run the batch on another day, I want it to refer to the folder
whose name is the date of the last Friday, so this routine provides the
date.
It's easy to change to Monday by shifting the numbers on line 12.
[01] :get_last_friday_date
[02] rem returns variable _last_friday_date in the form YYYY-MM-DD
[03] rem if run on Friday, returns today's date [if Fri=0 is changed to
Fri=7 then it would return last week's]
[04] rem month lengths from Dec to Nov days-28
[05] set "_MonthLengths=x330323233232"
[06] for /f "tokens=1-4 delims=/ " %%a in ("%date%") do (
[07] set "_weekday=%%a"
[08] set /a _month=1%%b - 100
[09] set /a _day=1%%c - 100
[10] set /a _year=%%d
[11] )
[12] set /a Sun=2, Mon=3, Tue=4, Wed=5, Thu=6, Fri=0, Sat=1
[13] call set /a _weekdaynumber=%%%_weekday%%%
[14] set /a _last_friday_day = _day - _weekdaynumber
[15] set /a _last_friday_month = _month
[16] set /a _last_friday_year = _year
[17] if %_last_friday_day% LSS 1 (
[18] call set /a "_last_friday_day += 28 +
%%_MonthLengths:~%_month%,1%% + !(_year %%%% 4) * !(_month-3)"
[19] set /a "_last_friday_month = (_month + 10) %% 12 + 1"
[20] )
[21] if %_last_friday_month% EQU 12 if not %_month% EQU 12 set /a
_last_friday_year -= 1
[22] set "_last_friday_month=0%_last_friday_month%"
[23] set "_last_friday_month=%_last_friday_month:~-2,2%"
[24] set "_last_friday_day=0%_last_friday_day%"
[25] set "_last_friday_day=%_last_friday_day:~-2,2%"
[26] set
"_last_friday_date=%_last_friday_year%-%_last_friday_month%-%_last_friday_day%"
[27] goto :eof