曜日と時間の組み合わせで条件分岐するバッチファイル

バッチファイル

21:00〜23:59 → 月曜〜金曜 にシャットダウン
00:00〜05:59 → 火曜〜土曜 にシャットダウン

@echo off
setlocal enabledelayedexpansion

:: PowerShellで曜日名を取得(例: Monday, Tuesday...)
for /f %%a in ('powershell -Command "(Get-Date).DayOfWeek"') do (
    set "weekday=%%a"
)

:: 時刻取得(時のみ)
for /F "tokens=1 delims=:" %%a in ("%time%") do (
    set hour=%%a
)

:: 先頭の0を削除
if "!hour:~0,1!"=="0" set hour=!hour:~1!

:: シャットダウン条件チェック
set shutdown=false

:: 21〜23時 → 月〜金
if !hour! GEQ 21 (
    if /I "!weekday!"=="Monday"  set shutdown=true
    if /I "!weekday!"=="Tuesday" set shutdown=true
    if /I "!weekday!"=="Wednesday" set shutdown=true
    if /I "!weekday!"=="Thursday" set shutdown=true
    if /I "!weekday!"=="Friday" set shutdown=true
)

:: 0〜5時 → 火〜土
if !hour! LSS 6 (
    if /I "!weekday!"=="Tuesday" set shutdown=true
    if /I "!weekday!"=="Wednesday" set shutdown=true
    if /I "!weekday!"=="Thursday" set shutdown=true
    if /I "!weekday!"=="Friday" set shutdown=true
    if /I "!weekday!"=="Saturday" set shutdown=true
)

if "!shutdown!"=="true" (
    echo シャットダウンを実行します...
timeout /t 55

    shutdown /s /t 60
) else (
    echo 今はシャットダウン対象外の時間・曜日です。
)

endlocal
timeout /t 55

etc

Posted by eightban