Windows環境で、月の勤務最終日にお知らせメッセージを表示させる

  • 2
    いいね
  • 0
    コメント
この記事は最終更新日から1年以上が経過しています。

私の勤め先では月末に提出物を出すのだが、ついつい忘れがちになってしまうので、メッセージを出してほしいと思っていた。
普段はタスクスケジューラでバッチ起動させてメッセージを出しているのだが、勤務最終日=月の最終日では無いのでメッセージを出させるタイミングが難しく悩んでいた。

考えた末、「毎日、決まった時間に勤務最終日かを判定し、勤務最終日であればメッセージを出す」というものにしようと思い、ちょいちょいと書いてみた。
このプログラムを毎日決まった時間にタスクスケジューラで起動させようというわけだ。
普段はバッチで簡単に済ませているのだが、今回は初めてVBSで書いてみた。

なお、ここでの勤務最終日の定義はその月の中で土日祝日でない最後の日である。

check.vbs
Dim thisYear
Dim thisMonth
Dim lastDay
Dim checkDay
Dim checkDate
Dim checkWeekday
Dim lastWeekday
Dim isWeekday

' Get this year and this month
thisYear  = Year(Date)
thisMonth = Month(Date)
' Calculate the last day of this month
lastDay = Day(DateSerial(thisYear, thisMonth + 1, 1) - 1)
' Calculate the last weekday of this month
LastWeekday = Null
checkDay = lastDay
While  IsNull(LastWeekday)
    checkDate = DateSerial(thisYear, thisMonth, checkDay)
    checkWeekday = Weekday(checkDate)
    isWeekday = (checkWeekday <> vbSaturday) And (checkWeekday <> vbSunday) And (isHoliday(checkDate) <> false)
    If isWeekday Then
        LastWeekday = checkDay
    End If 
    checkDay = checkDay - 1
Wend
' If today is the last weekday of this month,display a message
If Day(Date) = LastWeekday Then
    WScript.Echo "月末なので提出物を提出してください。"
End If 

' ------  Holiday confirmation  ------
Function isHoliday(checkDate)

Holidays = Array("2016/1/1","2016/1/11","2016/2/11","2016/3/20","2016/3/21","2016/4/29","2016/5/3","2016/5/4","2016/5/5","2016/7/18","2016/8/11","2016/9/19","2016/9/22","2016/10/10","2016/11/3","2016/11/23","2016/12/23","2017/1/1","2017/1/2","2017/1/9","2017/2/11","2017/3/20","2017/4/29","2017/5/3","2017/5/4","2017/5/5","2017/7/17","2017/8/11","2017/9/18","2017/9/23","2017/10/9","2017/11/3","2017/11/23","2017/12/23","2017/1/1","2017/1/2","2017/1/9","2017/2/11","2017/3/20","2017/4/29","2017/5/3","2017/5/4","2017/5/5","2017/7/17","2017/8/11","2017/9/18","2017/9/23","2017/10/9","2017/11/3","2017/11/23","2017/12/23","2018/1/1","2018/1/8","2018/2/11","2018/2/12","2018/3/21","2018/4/29","2018/4/30","2018/5/3","2018/5/4","2018/5/5","2018/7/16","2018/8/11","2018/9/17","2018/9/23","2018/9/24","2018/10/8","2018/11/3","2018/11/23","2018/12/23","2018/12/24","2019/1/1","2019/1/14","2019/2/11","2019/3/21","2019/4/29","2019/5/3","2019/5/4","2019/5/5","2019/5/6","2019/7/15","2019/8/11","2019/8/12","2019/9/16","2019/9/23","2019/10/14","2019/11/3","2019/11/4","2019/11/23","2019/12/23","2020/1/1","2020/1/13","2020/2/11","2020/3/20","2020/4/29","2020/5/3","2020/5/4","2020/5/5","2020/5/6","2020/7/20","2020/8/11","2020/9/21","2020/9/22","2020/10/12","2020/11/3","2020/11/23","2020/12/23","2021/1/1","2021/1/11","2021/2/11","2021/3/20","2021/4/29","2021/5/3","2021/5/4","2021/5/5","2021/7/19","2021/8/11","2021/9/20","2021/9/23","2021/10/11","2021/11/3","2021/11/23","2021/12/23")

HolidaysCount = UBound(Holidays) - 1
For intCounter = 0 To HolidaysCount
    Holiday = Holidays(intCounter)
    isHoliday = CDate(Holiday) = checkDate
    If isHoliday Then
        Exit For
    End If 
Next

End Function

https://github.com/butackle/checkLastWorkingDay

土日の判定は良かったのだが祝日を判定するのが難しかったので、今回は「2021年までの祝日を配列で放り込んで判定」という、あまりスマートでは無い方法を取った。
ただ、これだと会社独自の休みなども反映させやすいので、今回はコレで良いかなと思っている。