1.

Solve : Set Variable Equal to the Second Wednesday of the Month?

Answer»

Since the date of the second Wednesday of the month changes every month I am unsure of how to reference this day of the month so I can set it equal to a variable. My plan is to add the logic to a script I run every day to maintain the servers. Instead of manually running a Windows Updates check after Patch Tuesday I want my script to determine if it is the right time in the month to fetch updates.

So . . . how can I set a variable equal to the second Wednesday of every month?

Thanks,

MJGet the day, if it's Wednesday then check if the day of the month is between 8 and 14 (inclusive). If it is, bingo!

Untested.A clever solution. Thank you. Is there anything native or a CMD tool that you know of that does this?@echo off
Set SecondWed=False
Echo wscript.echo weekdayname(weekday(Date()), False) ^& "," ^& day(date) ^& "," ^& monthname(month(date),False) > DayDate.vbs
For /f "tokens=1,2,3 delims=," %%A in ('cscript //nologo DayDate.vbs') do (
Set DayName=%%A
Set DayDate=%%B
Set MonthName=%%C
)
Echo Today is %DayName% %DayDate% %MonthName%
If "%DayName%"=="Wednesday" (
If %DayDate% gtr 7 (
If %DayDate% lss 15 (
Set SecondWed=True
)
)
)
Echo Is second Wednesday: %SecondWed%
If "%SecondWed%"=="False" goto End

REM From here to :End will only be executed on the
REM target day

Echo Today is the second Wednesday of the month

:End





%date% holds the date, from there you can do some veriable arrangement to find it.

for me (could be diffrent format for you) %date% = Mon 09/09/2013

Code: [Select]
:find_2nd_Wed
setlocal EnableDelayedExpansion
for /f "tokens=* delims= " %%A in ("%date%") do (
set a=%%A
set b=%%B
)
for /f "tokens=2 delims=\" %%A in ("%b%") do set b=%%B
set /a b+=0

if "%a%"=="Mon" set /a b+=2
if "%a%"=="Tues" set /a b+=1
if "%a%"=="Thur" set /a b+=6
if "%a%"=="Fri" set /a b+=5
if "%a%"=="Sat" set /a b+=4
if "%a%"=="Sun" set /a b+=3

:loop.1
if not %a% GTR 14 (
if not %a% LSS 8 set day=%a%
) else (
goto :loop.1
)

set a=
set b=
goto :eof
This is untested, and some of the day names may be off. But it gives the general IDEA of I was trying to convey.
Quote from: Lemonilla on September 09, 2013, 02:12:51 PM

could be diffrent format for you

What if they live in (say) Europe?
HTTP://ss64.com/nt/date.html Gives a list of date -> locale

Also, you may find 'wmic path win32_localtime get day,dayofweek,weekinmonth' to be useful. According to ss64.com it is on XP Pro-win7.
Code: [Select]@echo off
setlocal EnableDelayedExpansion
wmic path win32_localtime get day,dayofweek,weekinmonth
for /f "tokens=1,2 delims==" %%A in ('wmic path win32_localtime get day^,dayofweek^,weekinmonth /value') do (
set %%A=%%B
)

set dow=%DayOfWeek%
set wim=%WeekInMonth%

Echo day = %day%
echo dow = %dow%
echo wim = %wim%


:loop.dow
if %dow% LSS 3 (
set /a dow+=1
set /a day+=1
)
if %dow% GTR 3 (
set /a dow-=1
set /a day-=1
)
if not %dow% EQU 3 goto :loop.dow

:loop.wim
if %wim% LSS 2 (
set /a wim+=1
set /a day+=7
)
if %wim% GTR 2 (
set /a wim-=1
set /a day-=7
)
if not %wim% EQU 2 goto :loop.wim

echo The second wednesday OCCURS on %day%
Sorry for the double post, just realized I had forgotten to clean up my code before posting it.
Code: [Select]@echo off
setlocal EnableDelayedExpansion
for /f "tokens=1,2 delims==" %%A in ('wmic path win32_localtime get day^,dayofweek^,weekinmonth /value') do (
if not "%%A"=="" if not "%%B"=="" set %%A=%%B
)

set dow=%DayOfWeek%
set wim=%WeekInMonth%

:loop.dow
if %dow% LSS 3 (
set /a dow+=1
set /a day+=1
)
if %dow% GTR 3 (
set /a dow-=1
set /a day-=1
)
if not %dow% EQU 3 goto :loop.dow

:loop.wim
if %wim% LSS 2 (
set /a wim+=1
set /a day+=7
)
if %wim% GTR 2 (
set /a wim-=1
set /a day-=7
)
if not %wim% EQU 2 goto :loop.wim

echo The second Wednesday occurs on %day%.
Thanks guys. As usual I requested help when I needced this right-away but other "hotter" things came up and I haven't gotten a CHANCE to apply your ideas yet. I'll post back for sure when I have. I really appreciate the help.

MJ


Discussion

No Comment Found