LoginSignup
1
0

More than 3 years have passed since last update.

バッチファイルで指定日時まで待つ

Last updated at Posted at 2020-11-06

バッチファイルで PowerShell を使って指定時間まで待つ。
スケジューラ登録するほどでもない、管理者権限がない ときに。
(Windows10, Window7 等)
(コマンドプロンプト スクリプト sleep wait DOS)

sleep_until.bat


@echo off

for /f "usebackq delims=," %%a in (`powershell "(get-date -hour 22 -minute 0 -second 0).adddays((6 - (get-date).dayofweek.value__)).tostring('yyyy/M/d H:m:s')"`) do set tm=%%a
echo 今度の土曜日22時: %tm%

for /f "usebackq delims=," %%a in (`powershell "(get-date -hour 22 -minute 0 -second 0).adddays((7 - (get-date).dayofweek.value__)).tostring('yyyy/M/d H:m:s')"`) do set tm=%%a
echo 今度の日曜日22時: %tm%

for /f "usebackq delims=," %%a in (`powershell "(get-date -day 1 -hour 22 -minute 0 -second 0).addmonths(1).adddays(-1).tostring('yyyy/M/d H:m:s')"`) do set tm=%%a
echo 今月末日の22時  : %tm%

for /f "usebackq delims=," %%a in (`powershell "(get-date -day 1 -hour 0 -minute 0 -second 0).addmonths(1).tostring('yyyy/M/d H:m:s')"`) do set tm=%%a
echo 来月1日の0時0分: %tm%

echo.
for /f "usebackq delims=," %%a in (`powershell "(get-date).addminutes(1).tostring('yyyy/M/d H:m:s')"`) do set tm=%%a
echo 1分後            : %tm%
echo.
echo %date% %time%
rem tm 時間まで待つ
powershell "$w = [DateTime]'%tm%' ; if ((get-date) -lt $w) {  $w = new-timespan -start (get-date) -end $w ; Start-Sleep -seconds $w.totalseconds ; $ws = New-Object -com Wscript.Shell}"
echo %date% %time%
echo.
pause

goto :eof


実行例

C:\> sleep_until.bat
今度の土曜日22時: 2020/11/7 22:0:0
今度の日曜日22時: 2020/11/8 22:0:0
今月末日の22時  : 2020/11/30 22:0:0
来月1日の0時0分: 2020/12/1 0:0:0

1分後           : 2020/11/6 20:45:7

2020/11/06 20:44:07.04
2020/11/06 20:45:07.34

続行するには何かキーを押してください . . .

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0