Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Get date of recent weekday

31 views
Skip to first unread message

Tom Del Rosso

unread,
Sep 2, 2021, 3:40:16 AM9/2/21
to
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

unread,
Sep 2, 2021, 11:04:51 AM9/2/21
to
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

unread,
Sep 2, 2021, 11:52:01 AM9/2/21
to
Zaidy036 wrote:
> 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

unread,
Sep 2, 2021, 12:39:15 PM9/2/21
to
On 02.09.2021 17:51, Tom Del Rosso wrote:
> Zaidy036 wrote:

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

unread,
Sep 7, 2021, 2:00:44 PM9/7/21
to
2021년 9월 2일 목요일 오후 4시 40분 16초 UTC+9에 Tom Del Rosso님이 작성한 내용:
@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

unread,
Sep 7, 2021, 2:19:17 PM9/7/21
to
@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

unread,
Sep 7, 2021, 2:39:24 PM9/7/21
to
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

unread,
Sep 8, 2021, 4:40:45 AM9/8/21
to
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
0 new messages