Discussion:
Get date of recent weekday
(too old to reply)
Tom Del Rosso
2021-09-02 07:34:52 UTC
Permalink
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
Zaidy036
2021-09-02 15:04:50 UTC
Permalink
Post by Tom Del Rosso
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
If you do a lot of date calculations I suggest using Julian dates to
make logic simpler.

Look at <https://www.robvanderwoude.com/datetimentmath.php>

Make two batches <1>DateToJ and <2>DateToC. Run <1>, apply logic, run
<2>, generate folder name.
Tom Del Rosso
2021-09-02 15:51:58 UTC
Permalink
Post by Zaidy036
If you do a lot of date calculations I suggest using Julian dates to
make logic simpler.
Look at <https://www.robvanderwoude.com/datetimentmath.php>
Make two batches <1>DateToJ and <2>DateToC. Run <1>, apply logic, run
<2>, generate folder name.
That's great, although the two routines together are as long as mine.
It's nice that the website calculates values for today and fills them in
automatically. I have javascript disabled by default but that still
worked somehow. I see that today's date is hardcoded in the source, so
the webserver must update it daily.
Herbert Kleebauer
2021-09-02 16:39:12 UTC
Permalink
Post by Tom Del Rosso
Post by Zaidy036
Make two batches <1>DateToJ and <2>DateToC. Run <1>, apply logic, run
<2>, generate folder name.
That's great, although the two routines together are as long as mine.
Here an old batch which calculates the number of days since 1901
without using an IF statement:


@echo off
setlocal disabledelayedexpansion

:: extract the variables %y% %m% %d% from the %date% variable
:: (this depends on the local date format)
:: valid year range: 1901-2099

set /a y=2004
set /a m=3
set /a d=1

call :date2day
set /a w=%w%-1
call :day2date

echo %y% %m% %d%
goto :eof


:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: convert the date in %y% %m% %d% to the number of days (%w%) ::
:: since 1901 (day 0 is 1. Jan. 1901) ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:date2day
set /a w=(%y%-1901)*365+(%y%-1901)/4+%d%-1+(!(%y% %% 4))*(!((%m%-3)^&16))
set /a w=(%w%+(%m%-1)*30+2*(!((%m%-7)^&16))-1+((65611044^>^>(2*%m%))^&3))
echo %y% %m% %d% %w%
goto :eof

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: convert the number of days in %w% to the date (%y% %m% %d%) ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:day2date
set /a x=%w%/1461
set /a w=%w%-%x%*1461
set /a z=%w%/365-((%w%/365)^>^>2)
set /a w=%w%-%z%*365
set /a y=1901+%x%*4+%z%
set /a v=%w%-!(%y% %% 4)
set /a m=!!(%w%/31)+!!(%v%/59)+!!(%v%/90)+!!(%v%/120)+!!(%v%/151)+!!(%v%/181)
set /a m=%m%+!!(%v%/212)+!!(%v%/243)+!!(%v%/273)+!!(%v%/304)+!!(%v%/334)+1
set /a d=%w%+1-(!(%y% %% 4))*(!((%m%-3)^&16))
set /a d=%d%-((%m%-1)*30+2*(!((%m%-7)^&16))-1+((65611044^>^>(2*%m%))^&3))
goto :eof
mokomoji
2021-09-07 18:00:43 UTC
Permalink
Post by Tom Del Rosso
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
@echo off
setlocal
cd /d %~dp0

set "z_day1=mon" ::1
set "z_day2=tue" ::2
set "z_day3=wed" ::3
set "z_day4=thu" ::4
set "z_day5=fri" ::0
set "z_day6=sat" ::1
set "z_day7=sun" ::2

for /f "tokens=1* delims==" %%f in (
'wmic path win32_localtime get dayofweek /value'
) do (
if "%%~g" neq "" (
call set "z_toweek=%%%%%!%z_day%%~g%!%%%%%"
call set z_dayx=%%g
call set /a "z_value1=(%%z_dayx%% %%%%5)"
call set /a "z_value2=!(%%z_dayx%%/5)*2"
call set /a "z_value3=%%z_value1%%+%%z_value2%%
))
echo Monthly do not with
echo Leap years do not with
call echo today "%z_toweek%" "%date:~-2%" day -"%z_value3%" day = last friday

pause
endlocal
mokomoji
2021-09-07 18:19:16 UTC
Permalink
@echo off
setlocal
cd /d %~dp0
set "z_day1=mon" ::1
set "z_day2=tue" ::2
set "z_day3=wed" ::3
set "z_day4=thu" ::4
set "z_day5=fri" ::0
set "z_day6=sat" ::1
set "z_day7=sun" ::2

for /f "tokens=1* delims==" %%f in (
'wmic path win32_localtime get dayofweek /value'
) do (
if "%%~g" neq "" (
call set "z_toweek=%%%%%!%z_day%%~g%!%%%%%"
call set z_dayx=%%g
call set /a "z_value1=(%%z_dayx%% %%%%5)"
call set /a "z_value2=!(%%z_dayx%%/5)*2"
call set /a "z_value3=%%z_value1%%+%%z_value2%%
))
echo Monthly do not with
echo Leap years do not with
call echo today "%z_toweek%" "%date:~-2%" day -"%z_value3%" day = last friday

forfiles /d -%z_value3% /c "cmd /c echo @file"

pause
endlocal


w.t.perfect~!!!
mokomoji
2021-09-07 18:39:23 UTC
Permalink
today sample folder find test

@echo off
setlocal
cd /d %~dp0
set "z_day1=mon" ::1
set "z_day2=tue" ::2
set "z_day3=wed" ::3
set "z_day4=thu" ::4
set "z_day5=fri" ::0
set "z_day6=sat" ::1
set "z_day7=sun" ::2

for /f "tokens=1* delims==" %%f in (
'wmic path win32_localtime get dayofweek /value'
) do (
if "%%~g" neq "" (
call set "z_toweek=%%%%%!%z_day%%~g%!%%%%%"
rem today 5
call set z_dayx=5
call set /a "z_value1=(%%z_dayx%% %%%%5)"
call set /a "z_value2=!(%%z_dayx%%/5)*2"
call set /a "z_value3=%%z_value1%%+%%z_value2%%
))
echo Monthly do not with
echo Leap years do not with
call echo today "%z_toweek%" "%date:~-2%" day -"%z_value3%" day = last friday

for /f "tokens=1* delims=-" %%f in (
'forfiles /d -%z_value3% /c "cmd /c echo @isdir-@file"^|find /i "true"'
) do (
echo %%f-%%g
)

pause
endlocal
mokomoji
2021-09-08 08:40:44 UTC
Permalink
i finded week friday before
i'm happy

This might not be useful to everyone else, but it's do not worth mentioning.
I do not numbered the lines this time because a lot of them will wrap.

o_o)/)

ver 4.0

@echo off
setlocal
cd /d "%~dp0"
for /f "tokens=1* delims==" %%f in (
'wmic path win32_localtime get dayofweek /value'
) do (
call set z_dayx=%%g
call echo ""|call find /v "%%z_dayx%%" 2>nul>nul&&(
call set /a "z_value=(%%z_dayx%% %%%% 5)+!(%%z_dayx%% / 5)*2" 2>nul
))

for /f "tokens=1* delims=-" %%f in (
'forfiles /d -%z_value% /c "cmd /c echo @isdir-@file" 2^>nul^|find /i "true"'
) do echo --%%f-%%g--
endlocal
pause

Loading...